1.单个权限申请
(1)第一种方式
public class MainActivity extends AppCompatActivity {
String writePermission = Manifest.permission.WRITE_EXTERNAL_STORAGE;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
* shouldShowRequestPermissionRationale
* 1. 在允许询问时返回true
* 2,在权限通过 或者权限被拒绝并且禁止询问时返回false 但是有一个例外,就是重来没有询问过的时候,也是返回的false 所以单纯的使用shouldShowRequestPermissionRationale去做什么判断,是没用的,只能在请求权限回调后再使用
*Google的原意是: 1,没有申请过权限,申请就是了,所以返回false; 2,申请了用户拒绝了,那你就要提示用户了,所以返回true; 3,用户选择了拒绝并且不再提示,那你也不要申请了,也不要提示用户了,所以返回false; 4,已经允许了,不需要申请也不需要提示,所以返回false
*/
int i = checkSelfPermission(writePermission);
if (i == PackageManager.PERMISSION_GRANTED) {
Log.e("tag_", "权限同意");
} else if (i == PackageManager.PERMISSION_DENIED) {
Log.e("tag_", "权限拒绝,开始申请");
requestPermissions(new String[]{writePermission}, 55);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 55) {
boolean equals = permissions[0].equals(writePermission);
if (equals) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.e("tag_", "权限申请,同意");
} else if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
boolean b = shouldShowRequestPermissionRationale(writePermission);
if (b) {
Log.e("tag_", "权限申请,拒绝,下次还能申请");
} else {
Log.e("tag_", "权限申请,拒绝,下次不能申请了");
}
}
}
}
}
}
(2)第二种方式
public class SecondActivity extends AppCompatActivity {
String phonePermission = Manifest.permission.WRITE_EXTERNAL_STORAGE;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int i = checkSelfPermission(phonePermission);
if (i == PackageManager.PERMISSION_GRANTED) {
Log.e("tag_", "权限同意");
} else if (i == PackageManager.PERMISSION_DENIED) {
Log.e("tag_", "权限拒绝,开始申请");
registerForActivityResult(new ActivityResultContracts.RequestPermission(), new ActivityResultCallback<Boolean>() {
@Override
public void onActivityResult(Boolean result) {
if (result) {
Log.e("tag_", "权限申请,同意");
} else {
boolean b = shouldShowRequestPermissionRationale(phonePermission);
if (b) {
Log.e("tag_", "权限申请,拒绝,下次还能申请");
} else {
Log.e("tag_", "权限申请,拒绝,下次不能申请了");
}
}
}
}).launch(phonePermission);
}
}
}
2.多个权限申请
(1)第一种方式
public class MutiActivity extends AppCompatActivity {
String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.CAMERA};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<String> noGrantedList = new ArrayList<>();
for (String permission : permissions) {
int checkSelfPermission = checkSelfPermission(permission);
if (checkSelfPermission == PackageManager.PERMISSION_GRANTED) {
} else if (checkSelfPermission == PackageManager.PERMISSION_DENIED) {
noGrantedList.add(permission);
}
}
if (!noGrantedList.isEmpty()) {
//注意:里边可能包含不能请求的权限
Log.e("tag_没有申请的权限", noGrantedList.toString());
String[] permissions = new String[noGrantedList.size()];
noGrantedList.toArray(permissions);
requestPermissions(permissions,66);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode==66){
for (int i = 0; i < permissions.length; i++) {
String permission = permissions[i];
int grantResult = grantResults[i];
if (grantResult == PackageManager.PERMISSION_GRANTED) {
Log.e("tag_权限申请同意",permission);
} else if (grantResult == PackageManager.PERMISSION_DENIED) {
boolean b = shouldShowRequestPermissionRationale(permission);
if(b){
Log.e("tag_权限申请拒绝——还可以申请",permission);
}else {
Log.e("tag_权限申请拒绝--不可以申请",permission);
}
}
}
}
}
}
(2)第二种方式
public class SecondMutiActivity extends AppCompatActivity {
String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.CAMERA};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<String> noGrantedList = new ArrayList<>();
for (String permission : permissions) {
int checkSelfPermission = checkSelfPermission(permission);
if (checkSelfPermission == PackageManager.PERMISSION_GRANTED) {
} else if (checkSelfPermission == PackageManager.PERMISSION_DENIED) {
noGrantedList.add(permission);
}
}
if (!noGrantedList.isEmpty()) {
//注意:里边可能包含不能请求的权限
Log.e("tag_没有申请的权限", noGrantedList.toString());
String[] permissions = new String[noGrantedList.size()];
noGrantedList.toArray(permissions);
registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), new ActivityResultCallback<Map<String, Boolean>>() {
@Override
public void onActivityResult(Map<String, Boolean> result) {
for (Map.Entry<String, Boolean> entry:result.entrySet()){
String key = entry.getKey();
Boolean value = entry.getValue();
if (value) {
Log.e("tag_权限申请同意",key);
} else {
boolean b = shouldShowRequestPermissionRationale(key);
if(b){
Log.e("tag_权限申请拒绝——还可以申请",key);
}else {
Log.e("tag_权限申请拒绝--不可以申请",key);
}
}
}
}
}).launch(permissions);
}
}
}
|