Android 7.0 以上 安装 apk 的方法

1.安装apk方法

/**

*

* @param context 上下文

* @param url apk安装路径

*/

public static void doInstall(Context context, String url) {

File apkFile = new File(url);

Intent intent = new Intent(Intent.ACTION_VIEW);

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

//判断是否是7.0以上版本

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

Uri contentUri = FileProvider.getUriForFile(context, "包名.fileprovider", apkFile);

intent.setDataAndType(contentUri, "application/vnd.android.package-archive");

} else {

//7.0以下安装apk

intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");

}

context.startActivity(intent);

}

 

2.在AndroidManifest.xml 文件中添加provider 如下:

 

<provider

android:name=".util.AppUpdateFileProvider"

android:authorities="${applicationId}.fileprovider"

android:exported="false"

android:grantUriPermissions="true">

<meta-data

android:name="android.support.FILE_PROVIDER_PATHS"

android:resource="@xml/update_paths" />

</provider>

 

其中 AppUpdateFileProvider是 util包下的 类 如下:

public class AppUpdateFileProvider extends FileProvider { }

applicationId 是指 build.gradle文件下的包名

xml/update_paths 是指在 res包下创建 xml包 xml包下面创建的文件 是 update_paths.xml

其中内容是

 

<?xml version="1.0" encoding="utf-8"?>

<paths>

<external-path path="Android/data/${applicationId}/" name="files_root" />

<external-path path="." name="external_storage_root" />

</paths>

有什么疑问的 可以留言 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值