在AndroidManifest.xml中添加<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
public void apkNeedInstall(String address) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (getPackageManager().canRequestPackageInstalls()) {
install(address);//安装
} else {
//友好点的弹窗提示~ 需要打开安装未知来源应用权限~ 点击确认
toInstallPermissionSettingIntent();
}
} else {
install(address);//安装
}
private void toInstallPermissionSettingIntent() {//打开安装未知来源的设置界面
Uri packageURI = Uri.parse("package:" + getPackageName());
Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, packageURI);
startActivityForResult(intent, INSTALL_PERMISS_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == INSTALL_PERMISS_CODE) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (getPackageManager().canRequestPackageInstalls()) {//再次判断有没有授予
installApp(address);//安装
}
}
}
}