Android动态权限申请

之前在做自己的项目的时候,由于没有动态申请权限,导致APP强制关闭了。Android 6.0(API 23)加入了动态权限,权限有普通权限和危险权限两种,其中危险权限在6.0以上的手机是需要动态添加权限的。

  • 普通权限,只需要在AndroidManifest.xml中申请即可
android.permission.ACCESS_LOCATION_EXTRA_COMMANDS
  android.permission.ACCESS_NETWORK_STATE
  android.permission.ACCESS_NOTIFICATION_POLICY
  android.permission.ACCESS_WIFI_STATE
  android.permission.ACCESS_WIMAX_STATE
  android.permission.BLUETOOTH
  android.permission.BLUETOOTH_ADMIN
  android.permission.BROADCAST_STICKY
  android.permission.CHANGE_NETWORK_STATE
  android.permission.CHANGE_WIFI_MULTICAST_STATE
  android.permission.CHANGE_WIFI_STATE
  android.permission.CHANGE_WIMAX_STATE
  android.permission.DISABLE_KEYGUARD
  android.permission.EXPAND_STATUS_BAR
  android.permission.FLASHLIGHT
  android.permission.GET_ACCOUNTS
  android.permission.GET_PACKAGE_SIZE
  android.permission.INTERNET
  android.permission.KILL_BACKGROUND_PROCESSES
  android.permission.MODIFY_AUDIO_SETTINGS
  android.permission.NFC
  android.permission.READ_SYNC_SETTINGS
  android.permission.READ_SYNC_STATS
  android.permission.RECEIVE_BOOT_COMPLETED
  android.permission.REORDER_TASKS
  android.permission.REQUEST_INSTALL_PACKAGES
  android.permission.SET_TIME_ZONE
  android.permission.SET_WALLPAPER
  android.permission.SET_WALLPAPER_HINTS
  android.permission.SUBSCRIBED_FEEDS_READ
  android.permission.TRANSMIT_IR
  android.permission.USE_FINGERPRINT
  android.permission.VIBRATE
  android.permission.WAKE_LOCK
  android.permission.WRITE_SYNC_SETTINGS
  com.android.alarm.permission.SET_ALARM
  com.android.launcher.permission.INSTALL_SHORTCUT
  com.android.launcher.permission.UNINSTALL_SHORTCUT
  • 危险权限,需单独申请的权限,共分为9组,每组只要有一个权限申请成功,默认整组权限都可以使用
group:android.permission-group.CONTACTS
    permission:android.permission.WRITE_CONTACTS
    permission:android.permission.GET_ACCOUNTS    
    permission:android.permission.READ_CONTACTS

  group:android.permission-group.PHONE
    permission:android.permission.READ_CALL_LOG
    permission:android.permission.READ_PHONE_STATE 
    permission:android.permission.CALL_PHONE
    permission:android.permission.WRITE_CALL_LOG
    permission:android.permission.USE_SIP
    permission:android.permission.PROCESS_OUTGOING_CALLS
    permission:com.android.voicemail.permission.ADD_VOICEMAIL

  group:android.permission-group.CALENDAR
    permission:android.permission.READ_CALENDAR
    permission:android.permission.WRITE_CALENDAR

  group:android.permission-group.CAMERA
    permission:android.permission.CAMERA

  group:android.permission-group.SENSORS
    permission:android.permission.BODY_SENSORS

  group:android.permission-group.LOCATION
    permission:android.permission.ACCESS_FINE_LOCATION
    permission:android.permission.ACCESS_COARSE_LOCATION

  group:android.permission-group.STORAGE
    permission:android.permission.READ_EXTERNAL_STORAGE
    permission:android.permission.WRITE_EXTERNAL_STORAGE

  group:android.permission-group.MICROPHONE
    permission:android.permission.RECORD_AUDIO

  group:android.permission-group.SMS
    permission:android.permission.READ_SMS
    permission:android.permission.RECEIVE_WAP_PUSH
    permission:android.permission.RECEIVE_MMS
    permission:android.permission.RECEIVE_SMS
    permission:android.permission.SEND_SMS
    permission:android.permission.READ_CELL_BROADCASTS

如果你暂时不支持动态权限申请,那么targetSdkVersion最大只能设置为22,不然就会像我一样在需要这些权限的时候应用强制关闭

申请步骤

  1. 先检查一下需要的权限是否已经被允许了,如果已经允许,就可以直接进行你的操作:
//比如我有一个按钮,需要录制音频,那么就需要动态申请权限了
button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //检查录音权限
                int permissionCheck=ContextCompat.checkSelfPermission(Record_activity.this,Manifest.permission.RECORD_AUDIO);
                //录音文件需保存,检查手机存储权限
                int permissionCheck2=ContextCompat.checkSelfPermission(Record_activity.this,Manifest.permission.WRITE_EXTERNAL_STORAGE);
                //PERMISSION_DENIED即没有权限
                   if(permissionCheck==PackageManager.PERMISSION_DENIED||permissionCheck2==PackageManager.PERMISSION_DENIED){
                //开始申请权限 数字0为requestCode,待会的请求完系统自动调用onRequestPermissionsResult() 方法函数需要用到
                    ActivityCompat.requestPermissions(Record_activity.this,new String[]{Manifest.permission.RECORD_AUDIO,Manifest.permission.WRITE_EXTERNAL_STORAGE},0);
                }else {
                /*权限已允许,开始录制音频*/
            }
            }
        });

请求完,之后,会调用onRequestPermissionsResult() 方法,需要我们在activity中重写

//无论权限申请成功与否,都会调用该方法
@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode){//这里的requestCode要与我们发起请求的requestCode一样
            case 0:
                if(grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED&&grantResults[1]==PackageManager.PERMISSION_GRANTED){//PERMISSION_GRANTED即为允许
                    Toast.makeText(Record_activity.this,"点击录制按钮,即可开始录制",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(Record_activity.this,"拒绝权限无法录音",Toast.LENGTH_SHORT).show();
                }
                break;

        }
    }

以上是关于Android 6.0的动态权限申请,还有很多不完整和不足之处,还望指点。

你可能感兴趣的:(Android)