Permission management of new features of android6.0

status quo:

1. When the compiled version of the code is 5.0 and below (API23 or below), the permissions are registered in the Manifest.

    There is no problem with mobile phones running below 6.0. The user can only choose to agree to the permission during installation, and cannot be modified (regardless of the factors set by third-party security software)
    Running on 6.0 and above, users with sensitive permissions can modify it in the settings (no third-party software is required). When modifying, the system will prompt the user that "it may cause it to fail to run normally", but the modification can still be successful. Once modified below API23, there is no way to open the permission through the API method inside the program. For non- sensitive permissions, it cannot be modified after installation.
 

2. When the compiled version of the code is 6.0 and above (API23 and above),the permissions are registered in Manifest.

    There is no problem with mobile phones running below 6.0. The user can only choose to agree to the permission during installation, and cannot be modified (regardless of third-party security software settings). Running on 6.0 and above , the permission can be obtained after installation for non- sensitive permissions. , and cannot be modified. For sensitive permissions, there is no permission by default after installation. The program can judge and prompt the user to grant these permissions.
 
Android6.0 APIs related to permissions
//Check if a permission has been granted, return PackageManager.PERMISSION_GRANTED or PERMISSION_DENIED
ContextCompat.checkSelfPermission()
//Request the user to grant a (some) permission
ActivityCompat.requestPermissions()
// The result of requesting permission from the user
OnRequestPermissionsResultCallback()
//A user-friendly method provided by Google, indicating whether to explain the permissions to the user
ActivityCompat.shouldShowRequestPermissionRationale()
 
Fragment similar

FragmentCompat.requestPermissions()
OnRequestPermissionsResultCallback()
FragmentCompat.shouldShowRequestPermissionRationale()
 
Example:
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView(R.layout.activity_main);

        readExtStorageDir();
    }

    /**
     * Read external SD card directory
     */
    private void readExtStorageDir() {
        int permissionResult = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
        if (PackageManager.PERMISSION_GRANTED == permissionResult) {//Have permission
            doReadExtStorageDir();
        } else {//No permission to ask the user for permission
            final String[] permissions = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE};
            ActivityCompat.requestPermissions(MainActivity.this, permissions, 100);
        }
    }

    /**
     * The result of requesting permission
     *
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (100 == requestCode) {
            if (null != permissions) {
// is the permission to read the external SD card & has been given permission
                if (Manifest.permission.READ_EXTERNAL_STORAGE.equals(permissions[0]) && PackageManager.PERMISSION_GRANTED == grantResults[0]) {
                    doReadExtStorageDir();
                }
            }
        }
    }

    private void doReadExtStorageDir() {
        File storageDir = Environment.getExternalStorageDirectory();
        File[] files = storageDir.listFiles();
        if (null != files && files.length > 0) {
            for (File f : files) {
                Log.e("P", "==>" + f.getName());
            }
        }
    }

}
 
Personalized Tips:
   When the user refuses, but there is no point to no longer prompt, then should the user be given a description of the permission requirements at this time? At this time, the shouldShowRequestPermissionRationale method is used
Understanding of shouldShowRequestPermissionRationale():
如果之前请求过并被用户拒绝,那么它会返回 true,如果之前用户已经勾选了不再提示的选项,它会返回 false,同样当设备策略禁止应用拥有这条权限时也会返回 false。第一次请求这个权限也返回false。那么我们需要做的,就是在用户确切需要拒绝这个权限请求前向他解释我们为什么需要,否则当彻底拒绝后(用户在拒绝的时候勾选了不再提示也返回false,但执行requestPermissions()方法不会显示请求权限框),除非用户手动去设置中开启,我们都无法再得到权限了。
/**
 * 读取外部SD卡目录
*/
private void readExtStorageDir() {
    int permissionResult = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
    if (PackageManager.PERMISSION_GRANTED == permissionResult) {
        doReadExtStorageDir();
    } else {
        final String[] permissions = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE};

        boolean shouldShowRequestPermission = ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE);
        Log.e("P", "shouldShowRequestPermission==" + shouldShowRequestPermission);
        if (shouldShowRequestPermission) {
            showMessageOKCancel("向用户解释权限,请用户点击允许!", new DialogInterface.OnClickListener() {

                @Override
public void onClick(DialogInterface dialogInterface, int i) {
                    ActivityCompat.requestPermissions(MainActivity.this, permissions, 100);
                }
            });
            return;
        }
        ActivityCompat.requestPermissions(MainActivity.this, permissions, 100);

    }
}
一次请求多个权限:
//定义请求多个权限的名称    
String[] permissions = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA};
//向用户请求赋予权限
ActivityCompat.requestPermissions(MainActivity.this,permissions,100);

//回调结果
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    Log.e("P", requestCode + " " + permissions[0] + " " + grantResults[0]);
    if (100 == requestCode) {
        if (null != permissions) {
            for (int i = 0; i < permissions.length; i++) {
                Log.e("P", permissions[i] + " ===> " + grantResults[i]);
                if (Manifest.permission.READ_EXTERNAL_STORAGE.equals(permissions[i])) {
                    if (PackageManager.PERMISSION_GRANTED == grantResults[i]) {
                        doReadExtStorageDir();
                    }
                }
            }
        }
    }
}
  
 
权限组 
 ActivityCompat.requestPermissions()不能传权限组名称,只能传权限名称,同一组的任何一个权限被授权了,其他权限也自动被授权。
敏感权限列表
权限组 权限
android.permission-group.CALENDAR(日历数据)
  • android.permission.READ_CALENDAR
  • android.permission.WRITE_CALENDAR
android.permission-group.CAMERA(相机)
  • android.permission.CAMERA
android.permission-group.CONTACTS(联系人)
  • android.permission.READ_CONTACTS
  • android.permission.WRITE_CONTACTS
  • android.permission.GET_ACCOUNTS
android.permission-group.LOCATION(位置)
  • android.permission.ACCESS_FINE_LOCATION
  • android.permission.ACCESS_COARSE_LOCATION
android.permission-group.MICROPHONE(麦克风)
  • android.permission.RECORD_AUDIO
android.permission-group.PHONE(电话)
  • android.permission.READ_PHONE_STATE
  • android.permission.CALL_PHONE
  • android.permission.READ_CALL_LOG
  • android.permission.WRITE_CALL_LOG
  • com.android.voicemail.permission.ADD_VOICEMAIL
  • android.permission.USE_SIP
  • android.permission.PROCESS_OUTGOING_CALLS
android.permission-group.SENSORS(传感器)
  • android.permission.BODY_SENSORS
android.permission-group.SMS(短信)
  • android.permission.SEND_SMS
  • android.permission.RECEIVE_SMS
  • android.permission.READ_SMS
  • android.permission.RECEIVE_WAP_PUSH
  • android.permission.RECEIVE_MMS
  • android.permission.READ_CELL_BROADCASTS
android.permission-group.STORAGE(存储)
  • android.permission.READ_EXTERNAL_STORAGE
  • android.permission.WRITE_EXTERNAL_STORAGE
 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326610281&siteId=291194637