在应用中更新App版本号 (3)

下载Apk: 转换和解析Url, 设置通知信息和存储位置, 存储下载Id, 自己主动安装更新.

/** * 下载Apk, 并设置Apk地址, * 默认位置: /storage/sdcard0/Download * * @param context 上下文 * @param updateInfo 更新信息 * @param infoName 通知名称 * @param storeApk 存储的Apk */ @SuppressWarnings("unused") public static void downloadApk( Context context, UpdateInfo updateInfo, String infoName, String storeApk ) { if (!isDownloadManagerAvailable()) { return; } String description = updateInfo.data.description; String appUrl = updateInfo.data.appURL; if (appUrl == null || appUrl.isEmpty()) { Log.e(TAG, "请填写\"App下载地址\""); return; } appUrl = appUrl.trim(); // 去掉首尾空格 if (!appUrl.startsWith("http")) { appUrl = "http://" + appUrl; // 加入Http信息 } Log.e(TAG, "appUrl: " + appUrl); DownloadManager.Request request; try { request = new DownloadManager.Request(Uri.parse(appUrl)); } catch (Exception e) { e.printStackTrace(); return; } request.setTitle(infoName); request.setDescription(description); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { request.allowScanningByMediaScanner(); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); } request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, storeApk); Context appContext = context.getApplicationContext(); DownloadManager manager = (DownloadManager) appContext.getSystemService(Context.DOWNLOAD_SERVICE); // 存储下载Key SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(appContext); sp.edit().putLong(PrefsConsts.DOWNLOAD_APK_ID_PREFS, manager.enqueue(request)).apply(); }

使用DownloadManager下载文件是Android的推荐方式.
存储下载Id(manager.enqueue(request))是为了在安装应用时, 找到Apk.
默认存储地址/storage/sdcard0/Download.

4.自己主动安装

注冊广播接收器, 接收消息ACTION_DOWNLOAD_COMPLETE, 下载完毕会发送广播. 获取下载文件的Uri, 进行匹配, 发送安装消息, 自己主动安装.

/** * 安装下载接收器 * <p> * Created by wangchenlong on 16/1/5. */ public class InstallReceiver extends BroadcastReceiver { private static final String TAG = "DEBUG-WCL: " + InstallReceiver.class.getSimpleName(); // 安装下载接收器 @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) { long downloadApkId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1); installApk(context, downloadApkId); } } // 安装Apk private void installApk(Context context, long downloadApkId) { // 获取存储ID SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); long id = sp.getLong(PrefsConsts.DOWNLOAD_APK_ID_PREFS, -1L); if (downloadApkId == id) { DownloadManager dManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); Intent install = new Intent(Intent.ACTION_VIEW); Uri downloadFileUri = dManager.getUriForDownloadedFile(downloadApkId); if (downloadFileUri != null) { install.setDataAndType(downloadFileUri, "application/vnd.android.package-archive"); install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(install); } else { Log.e(TAG, "下载失败"); } } } }

安装本应用下载的Apk, 不安装其它Apk, 存储下载Id, 与广播Id进行匹配.
下载失败, 也会发送下载完毕(ACTION_DOWNLOAD_COMPLETE)广播, Uri可能为空, 须要推断, 否则发生崩溃.

OK, that’s all! Enjoy It!

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzdxfw.html