下载apk文件与下载管理器管理器、文件、apk

2023-09-06 06:05:49 作者:是夢擾了她。

你好我在这里面临的一个小问题。我的应用程序的更新(即未在Play商店中有售)我通过下载管理器下载到Donwload directoryof设备。该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.

下载我弹出一个对话框,如果用户想要安装更新(不是恶意的或者什么的,无需根)之后。一切正常的除了用户取消该对话框,并希望为手动通过点击下载diretory下载的文件来安装APK。如果是的话,我没有得到的包安装程序以选择要打开的文件。在一些设备上的HTML浏览器打开没有问题。如果我从浏览器下载APK通过QR- code(直接链接),一切都很好,所以我想这是一个失败的下载管理器来了。

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文件中的与下载管理器的,因此,它被公认为是.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?

这里的$ C $下下载和installIntent:

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

正如你可以看到我做了

As you can see I did

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

将做到这一点......

will do it...