Android学习之----利用DownLoadManager实现版本升级

参考 http://www.trinea.cn/android/android-downloadmanager/

http://jk2k.com/2016/01/install-apk-automatically-after-the-download-is-complete-with-download-manager/


效果:

Android学习之----利用DownLoadManager实现版本升级_第1张图片

通知栏显示


结果

不写demo了 直接看代码  需要可以直接传入下载地址 使用即可

public class VersionUpdate {
    private DownCompleteReceiver downCompleteReceiver;
    private static VersionUpdate versionUpdate = new VersionUpdate();

    public static VersionUpdate newInstance() {
        return versionUpdate;
    }


    private VersionUpdate() {

    }

    public void gotoUpdate(Context context, String url) {
        String appName=context.getResources().getString(R.string.app_name);
        DownloadManager downloadManager = (DownloadManager) context.getSystemService(context.DOWNLOAD_SERVICE);
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
        request.setTitle(appName);
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.setDescription(appName+"升级中...");
        request.setVisibleInDownloadsUi(true);
        long downloadId = downloadManager.enqueue(request);
         downCompleteReceiver = new DownCompleteReceiver(downloadId);
        context.registerReceiver(downCompleteReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
        request.setDestinationInExternalPublicDir(appName, appName+".apk");
    }



    public void createDialogUpdate(final Context context, final String url) {
        AlertDialog.Builder alert=new AlertDialog.Builder(context);
        alert.setTitle("更新提示")
                .setMessage("发现新版本,是否立即更新?")
                .setCancelable(false).setNeutralButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).setPositiveButton("立即更新", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                gotoUpdate(context, url);
                dialog.dismiss();
            }
        });
        alert.create().show();
    }


    public class DownCompleteReceiver extends BroadcastReceiver {
        long enqueueId;

        public DownCompleteReceiver(long enqueueId) {
            this.enqueueId = enqueueId;
        }

        @Override
        public void onReceive(Context context, Intent intent) {

            DownloadManager dm = (DownloadManager) context.getSystemService(context.DOWNLOAD_SERVICE);

            long id = intent.getExtras().getLong(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
            LogcatUtils.d(DownCompleteReceiver.class.getSimpleName(), "id = " + id + " , ");

            if (enqueueId != id) {
                return;
            }

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

            Cursor c = dm.query(query);

            if (c != null && c.moveToFirst()) {
                int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
                // 下载失败也会返回这个广播,所以要判断下是否真的下载成功
                if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
                    // 获取下载好的 apk 路径
                    String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
                    // 提示用户安装
                    installAPP(Uri.parse("file://" + uriString), context);
                }
                c.close();
            }
        }

        private void installAPP(Uri data, Context context) {
            Intent promptInstall = new Intent(Intent.ACTION_VIEW)
                    .setDataAndType(data, "application/vnd.android.package-archive");
            // FLAG_ACTIVITY_NEW_TASK 可以保证安装成功时可以正常打开 app
            promptInstall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(promptInstall);
        }
    }

    public void  unregisterReceiver(Context context){
        if(downCompleteReceiver!=null)
        context.unregisterReceiver(downCompleteReceiver);
    }

}



使用:  VersionUpdate.newInstance().createDialogUpdate(this,url);


最后记得 取消 广播VersionUpdate.newInstance().unregisterReceiver()



结束


写的粗糙  有问题往指出




你可能感兴趣的:(DownloadManager)