检查app版本更新并调用系统下载并显示到通知栏
2016-11-21 17:53
585 查看
1.下载apk代码片段
2.在application中onCreate注册广播
IntentFilter downLoadfilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
BroadcastReceiver downloadReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
4.安装apk代码片段
private void downloadApk(String url, Activity activity) { DownloadManager downloadManager=(DownloadManager)activity.getSystemService(Context.DOWNLOAD_SERVICE); // 开始下载 Uri resource = Uri.parse(url); DownloadManager.Request request = new DownloadManager.Request(resource); request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI); request.setAllowedOverRoaming(false); // 设置文件类型 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton(); String mimeString = mimeTypeMap.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(url)); request.setMimeType(mimeString); // 在通知栏中显示 request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE); request.setVisibleInDownloadsUi(true); // sdcard的目录下的download文件夹 request.setDestinationInExternalPublicDir("/download/", url.substring(url.lastIndexOf("/") + 1, url.length())); request.setTitle(getApplication().getApplicationInfo().loadLabel(getPackageManager())); //将下载apk的id传给application long id = downloadManager.enqueue(request); MyApplication application = (MyApplication) getApplication(); application.downLoadId = id; }
2.在application中onCreate注册广播
IntentFilter downLoadfilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
BroadcastReceiver downloadReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) { VersionUtils.installApk(getDownloadedFileName(), context); unregisterReceiver(downloadReceiver); } } }; registerReceiver(downloadReceiver, downLoadfilter);文件名代码片段
//获取下载apk的名字 //针对安卓7.0,DownloadManager.COLUMN_LOCAL_FILENAME已不适用 private String getDownloadedFileName() { String fileName = ""; DownloadManager downloadManager = (DownloadManager) getSystemService(Activity.DOWNLOAD_SERVICE); DownloadManager.Query query = new DownloadManager.Query(); query.setFilterByStatus(DownloadManager.STATUS_SUCCESSFUL); Cursor c = downloadManager.query(query); while (c.moveToNext()) { long id = c.getLong(c.getColumnIndex(DownloadManager.COLUMN_ID)); if (id == downLoadId) { if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) { String fileUri = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)); fileName = Uri.parse(fileUri).getPath(); } else { fileName = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME)); } break; } } if (c!=null){ c.close(); } return fileName; }
4.安装apk代码片段
public static void installApk(String path, Context act) { File apkfile = new File(path); if (!apkfile.exists()) { return; } // 通过Intent安装APK文件 Intent intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType(Uri.fromFile(apkfile), "application/vnd.android.package-archive"); act.startActivity(intent); }
相关文章推荐
- android 客户端版本检测更新,服务下载,通知栏显示
- App自动更新之通知栏下载 版本更新 与 升级
- App强制更新时,点击进行下载的逻辑,分为调用原生浏览器下载和自定义显示进度条下载两种
- 转 android客户端版本检测更新,服务下载,通知栏显示
- 版本检测更新 调用AppStore 显示自己的app
- app版本更新和下载,通知栏实时进度(使用服务和广播)
- Android 通过蒲公英pgyer的接口 Service 实现带进度下载App 通知栏显示 在线更新 自动更新Demo
- android客户端版本检测更新,服务下载,通知栏显示
- Android 通过蒲公英pgyer的接口 Service 实现带进度下载App 通知栏显示 在线更新 自动更新Demo
- android客户端版本检测更新,服务下载,通知栏显示
- App自动更新之通知栏下载
- Android学习系列(2)--App自动更新之通知栏下载
- Android学习系列(2)--App自动更新之通知栏下载
- 提示用户进行版本更新并且发布通知监控版本下载情况
- App自动更新之通知栏下载
- 一个APP下载升级的Demo(通知栏实时更新下载进度)------(一)
- 【Android】Web开发之通知栏下载更新APP
- iOS 检查app版本更新操作
- iOS学习笔记31—appStore检查版本更新与从app里跳到appstore评论页面
- appStore检查版本更新与从app里跳到appstore评论页面