Android7.0安装apk文件之后不弹出安装界面的问题
2019/1/10
Android7.0以下的版本,别忘了加上:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Android7.0以上的版本,还需要加上权限:
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
完整的代码如下:
//安装apk
protected void installApk(File file) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { // 7.0 以上
Uri apkUri = FileProvider.getUriForFile(this, "com.scsoft.depot.provider", file);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
} else { // 7.0以下
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
}
startActivity(intent);
}
Android 7.0以上版本还需要在manifest.xml加上一个Provider
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.scsoft.depot.provider"
android:exported="false" android:grantUriPermissions="true"> <!-- 元数据 -->
<meta-data android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/update_apk_paths" />
</provider>
对应的android:resource="@xml/
update_apk_paths"文件需要新建:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<paths>
<external-path name="scdepot" path="Pictures/scdepot" />
</paths>
</resources>