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

时间:2015-10-24 19:24:53

标签: 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));

}

2 个答案:

答案 0 :(得分:3)

你可以这样做:

你的清单中的

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

答案 1 :(得分:1)

安装apk check this

退出应用使用finish();