android 7.0apk安装问题
一直做的都是电视端的项目,突然有一天要维护以前的代码 ,客户反馈手机更新下载完成,然后就崩溃了 ,我很奇怪,手上的几个测试机都是OK 的 。后来确认他的手机是7.0的abdroid 系统 , 那么问题就来了 ,勘定是google添加的权限问题
不说废话了,直接上代码
清单文件,注意那个是项目的包名
<!--android 7.0apk安装使用--> <provider android:name="android.support.v4.content.FileProvider" android:authorities="com.mmb.activity.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider>
2:清单文件引用的布局
<?xml version="1.0" encoding="utf-8"?> <paths> <external-path name="files_root" path="Android/data/com.mmb.activity/" /> <external-path name="external_storage_root" path="." /> </paths>
3:Activity中引用,判断系统版本
/** * 安装APK工具类 * * @param filePath 文件路径 */ public void installAPK(String filePath) { try { String authorities = "com.mmb.activity.fileprovider"; Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); File apkFile = new File(filePath); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); Uri contentUri = FileProvider.getUriForFile(context, authorities, apkFile); intent.setDataAndType(contentUri, "application/vnd.android.package-archive"); } else { intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive"); } context.startActivity(intent); } catch (Exception e) { } }
几个圈主的地方,都是包名 ,顺利的话 ,一次都可以集成,我还是研究了好半天