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

Install apk after download with download manager and exit from app

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

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

}

安装apk检查this

退出应用程序使用finish();

你可以这样做:

在您的清单中

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