Download .apk with DownloadManager

心不动则不痛 提交于 2019-11-29 15:32:48

问题


Hi I'm facing a little problem here. For updates of my App (that isn't available in Play Store) I download it via DownloadManager to the Donwload directoryof the device. The .apk file is on a ftp server.

After downloading I pop up a dialog if the user wants to install the update (not malicious or something, no root needed). Everything works fine except the user cancels this dialog and wants to "manually" install the apk by clicking on the downloaded file in the Download diretory. If so, I don't get the Package-Installer to choose to open the file. On some devices "HTML Viewer" is opened without a question. If I download the apk via QR-Code (direct link) from browser everything is fine, so I guess it's a failure coming with the DownloadManager.

How can I download a file from ftp with DownloadManager, so that it is recognized as .apk and I can install it from the download region?

Here's the code for downloading and installIntent:

String url = "http://www.test.xx/myApp.apk";
Uri mUri = Uri.parse(url);
DownloadManager.Request r = new DownloadManager.Request(mUri);
r.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "myApp.apk");
r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
DownloadManager dm = (DownloadManager)activity.getSystemService(Context.DOWNLOAD_SERVICE);
dm.enqueue(r);

And on download receive:

@Override
public void onReceive(Context context, Intent intent) {
    try {
        Bundle extras = intent.getExtras();
        long myDownloadID = DashboardActivity.this.mSharedPref.getLong(PreferenceIdentifier.DOWNLOAD_APK.toString(), 0);
        mSharedPref.edit().remove(PreferenceIdentifier.DOWNLOAD_APK.toString()).commit();
        long downloadCompletedId = extras.getLong(DownloadManager.EXTRA_DOWNLOAD_ID);
        DownloadManager.Query q = new DownloadManager.Query();

        if (myDownloadID == downloadCompletedId) {
            q.setFilterById(downloadCompletedId);
            DownloadManager mManager = (DownloadManager) MyActivity.this.getSystemService(Context.DOWNLOAD_SERVICE);
            Cursor c = mManager.query(q);
            if (c.moveToFirst()) {
                int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
                    if (status == DownloadManager.STATUS_SUCCESSFUL) {
                        Intent promptInstall = new Intent(Intent.ACTION_VIEW);
                        promptInstall.setDataAndType(
                            Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/myApp.apk")),
                                    "application/vnd.android.package-archive");
                            promptInstall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(promptInstall);
                    }
            }
            c.close();
        }

As you can see I did

promptInstall.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/myApp.apk")),
"application/vnd.android.package-archive");

I thought that would set the file type to apk so it is handles as one.

Hope I made my point clear, can anyone help?


回答1:


Yeah, well... kinda stupid..

promptInstall.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/myApp.apk")),
"application/vnd.android.package-archive");

just affects the way android want to open the file..

DownloadManager.Request r = new DownloadManager.Request(mUri);
            r.setMimeType("application/vnd.android.package-archive");

will do it...



来源:https://stackoverflow.com/questions/28171418/download-apk-with-downloadmanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!