一、权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
二、核心代码
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val data = FileProvider.getUriForFile(context, context.packageName, File("$basePath/APK/$apkName.apk"))
val intent = Intent(Intent.ACTION_VIEW)
intent.setDataAndType(data, "application/vnd.android.package-archive")
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(intent)
}else{
val data = Uri.fromFile(File("$basePath/APK/$apkName.apk"))
val intent = Intent(Intent.ACTION_VIEW)
intent.setDataAndType(data, "application/vnd.android.package-archive")
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(intent)
}
三、Android7.0(V24)相关的其他配置,如provider不在此赘述
|