安卓11读写文件适配:
安卓11需要增加管理所有文件权限:
private static final int MANAGE_APP_ALL_FILES_ACCESS_PERMISSION_REQUEST_CODE = 0; // 申请管理app所有文件权限
//1.判断系统版本,大于等于安卓11申请权限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
if (Environment.isExternalStorageManager()) {
//TODO 安卓6.0多态申请权限
} else {
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
intent.setData(Uri.parse("package:" + AppUtils.getAppPackageName()));
startActivityForResult(intent, MANAGE_APP_ALL_FILES_ACCESS_PERMISSION_REQUEST_CODE);
}
} else {
//TODO 安卓6.0多态申请权限
}
//2.申请管理app所有文件权限回调
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == MANAGE_APP_ALL_FILES_ACCESS_PERMISSION_REQUEST_CODE) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
if (Environment.isExternalStorageManager()) {
//TODO 安卓6.0多态申请权限
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle("温馨提醒");
builder.setMessage("当前应用需要读写权限,去设置?");
builder.setPositiveButton("设置", (DialogInterface dialog, int which) -> {
dialog.dismiss();
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
intent.setData(Uri.parse("package:" + AppUtils.getAppPackageName()));
startActivityForResult(intent, MANAGE_APP_ALL_FILES_ACCESS_PERMISSION_REQUEST_CODE);
});
builder.setNegativeButton("取消", (DialogInterface dialog, int which) -> {
dialog.dismiss();
});
builder.show();
}
}
}
}
2.安卓12 android:exported="true" 适配
gradle打包报错信息:
Manifest merger failed : android:exported needs to be explicitly specified for . Apps targeting Android 12 and higher are required to specify an explicit value for android:exported
when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details.
解决方案: AndroidManifest.xml->application->下面四大组件全部都需要加以下节点
android:exported="true"
//或者
android:exported="false"