您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
91_如何通过代码安装一个apk文件-补充
发布时间:2021-03-30 15:49:17编辑:雪饮阅读()
android.os.FileUriExposedException异常
昨天在处理以代码安卓一个apk文件方面问题时候关于android.os.FileUriExposedException错误的解决,因为太晚了,还有其它事情等,所以文档写的有点粗犷。那么这里单独开篇细说下。
1、在AndroidManifest.xml中添加如下代码
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="app的包名.fileProvider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
这段代码必须是在application节点下
注意:
authorities:app的包名.fileProvider
grantUriPermissions:必须是true,表示授予 URI 临时访问权限
exported:必须是false
resource:中的@xml/file_paths是我们接下来要添加的文件
2、在res目录下新建一个xml文件夹,并且新建一个file_paths的xml文件
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path path="Android/data/app的包名/" name="files_root" />
<external-path path="." name="external_storage_root" />
</paths>
path:需要临时授权访问的路径(.代表所有路径)
name:就是你给这个访问路径起个名字
4、修改代码适配Android N或安卓7以后
//添加flags,表明我们要被授予什么样的临时权限
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//BuildConfig.APPLICATION_ID直接是应用的包名
Uri contentUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileProvider", apkFile);
intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
Intent.ACTION_VIEW
另外昨天太赶了(急着想赶紧处理结束然后和蓓蓓聊天,但最后结束后还是23点了,想着太晚了不好打扰人家,心有不甘的睡了。。。)就把Intent.ACTION_VIEW也忽略了,心里默认大家都能懂的。
Intent.ACTION_VIEW用于显示用户的数据。比较通用,会根据用户的数据类型打开相应的Activity。比如 tel:13400010001打开拨号程序,比如发送短信等
代码1:
//打开拨号程序
Uri uri =Uri.parse("tel:1232333");
Intent it = new Intent(Intent.ACTION_VIEW,uri);
startActivity(it);
代码2:
//调用发送短信的程序
Intent it = new Intent(Intent.ACTION_VIEW);
it.putExtra("sms_body", "信息内容...");
it.setType("vnd.android-dir/mms-sms");
startActivity(it);
关键字词:android,apk,代码,安装