Android app内更新,兼容7.0
2018-01-15 10:35
375 查看
之前项目有需求做app内更新,试了很多都不行,最后自己结合网上找到的资料,自己写了一个工具类,兼容Android7.0,有问题的话欢迎留言。二话不说直接上代码吧
/** * 2018-01-09 haoshiwei * app内部更新的工具类 * 兼容7.0 */ public class DownloadUtils { //下载器 private DownloadManager downloadManager; //上下文 private Context mContext; //下载的ID private long downloadId; public DownloadUtils(Context context){ this.mContext = context; } //下载apk public void downloadAPK(String url, String name) { //创建下载任务 DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); //移动网络情况下是否允许漫游 request.setAllowedOverRoaming(false); //在通知栏中显示,默认就是显示的 request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE); request.setTitle("狐小狸"); request.setDescription("Apk Downloading"); request.setVisibleInDownloadsUi(true); //设置下载的路径 request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS , name); //获取DownloadManager downloadManager = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE); //将下载请求加入下载队列,加入下载队列后会给该任务返回一个long型的id,通过该id可以取消任务,重启任务、获取下载的文件等等 downloadId = downloadManager.enqueue(request); //注册广播接收者,监听下载状态 mContext.registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); } //广播监听下载的各个状态 private BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { checkStatus(); } }; //检查下载状态 private void checkStatus() { DownloadManager.Query query = new DownloadManager.Query(); //通过下载的id查找 query.setFilterById(downloadId); Cursor c = downloadManager.query(query); if (c.moveToFirst()) { int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)); switch (status) { //下载暂停 case DownloadManager.STATUS_PAUSED: break; //下载延迟 case DownloadManager.STATUS_PENDING: break; //正在下载 case DownloadManager.STATUS_RUNNING: break; //下载完成 case DownloadManager.STATUS_SUCCESSFUL: //下载完成安装APK installAPK(); break; //下载失败 case DownloadManager.STATUS_FAILED: Toast.makeText(mContext, "下载失败", Toast.LENGTH_SHORT).show(); break; } } c.close(); }
//下载到本地后执行安装private void installAPK() {//获取下载文件的UriUri downloadFileUri = downloadManager.getUriForDownloadedFile(downloadId);if (downloadFileUri != null) {Intent intent= new Intent(Intent.ACTION_VIEW);intent.setDataAndType(downloadFileUri, "application/vnd.android.package-archive");intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);mContext.startActivity(intent);mContext.unregisterReceiver(receiver);}}}这里使用了Android自带的downloadManager,但是有一个 问题就是在Android7.0以上的手机不兼容。
/*** 7.0兼容*/private void installAPK() {File apkFile =new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "huxiaoli.apk");Intent intent = new Intent(Intent.ACTION_VIEW);intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);if (Build.VERSION.SDK_INT >= 24) {Uri apkUri = FileProvider.getUriForFile(mContext, mContext.getPackageName() + ".provider", apkFile);intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);intent.setDataAndType(apkUri, "application/vnd.android.package-archive");} else {intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");}mContext.startActivity(intent);}这里我们在7.0以上的手机上使用file.provider要在manifest文件里面配置一下
<providerandroid:name="android.support.v4.content.FileProvider"android:authorities="你的包名.provider"android:exported="false"android:grantUriPermissions="true"><meta-dataandroid:name="android.support.FILE_PROVIDER_PATHS"android:resource="@xml/provider_paths"/></provider>在drawable目录下创建xml包下面创建provider_paths文件
<?xml version="1.0" encoding="utf-8"?><resources xmlns:android="http://schemas.android.com/apk/res/android"><paths><external-pathname="download"path=""/></paths></resources>这样就大功告成了,有问题的话欢迎留言
相关文章推荐
- android app 更新下载安装 适配android 7.0
- Android使用DownloadMange进行版本更新(兼容7.0)
- android App更新7.0权限
- App版本更新兼容7.0
- 记APP实现多语言(国际化)过程,兼容Android 7.0以上
- Android版本更新-----兼容android7.0
- 直接拿来用的版本更新(Android 兼容7.0)
- [Android开发] app在华为手机的应用市场和安装时候显示未兼容android7.0
- Android应用更新详解,兼容7.0
- Android下载更新(兼容7.0文件,支持5.0通知栏显示Icon)
- Android 版本更新工具 兼容7.0
- APP版本更新(兼容6.0,7.0系统)
- Android 在线下载更新App 下载完成安装APK(兼容Android7.0)
- android自动更新代码实现------------安卓开发第一步:让APP学会自动更新
- android 测试更新app时 提示“未安装应用程序”
- Android通过DownloadManager实现App的版本更新功能
- [置顶] Android 7.0 系统解决拍照的问题 exposed beyond app through ClipData.Item.getUri()
- Android如何实现APP自动更新
- Android系列学习讲座之二--App自动更新之通知栏下载
- android版本更新适配7.0,解决7.0手机无法更新安装apk