简体   繁体   English

下载完成后如何进行APK自动安装

[英]How can I make an apk auto install when download completes

Hi guys was wondering if there was a bit of code I could use that would make an app auto install once the download completes? 大家好,我想知道是否可以使用一些代码来完成下载后自动安装应用程序?

My app has a download section with in it. 我的应用程序包含一个下载部分。 I was using Google Drive to handle the downloads. 我正在使用Google云端硬盘来处理下载。 but I am encountering issues with some devices. 但是我在某些设备上遇到问题。 So I have decided to move away from google 所以我决定离开Goog​​le

I am now using media fire as my host. 我现在正在使用媒体火作为主机。 My app uses direct download. 我的应用程序使用直接下载。 But it always downloads using the download manager. 但它始终使用下载管理器进行下载。 What I would like it to do is more like how Google Drive works with direct download. 我想做的更像是Google云端硬盘如何直接下载。 Which is it gives me the option to install as soon as download completes.which i have now solved with these few lines of code 这是给我一个下载完成后立即安装的选项。我现在用以下几行代码解决了

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new 
File(Environment.getExternalStorageDirectory() + "/download/" + "app.apk")), 
"application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

is there a way to check download folder before downloading the file. 有没有一种方法可以在下载文件之前检查下载文件夹。 if the file is already there install if not got to web page for download. 如果文件已经存在,请安装,如果未下载到网页上。 rather it saying parse error then going to webpage or having multiple downloads of same file. 而是说解析错误,然后转到网页或多次下载同一文件。

Thanks in advance as always. 一如既往地感谢您。

You can get the downloaded Uri after the download complete, so you don't have to specify a file name to save. 下载完成后即可获取下载的Uri ,因此无需指定要保存的文件名。 If you use DownloadManager , below is a simple example. 如果使用DownloadManager ,则下面是一个简单的示例。

    final DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse("http://remotehost/your.apk"));
    final long id = downloadManager.enqueue(request);
    BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
                Intent installIntent = new Intent(Intent.ACTION_VIEW);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                    installIntent.setDataAndType(downloadManager.getUriForDownloadedFile(id),
                            "application/vnd.android.package-archive");
                } else {
                    Cursor cursor = downloadManager.query(new DownloadManager.Query().setFilterById(id));
                    try {
                        if (cursor != null && cursor.moveToFirst()) {
                            int status = cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS));
                            String localUri = cursor.getString(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_LOCAL_URI));
                            if (status == DownloadManager.STATUS_SUCCESSFUL) {
                                installIntent.setDataAndType(Uri.parse(localUri), "application/vnd.android.package-archive");
                            }
                        }
                    } finally {
                        if (cursor != null) {
                            cursor.close();
                        }
                    }
                }
                installIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.sendBroadcast(installIntent);
            }
        }
    };
    registerReceiver(broadcastReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以编程方式下载和安装 apk? - How do I programmatically download and install an apk? 如何下载apk并安装在android 6上 - how to download apk and install on android 6 如何检查 apk 文件是否已安装在我的模拟器中? - How can i check apk file is already install in my Emulator? 如何 adb 将 apk 安装到多个连接的设备? - How can I adb install an apk to multiple connected devices? 是否可以制作一个脚本/ apk,该脚本/ apk将下载另一个apk,进行安装并在应用程序表单中插入指定的文本? - Is it possible to make a script/apk that will download another apk, install it and insert a specified text to the app form? Android自动下载并安装APK - Android automatically download and install apk Android DownloadManager-下载并安装APK - Android DownloadManager - Download apk and install it 如何在android studio中远程下载和安装apk更新? - How to download and install an apk update in android studio remotly? 执行主类时如何使 bash 自动完成 java 限定的类名 - How can I make bash auto-complete java qualified class names when executing a main class 在 Play 框架中文件下载完成时的通知 - Notification when file download completes in Play framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM