Android 权限获取与权限大全

普通权限声明即可,危险权限需要动态获取

AndroidManifest中声明权限



















单个权限动态获取

if (ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.CALL_PHONE)!=PackageManager.PERMISSION_GRANTED){
    ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.CALL_PHONE},1);
}else {
    doSomething()
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode){
        case 1:
            if (grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
                doSomething();
            }else {
                Log.i(TAG, "onCreate: 请求被用户拒绝");
            }
    }
}

 

多个权限同时获取

//多个权限同时获取
List permissionList=new ArrayList<>();
if (ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.WRITE_EXTERNAL_STORAGE)!=PackageManager.PERMISSION_GRANTED){
    permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.READ_PHONE_STATE)!=PackageManager.PERMISSION_GRANTED){
    permissionList.add(Manifest.permission.READ_PHONE_STATE);
}
if (ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.ACCESS_COARSE_LOCATION)!=PackageManager.PERMISSION_GRANTED){
    permissionList.add(Manifest.permission.ACCESS_COARSE_LOCATION);
}
if (!permissionList.isEmpty()){
    String [] permissions=permissionList.toArray(new String [permissionList.size()]);
    ActivityCompat.requestPermissions(MainActivity.this,permissions,1);
}else{
    doSomething();
}
//多个权限同时获取
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode){
        case 1:
            if (grantResults.length>0){
                for (int result:grantResults){
                    if (result!=PackageManager.PERMISSION_GRANTED){
                        Toast.makeText(this,"请同意所以请求才能运行程序",Toast.LENGTH_SHORT).show();
                        finish();
                        return;
                    }
                    doSomething();
                }
            }else {
                Toast.makeText(this,"发生权限请求错误,程序关闭",Toast.LENGTH_SHORT).show();
                finish();
            }
            break;
        default:
    }
}

 

权限大全

https://blog.csdn.net/qq_31660173/article/details/51354277 

 

危险权限组

表 1. 危险权限和权限组。

权限组 权限
CALENDAR
  • READ_CALENDAR
  • WRITE_CALENDAR
CAMERA
  • CAMERA
CONTACTS
  • READ_CONTACTS
  • WRITE_CONTACTS
  • GET_ACCOUNTS
LOCATION
  • ACCESS_FINE_LOCATION
  • ACCESS_COARSE_LOCATION
MICROPHONE
  • RECORD_AUDIO
PHONE
  • READ_PHONE_STATE
  • CALL_PHONE
  • READ_CALL_LOG
  • WRITE_CALL_LOG
  • ADD_VOICEMAIL
  • USE_SIP
  • PROCESS_OUTGOING_CALLS
SENSORS
  • BODY_SENSORS
SMS
  • SEND_SMS
  • RECEIVE_SMS
  • READ_SMS
  • RECEIVE_WAP_PUSH
  • RECEIVE_MMS
STORAGE
  • READ_EXTERNAL_STORAGE
  • WRITE_EXTERNAL_STORAGE

你可能感兴趣的:(Android技术小点,Android概念)