在BaseActivity定义如下方法
//检查是否有权限
public boolean hasPermission(String... permissions){
for (String permission:permissions){
if(ContextCompat.checkSelfPermission(this,permission)!= PackageManager.PERMISSION_GRANTED){
return false;
}
}
return true;
}
//请求权限
public void requestPermission(int code,String... permissions){
ActivityCompat.requestPermissions(this,permissions,code);
}
//请求结果
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode){
case Constants.WRITExxxx:
if(grantResults[0]==PackageManager.PERMISSION_GRANTED){
}else{//用户拒绝
}
doSDCardPersimission();
break;
case Constants.CAllxxxx
doCallphone();
}
}
//子类具体实现
public void doSDCardPersimission(){
}
//子类具体实现
public void doCallphone(){
}
具体的实现类中,这样使用
//调用打电话方法
private void callPhone(){
if(hasPermission(Manifest.permission.CALL_PHONE)){
doCallphone();
}else{
requestPermission(Constants.CAllxxxx,Manifest.permission.CALL_PHONE);
}
}
//真正的打电话操作
public void doCallphone(){
//调用系统打电话方法
}