android.permission.REQUEST_INSTALL_PACKAGES
1) res中建一个目录:xml
新建update_apk_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="downloadFile" path="jyappdownload/" />
<external-path name="external_storage_root" path="." />
</paths>
2)java下包名更路径中 新建UpdateApkFileProvider.java
public class UpdateApkFileProvider extends FileProvider {
}
3) AndroidManifest.xml 中配置
配置8.0权限
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
provider
<provider android:name=".UpdateApkFileProvider" android:authorities="${applicationId}.update.provider" android:exported="false" android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/update_apk_paths" />
</provider>
4)使用
/** * 判断是否是8.0,8.0需要处理未知应用来源权限问题,否则直接安装 */
public void checkIsAndroidO() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
boolean b = mContext.getPackageManager().canRequestPackageInstalls();
if (b) {
installAPk(mContext, downLoadfile);//安装应用的逻辑
} else {
Activity activity = AppDroid.getInstance().uiStateHelper.getCurrentActivity().get(); //请求安装未知应用来源的权限
ActivityCompat.requestPermissions(activity, new String[] {Manifest.permission.REQUEST_INSTALL_PACKAGES}, INSTALL_PACKAGES_REQUESTCODE);
}
} else {
installAPk(mContext, downLoadfile);
}
}
参考如下: