Android6.0动态权限申请

  • 6.0权力的基本知识,以下是急需单独申请的权能,共分为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
    
  • 以下是平凡权限,只需要在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

一回提请五个权力

实际和报名一个权力是均等的,只是requestPermissions(final @NonNull
Activity activity,
final @NonNull String[] permissions, final int
requestCode),里面的permissions给的参数多些而已。

 /**
     * 一次申请多个权限
     */
    public static void requestMultiPermissions(final Activity activity, PermissionGrant grant) {

        final List<String> permissionsList = getNoGrantedPermission(activity, false);
        final List<String> shouldRationalePermissionsList = getNoGrantedPermission(activity, true);

        //TODO checkSelfPermission
        if (permissionsList == null || shouldRationalePermissionsList == null) {
            return;
        }
        Log.d(TAG, "requestMultiPermissions permissionsList:" + permissionsList.size() + ",shouldRationalePermissionsList:" + shouldRationalePermissionsList.size());

        if (permissionsList.size() > 0) {
            ActivityCompat.requestPermissions(activity, permissionsList.toArray(new String[permissionsList.size()]),
                    CODE_MULTI_PERMISSION);
            Log.d(TAG, "showMessageOKCancel requestPermissions");

        } else if (shouldRationalePermissionsList.size() > 0) {
            showMessageOKCancel(activity, "should open those permission",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            ActivityCompat.requestPermissions(activity, shouldRationalePermissionsList.toArray(new String[shouldRationalePermissionsList.size()]),
                                    CODE_MULTI_PERMISSION);
                            Log.d(TAG, "showMessageOKCancel requestPermissions");
                        }
                    });
        } else {
            grant.onPermissionGranted(CODE_MULTI_PERMISSION);
        }

    }

 

 

 

                if (ActivityCompat.shouldShowRequestPermissionRationale(activity, requestPermission)) {
                    Log.i(TAG, "requestPermission shouldShowRequestPermissionRationale");
                    shouldShowRationale(activity, requestCode, requestPermission);

                } else {
                    Log.d(TAG, "requestCameraPermission else");
                    ActivityCompat.requestPermissions(activity, new String[]{requestPermission}, requestCode);
                }

            } else {
                Log.d(TAG, "ActivityCompat.checkSelfPermission ==== PackageManager.PERMISSION_GRANTED");
                Toast.makeText(activity, "opened:" + requestPermissions[requestCode], Toast.LENGTH_SHORT).show();
//得到权限的时候,就可以在回调里面做你想做的事情了
                permissionGrant.onPermissionGranted(requestCode);
            }
        }
  • (1)检查是不是经过权限checkSelfPermission(),假若已经开启,则直接做你想做的。

  • (2)假诺未打开,则判断是否需要向用户解释为何申请权限shouldShowRequestPermissionRationale。

  • (3)假诺需要(即重回true),则可以弹出对话框提醒用户申请权限原因,用户确认后申请权限requestPermissions(),假设不需要(即再次回到false),则一贯申请权限requestPermissions()。
    (这里是一机关代码,底部有相比完善的代码,整个demo可以在github中下载)。

    /**

         * Requests permission.
         *
         * @param activity
         * @param requestCode request code, e.g. if you need request CAMERA permission,parameters is PermissionUtils.CODE_CAMERA
         */
        public static void requestPermission(final Activity activity, final int requestCode, PermissionGrant permissionGrant) {
            if (activity == null) {
                return;
            }
    
            Log.i(TAG, "requestPermission requestCode:" + requestCode);
            if (requestCode < 0 || requestCode >= requestPermissions.length) {
                Log.w(TAG, "requestPermission illegal requestCode:" + requestCode);
                return;
            }
    
            final String requestPermission = requestPermissions[requestCode];
    
            //如果是6.0以下的手机,ActivityCompat.checkSelfPermission()会始终等于PERMISSION_GRANTED,
        // 但是,如果用户关闭了你申请的权限(如下图,在安装的时候,将一些权限关闭了),ActivityCompat.checkSelfPermission()则可能会导致程序崩溃(java.lang.RuntimeException: Unknown exception code: 1 msg null),
        // 你可以使用try{}catch(){},处理异常,也可以判断系统版本,低于23就不申请权限,直接做你想做的。permissionGrant.onPermissionGranted(requestCode);
    

    // if (Build.VERSION.SDK_INT < 23) { // permissionGrant.onPermissionGranted(requestCode); // return; // }

            int checkSelfPermission;
            try {
                checkSelfPermission = ActivityCompat.checkSelfPermission(activity, requestPermission);
            } catch (RuntimeException e) {
                Toast.makeText(activity, "please open this permission", Toast.LENGTH_SHORT)
                        .show();
                Log.e(TAG, "RuntimeException:" + e.getMessage());
                return;
            }
    
            if (checkSelfPermission != PackageManager.PERMISSION_GRANTED) {
                Log.i(TAG, "ActivityCompat.checkSelfPermission != PackageManager.PERMISSION_GRANTED");
    

 

 

goggle在Android6.0要求有些权力需要动态申请,直接下载AndroidManifest.xml中没用

 

 

  1. 将targetSdkVersion设置为23,注意,假若你将targetSdkVersion设置为>=23,则必须按照AndroidGoogle的渴求,动态的申请权限,假设你暂时不打算帮忙动态权限申请,则targetSdkVersion最大不得不设置为22
  2.  在AndroidManifest.xml中申请你需要的权杖,包括常见权限和需要申请的出格权限。

最先报名权限,此处分为3部。

相关文章