android - 使用下载管理器下载后安装 apk 并退出应用程序

标签 android broadcastreceiver android-download-manager

我已经创建了一个 Android 应用程序,如果有任何新版本发布,它会从服务器使用内置的“下载管理器”自动开始下载。为了在完成下载后自动安装,我创建了一个广播接收器来通知下载已经完成并完成,然后我开始安装它。当我留在应用程序中并且不关闭它时,它工作正常。但我的问题是当我关闭应用程序时。所以在完成下载后我想自动安装它。但我不会发生。我应该为这个问题做什么?

    private void download(String link) {
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(link));
    request.setDescription("new version");
    request.setTitle("app name");
    request.setMimeType("application/vnd.android.package-archive");
    request.setDestinationInExternalFilesDir(getApplicationContext(), Environment.DIRECTORY_DOWNLOADS, "myapk.apk");
    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

    downloadID = dm.enqueue(request);


    br = new BroadcastReceiver() {

        @Override
        public void onReceive(Context c, Intent i) {
            String action = i.getAction();

            if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {

                DownloadManager.Query query = new DownloadManager.Query();
                query.setFilterById(downloadID);

                Cursor downloadResult = dm.query(query);

                if (downloadResult.moveToFirst()) {
                    int statusColumnIndex = downloadResult.getColumnIndex(DownloadManager.COLUMN_STATUS);
                    int status = downloadResult.getInt(statusColumnIndex);

                    if (status == DownloadManager.STATUS_SUCCESSFUL) {
                        //download completed successfully
                        int localFileNameId = downloadResult.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME);

                        String downloadPathFile = downloadResult.getString(localFileNameId);
                        String downloadPathDir = downloadPathFile.substring(0, downloadPathFile.lastIndexOf("/") + 1);
                        String downloadName = downloadPathFile.substring(downloadPathFile.lastIndexOf("/") + 1);

                        Log.i("name =", downloadName);

                        File file = new File(downloadPathDir);
                        File[] files = file.listFiles();
                        for (File f : files) {
                            if (f.isFile() && f.exists() && !f.getName().equals(downloadName)) {
                                f.delete();
                            }
                        }

                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setDataAndType(Uri.fromFile(new File(downloadPathFile)), "application/vnd.android.package-archive");
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                        unregisterReceiver(br);
                    }
                }
            }
        }
    };

    registerReceiver(br, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

}

最佳答案

你可以这样做:

在你的 list 中

<receiver android:name="packageName.DownloadReceiver" android:exported="true"> 
    <intent-filter> 
        <action android:name="android.intent.action.DOWNLOAD_COMPLETE"/> 
    </intent-filter> 
</receiver>

然后在 DownloadReceiver.java 中添加处理自动安装的逻辑

public class DownloadReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
            // add your logic here
        }
    }
}

关于android - 使用下载管理器下载后安装 apk 并退出应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33322368/

相关文章:

java - Android DownloadManager 在某些情况下不起作用

Android Mapview 自定义标记不适用于 Picasso

android - 设置半透明时没有方向通知

android - PendingIntents 保持缓存相同的对象

android - DownloadManager 不添加文件扩展名

android - 当按下 RecyclerView 内的按钮时如何下载文件?

android - 使用bottomNavView时如何在 fragment 之间传递数据

安卓小工具 : OnClickPendingIntent won't Work

android - 检查 broadcstreceiver 是否已注册?

android - 使用 Install Referrer 获取(not%20set)UTM 参数 - Android