用DownloadManager下载.apk[英] Download .apk with DownloadManager

本文是小编为大家收集整理的关于用DownloadManager下载.apk的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

嗨,我在这里面临一个小问题.对于我的应用程序的更新(在Play商店中不可用),我通过DownloadManager下载到设备的Donwload目录. .APK文件在FTP服务器上.

下载后,如果用户要安装更新(不是恶意或其他需要根本不需要的根),则弹出一个对话框.一切正常,除了用户取消此对话框,并希望通过单击下载的Diretory中的下载文件来"手动"安装APK.如果是这样,我否则请不要让软件包安装程序选择打开文件.在某些设备上," HTML查看器"开放,没有问题.如果我从浏览器下载QR代码(直接链接)的APK,一切都很好,所以我想下载管理器会发生故障.

如何使用下载manager 从ftp 下载文件,以便将其识别为.apk,我可以从下载区域安装它?

这是用于下载和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);

和下载接收:

@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();
        }

您可以看到我做过

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

我认为将文件类型设置为APK,因此将其作为一个.

希望我能明确我的观点,有人可以帮忙吗?

推荐答案

是的,好吧...有点愚蠢..

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

只是影响Android想要打开文件的方式.

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

会做...

本文地址:https://www.itbaoku.cn/post/102516.html

问题描述

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?

推荐答案

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...