-2

The problem is, after I download .apk-file from server using this method:

public void Update(String apkName) {
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));

    File file = new File("/sdcard/Download/"+ apkName + ".apk");
    if (file.exists()) {
        file.delete();
    }

    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS + "/", apkName + ".apk");       
    DownloadManager manager = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
    manager.enqueue(request);
}

then I use BroadCastReciever to take install-action when the download-operation is complete as follows:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
// some codes here...

BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)){
                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/Download/" + apkName + ".apk")),
                        "application/vnd.android.package-archive");
                startActivity(i);
            } else {

            }

        }
    };

    getActivity().registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

    download_apk(apkName);
    return rootView;
}

Now problem is when the download-opration is complete DownloadManager try to install apk-file and I get this which is "Parse Error/There is a Problem parsing the package".

after I click OK, true Install-page shows up (which is triggered by broadcastReciever).

I run this proccess in Activity and it was perfectly fine. As you see above this time it is in Fragment! is this could be the reason??

How can I stop DownloadManager from running apk Automatically? can anybody help me out?

Jalal Aghazadeh
  • 164
  • 4
  • 18

1 Answers1

0

why Download-Manager automatically run (downloaded) apk-file in android?

Download manager does NOT start download APK automatically.

How can I stop DownloadManager from running apk Automatically?

Apparently you do not understand code you pasted in your question, so what touches APK is broadcast received. Just remove it and its registration and you are done.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • I did and Parse-error shows up. BroadCastReciever is doing fine. I need to stop 1st run which is some how executed. – Jalal Aghazadeh Jul 15 '15 at 12:39
  • this is done by broadcast receiver's startActivity. So it either doing fine and in such case what's your question or you need to get rid of it to stop auto install. – Marcin Orlowski Jul 15 '15 at 13:29
  • As you see my code above, I only run apk once but after downloading apk it runs twice: 1st one fails and 2nd one succeed. I need to know how 1st run happens and get rid of it. – Jalal Aghazadeh Jul 15 '15 at 19:00