Handling of Android 6.0 runtime permission issues

You may be familiar with the android runtime permissions, but the permission problem has always been a very important factor affecting the user experience. It is impossible for you to prompt the customer to authorize and it is impossible to give up the permission to change, so in general, it is very confusing for developers. This is an idea I have summarized for your reference
. First , let’s talk about permissions. For us, there are two types of permissions. One is that you can use it directly by applying for it (also called normal permissions). The second is that you need user authorization before you can use it (also It is called dangerous permission) To put it bluntly, it means that as long as the user's privacy is involved, the user's authorization is required (the android 6.0 runtime permission will only start after ApI 23). The
normal permission only needs the user's consent during installation.

ACCESS_LOCATION_EXTRA_COMMANDS
ACCESS_NETWORK_STATE
ACCESS_NOTIFICATION_POLICY
ACCESS_WIFI_STATE
BLUETOOTH
BLUETOOTH_ADMIN
BROADCAST_STICKY
CHANGE_NETWORK_STATE
CHANGE_WIFI_MULTICAST_STATE
CHANGE_WIFI_STATE
DISABLE_KEYGUARD
EXPAND_STATUS_BAR
GET_PACKAGE_SIZE
INSTALL_SHORTCUT
INTERNET
KILL_BACKGROUND_PROCESSES
MODIFY_AUDIO_SETTINGS
NFC
READ_SYNC_SETTINGS
READ_SYNC_STATS
RECEIVE_BOOT_COMPLETED
REORDER_TASKS
REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
REQUEST_INSTALL_PACKAGES
SET_ALARM
SET_TIME_ZONE
SET_WALLPAPER
SET_WALLPAPER_HINTS
TRANSMIT_IR
UNINSTALL_SHORTCUT
USE_FINGERPRINT
VIBRATE
WAKE_LOCK
WRITE_SYNC_SETTINGS

Dangerous permissions require client runtime authorization

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

Let’s briefly talk about the idea of
​​permission processing (take location information as an example)
(first determine whether to enable the location service, if it is not enabled, use the intent call to set this type of permission is something you must have. For example, users of AutoNavi map navigation must enable GPS positioning. )

The first check permissions provide all the permissions you want to retrieve whether you already have permissions
ContextCompat.checkSelfPermission(context {permission})

The second is to prompt the user why I need this permission and check whether the user has handled this permission. One is to refuse to return ture. The other is to Don't ask again (that is, no more prompts or the user manually prohibits the permission in the settings) Return false
* Note : When the customer refuses the permission and chooses not to prompt, you can only ask the customer to manually add and change the permission through the intent prompt or toast prompt the customer to manually enable
ActivityCompat.shouldShowRequestPermissionRationale(activity,{permission})

Third , apply for user authorization
ActivityCompat.requestPermissions(activity,{permission})

  • Fourth application permission callback

    • requestCode application code
    • permissions array of permissions
    • Does grantResults correspond to the status of the permission array? OnRequestPermissionsResult
      (int requestCode, String[] permissions, int[] grantResults)

    • PackageManager.PERMISSION_GRANTED already has the permission

    • PackageManager.PERMISSION_DENIED not owned
所以综上所述 具体流程是
     * if(checkSelfPermission){// 检查是否拥有该权限
     *     // 进行业务操作
     * }else{
     *     if(shouldShowRequestPermissionRationale){// 检查用户是否处理 返回true 代表用户已经拒绝
     *          requestPermissions // 去申请
     *     }else{ 返回false
     *          toast or Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,uri)
     *          看该权限对你app 的重要程度
     *     }
     * }
     * // 申请回调
     * onRequestPermissionsResult
     * if(grantResults[0]==PackageManager.PERMISSION_GRANTED){
     *     //执行业务
     * }else{
     *     requestPermissions // 去申请  或者 将以上流程再走一边
     * }

code show as below

 private void showContacts() {
        LocationManager service = (LocationManager) getSystemService(this.LOCATION_SERVICE);
        boolean enabled = service.isProviderEnabled(service.GPS_PROVIDER);
        if (enabled){// 已开启定位
            // 未开启权限
            if (ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED||ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION)!=PackageManager.PERMISSION_GRANTED){
                if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.ACCESS_FINE_LOCATION)){
                    ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1000);
                }else {
                    Uri uri =Uri.parse("package:"+getPackageName());
                    Intent in = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,uri);
                    startActivity(in);
                }
            }else {// 已开启权限
                // 获取位置信息
            }

        }else {// 未开启定位
            Toast.makeText(this, "系统检测到未开启GPS定位服务", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent();
            intent.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivityForResult(intent, 1315);

        }
    }

    //Android6.0申请权限的回调方法
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode){
            case 1000:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    //获取位置信息
                }else {
                    showContacts();
                }
                break;
        }
    }

With the attitude of teaching people how to fish

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325539572&siteId=291194637