Android7.0之安装apk文件
16lz
2021-01-26
由于Android7.0不能直接访问Uri导致安装apk失败这时候就要用到provider
接下来修改代码:
首先在AndroidMainifest.xml文件中添加:
<provider android:name="android.support.v4.content.FileProvider" android:authorities="这里写包名.fileProvider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> provider>
android:exported=”false”(必须为false)
android:grantUriPermissions=”true”(必须为true)不然会报错
然后在res下新建一个xml的文件夹,创建file_paths文件:
<?xml version="1.0" encoding="utf-8"?><paths> <external-path path="Android/data/这里写报名/" name="files_root" /> <external-path path="." name="external_storage_root" />paths>
接下来就是安装apk文件的代码
//判断Android系统版本号 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); Uri contentUri = FileProvider.getUriForFile(getApplicationContext(), "这里写包名" + ".fileProvider", apkfile); intent.setDataAndType(contentUri, "application/vnd.android.package-archive"); } else { //Android7.0之前安装apk intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType(Uri.fromFile(apkfile), "application/vnd.android.package-archive"); } startActivity(intent);
接下来就可以正常安装了。
注意检查包名是否正确,安装apk代码里的包名要和清单文件一致不然安装时候会报错。
更多相关文章
- Pro Android学习笔记(五八):Preferences(2):CheckBoxPreference
- android client随机验证码生成函数
- [android]MonkeyRunner-andrec实现思路
- Android下图像文件获取方式小结
- 获得android手机的联网状态
- Eclipse安装Android插件的问题
- Android(安卓)网络应用重点———使用HttpGet 下载apk文件并安装
- Android(安卓)Arduino 蓝牙模块通信源代码
- Button按钮的单击事件