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

查看:32
本文介绍了使用 DownloadManager 下载 .apk的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里遇到了一个小问题.对于我的应用程序的更新(在 Play 商店中不可用),我通过 DownloadManager 将其下载到设备的 Donwload 目录..apk 文件位于 ftp 服务器上.

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.

下载后,如果用户想要安装更新,我会弹出一个对话框(不是恶意的或其他什么,不需要 root).一切正常除了用户取消此对话框并希望通过单击下载目录中的下载文件手动"安装apk.如果是这样,我不会让 Package-Installer 选择打开文件.在某些设备上,HTML 查看器"会毫无问题地打开.如果我通过 QR 码(直接链接)从浏览器下载 apk,一切都很好,所以我猜这是下载管理器的失败.

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.

如何从 ftp 使用 DownloadManager 下载文件,使其被识别为 .apk 并且我可以从下载区域安装它?

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?

这是下载和安装Intent的代码:

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);

下载时接收:

@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 以便将其作为一个处理.

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");

只影响android打开文件的方式..

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

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

会做...

这篇关于使用 DownloadManager 下载 .apk的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆