Android 7.0 获取自定义权限

释放双眼,带上耳机,听听看~!
private String[] permissions = {Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
// Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.GET_ACCOUNTS,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.RECORD_AUDIO,
Manifest.permission.CALL_PHONE,
// Manifest.permission.READ_EXTERNAL_STORAGE
};
String content[]={"拍照和录制视频权限","读写存储卡权限","获取手机账户列表权限","手机定位权限","录制音频权限","打电话权限"};
private void checkPermissions() {
if (Build.VERSION.SDK_INT >= 23) {
ActivityCompat.requestPermissions(this, permissions, 321);
}
}
StringBuilder sb_error=null;
StringBuilder sb_wrong=null;
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 321) {
if (Build.VERSION.SDK_INT >= 23) {
int index = 0;
sb_error = new StringBuilder();
for (int i = 0; i < permissions.length; i++) {
/*if (permissions[i] == "Manifest.permission.READ_EXTERNAL_STORAGE" || permissions[i] == "Manifest.permission.ACCESS_COARSE_LOCATION")
{continue;}*/
if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
if (shouldShowRequestPermissionRationale(permissions[i])) {
sb_error.append(content[i]);
sb_error.append(" ");
index++;
}
}
}
if (index != 0) {
setAuthority(sb_error.toString());
index = 0;
}
}
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 123) {
if (Build.VERSION.SDK_INT >= 23) {
sb_wrong = new StringBuilder();
int index = 0;
for (int i = 0; i < permissions.length; i++) {
/*if (permissions[i] == "Manifest.permission.READ_EXTERNAL_STORAGE" || permissions[i] == "Manifest.permission.ACCESS_COARSE_LOCATION")
{continue;}*/
int checkper = ContextCompat.checkSelfPermission(GuideActivity.this, permissions[i]);
{
if (checkper != PackageManager.PERMISSION_GRANTED){
sb_wrong.append(content[i]);
sb_wrong.append(" ");
index++;
}
}
}
if (index != 0) {
setAuthority(sb_wrong.toString());
index = 0;
} else {
Toast.makeText(this, "获取所有权限成功", Toast.LENGTH_SHORT).show();
}
}
}
}
public void setAuthority(String content) {
new AlertDialog.Builder(this)
.setTitle("还有权限未设置")
.setIcon(android.R.drawable.ic_dialog_info)
.setMessage(content)
.setPositiveButton("立即设置", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, 123);
}
}).setCancelable(false).show();
}

为TA充电
共{{data.count}}人
人已赞赏
Android文章

Android 气泡图片

2021-5-6 11:58:16

Android文章

Android 7.0升级软件出错

2021-5-6 12:24:57

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索