Android--用DownLoadManager下载完成后启动安装
2015-12-01 22:10
411 查看
当我们用系统的服务DownLoadManager下载完成后,系统会发送一个广播,我们只需要注册一个广播,然后在广播里面写如一些相应的操作。
1、注册广播
2、接收广播
3、启动安装
1、注册广播
completeReceiver = new CompleteReceiver(); registerReceiver(completeReceiver,new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));//注册广播
2、接收广播
//接收到这个广播 class CompleteReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { openFile(intent, context);//启动安装 } }
3、启动安装
//启动安装 private void openFile(Intent intent,Context context){ long myDownLoadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID,-1); //拿到下载的Id SharedPreferences preferences = context.getSharedPreferences("downloadcomplete",0); long refence = preferences.getLong("refernece",0); if (refence == myDownLoadId){ //如果是我们下载完成后发送的广播消息,那么启动并安装 DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); Intent updateApk = new Intent(Intent.ACTION_VIEW); Uri downloadFileUri = downloadManager.getUriForDownloadedFile(myDownLoadId); updateApk.setDataAndType(downloadFileUri, "application/vnd.android.package-archive"); updateApk.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(updateApk); } }
相关文章推荐
- Android Drawable
- android textview字体大小设置
- Afinal框架的解析(一)
- android开发之Fragment利用Bundle保存状态
- 使用AsyncTaskLoader获取本地数据(图片)
- android 加密 SQLCipher和Conceal
- android shape的使用
- android下播放器视频输出方法总结
- 版本控制器之-git的简单用法
- android自定义View之刻度尺MnScaleBar
- 支付宝集成获取私钥与公钥
- android CHMOD命令用法
- [Android学习笔记八] 使用VideoView屏幕方向发生变化,视频方向自动切换
- Android异步加载(二)
- android 计算器
- android118 上拉下拉刷新列表listView实现
- Android之下拉列表Spinner控件使用
- android控件的监听绑定方法
- android studio 9.png 报错
- Android Manifest标签之data, grant-uri-permission, instrumentation, intent-filter