以请求获取写和读外部权限为例,Android 11以上需要额外文件管理权限,在最后有介绍 EasyPermissions官方github的链接
EasyPermissions的使用方法
在Manifest中添加:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
//android:requestLegacyExternalStorage="true"//此属性允许在设置中手动开启储存权限
在MainActivity中
private static final int RC_WRITE_AND_READ_EXTERNAL_STORAGE = 300;//识别码
@AfterPermissionGranted(RC_WRITE_AND_READ_EXTERNAL_STORAGE) //This is optional
private void requireWriteAndReadExternalStoragePermission() {
String[] perms = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};
if (EasyPermissions.hasPermissions(this, perms)) {
// 已经拥有相关权限
init();//初始化 *** 作 创建这个函数,把拥有相关权限之后的一些 *** 作放进去
} else {
// 没有拥有权限,去获取
EasyPermissions.requestPermissions(this, getString(R.string.permissions),
RC_WRITE_AND_READ_EXTERNAL_STORAGE, perms);
}
}
重写和实现以下方法
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
@Override
public void onPermissionsGranted(int requestCode, List<String> list) {
// 当权限授予后执行的 *** 作
init();
}
@Override
public void onPermissionsDenied(int requestCode, List<String> list) {
//当权限被拒绝后执行的 *** 作
Toast.makeText(this, getString(R.string.permissions_error), Toast.LENGTH_SHORT).show();
//下面这个将显示一个直接去设置里手动开启权限的对话框
if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
new AppSettingsDialog.Builder(this)
.setPositiveButton(R.string.authorize_msg)
.setTitle(R.string.authorize_title_msg)
.setRationale(R.string.modify_permission_msg)
.build()
.show();
}
}
private boolean hasExteralPermissions() {
String[] perms = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};
return EasyPermissions.hasPermissions(this, perms);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// 从设置里返回后,对用户授予的权限进行判断
if (requestCode == AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE) {
if (hasExteralPermissions() ? true : false) {
init();
} else {
Toast.makeText(this, "权限获取失败,即将关闭应用", Toast.LENGTH_SHORT).show();
finish();//结束程序
}
}
if (requestCode == 0x99){
if (Environment.isExternalStorageManager() ? true : false) {
init();
} else {
finish();
Toast.makeText(this, "权限获取失败,即将关闭应用", Toast.LENGTH_SHORT).show();
}
}
}
注意Android 11 需要授予所有文件的管理权限, 否则不可以运行
在Manifest额外添加:
只需改良以上方法:
@AfterPermissionGranted(RC_WRITE_AND_READ_EXTERNAL_STORAGE) //This is optional
private void requireWriteAndReadExternalStoragePermission() {
String[] perms = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};
if (EasyPermissions.hasPermissions(this, perms)) {
// 已经拥有相关权限
isManager();//修改这里!!! 如果是安卓11以上 获取文件管理者
} else {
// 没有获取相关权限 去申请权限
EasyPermissions.requestPermissions(this, getString(R.string.permissions),
RC_WRITE_AND_READ_EXTERNAL_STORAGE, perms);
}
}
//添加以下
public boolean gtSdk30() { //获取当前系统版本是否安卓11以上
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.R;
}
//R = 安卓11,O = 8, M = 6
private void isManager() {
if (gtSdk30()) {//判断当前手机系统版本
if (Environment.isExternalStorageManager()) init();
else getManager();
} else init();
}
private void getManager() {
AlertDialog alertDialog;//生成一个对话框 可跳转设置里手动开启权限
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.DialogStyle);//嫌麻烦,样式可设为null
builder.setPositiveButton(getString(R.string.authorize_msg), null);
builder.setTitle(getString(R.string.authorize_title_msg));
builder.setMessage(getString(R.string.file_manger_msg));
builder.setCancelable(false);
alertDialog = builder.create();
alertDialog.show();
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(v -> {
alertDialog.dismiss();//去获取文件管理
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, 0x99);
});
}
所需res资源
string
<!-- 权限文本 -->
<string name="permissions">为了正常使用,需要以下权限!</string>
<string name="permissions_error">未能获取到相关权限</string>
<!-- getManager-->
<string name="authorize_msg">点击前往授权</string>
<string name="authorize_title_msg">缺少必要权限</string>
<string name="file_manger_msg">Android 11 需要授予所有文件的管理权限</string>
<string name="modify_permission_msg">应用不能正常运行,需要在设置里手动开启访问储存权限</string>
theme对话框样式
嫌麻烦的话 把对话框样式去掉即可
<style name="DialogStyle" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorSecondary">@color/colorPrimary</item>
<item name="colorSurface">@color/colorPrimary</item>
<item name="colorOnSurface">@color/colorPrimary</item>
<item name="materialAlertDialogTitleTextStyle">@style/MaterialAlertDialog.App.Title.Text</item>
<item name="buttonBarPositiveButtonStyle">@style/ThemeOverlay.App.Button</item>
<item name="buttonBarNeutralButtonStyle">@style/ThemeOverlay.App.Button</item>
<item name="buttonBarNegativeButtonStyle">@style/ThemeOverlay.App.Button</item>
</style>
<style name="MaterialAlertDialog.App.Title.Text" parent="MaterialAlertDialog.MaterialComponents.Title.Text">
<item name="android:textColor">@color/colorPrimary</item>
<item name="android:textSize">18sp</item>
<item name="android:textStyle">bold</item>
</style>
<style name="ThemeOverlay.App.Button" parent="Widget.MaterialComponents.Button.TextButton">
<item name="android:textColor">@color/colorAccent</item>
<item name="android:textSize">15sp</item>
</style>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)