两段代码获取Android 6.0运行时权限

1. Activity请求权限 (Android6.0 = TargetAPI 23)
 if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {//申请数据存储权限
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    WRITE_EXTERNAL_STORAGE_REQUEST_CODE);
        }
2. 请求回调(授权或者拒绝)
 @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == WRITE_EXTERNAL_STORAGE_REQUEST_CODE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission Granted 授权
            } else {
                // Permission Denied 拒绝
            }
        }
    }
3. Fragment请求权限
在Fragment中申请权限,不要使用ActivityCompat.requestPermissions, 
直接使用Fragment的requestPermissions方法,
否则会回调到Activity的onRequestPermissionsResult



4. 运行时权限
  1. READ_CALENDAR //日历
  2. WRITE_CALENDAR
  3. CAMERA //相机
  4. READ_CONTACTS //联系人
  5. WRITE_CONTACTS
  6. GET_ACCOUNTS
  7. ADD_VOICEMAIL
  8. ACCESS_FINE_LOCATION //定位
  9. ACCESS_COARSE_LOCATION
  10. PROCESS_OUTGOING_CALLS
  11. RECORD_AUDIO //录音
  12. READ_PHONE_STATE
  13. CALL_PHONE //打电话
  14. READ_CALL_LOG
  15. WRITE_CALL_LOG
  16. USE_SIP
  17. BODY_SENSORS
  18. SEND_SMS //短信
  19. RECEIVE_SMS
  20. READ_SMS
  21. RECEIVE_WAP_PUSH
  22. RECEIVE_MMS
  23. READ_EXTERNAL_STORAGE //存储
  24. WRITE_EXTERNAL_STORAGE

你可能感兴趣的:(两段代码获取Android 6.0运行时权限)