Android6.0動態(tài)權(quán)限申請步驟以及需要注意的一些坑

因為工作需要泥技,簡單研究了一下Android6.0權(quán)限申請,在Google提供的sample的基礎(chǔ)上磕仅,寫了一個簡單的demo珊豹。算是自己的筆記吧簸呈,可能會比較混亂,主要是方便以后查看店茶。后期有別的問題蜕便,隨時更新~

  • 本demo github下載地址!7坊谩轿腺!

  • Google提供的demo的下載地址

  • 6.0權(quán)限的基本知識,以下是需要單獨申請的權(quán)限丛楚,共分為9組族壳,每組只要有一個權(quán)限申請成功了,就默認整組權(quán)限都可以使用了趣些。

      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
    
  • 以下是普通權(quán)限仿荆,只需要在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
    

申請步驟

    1. 將targetSdkVersion設(shè)置為23喧务,注意赖歌,如果你將targetSdkVersion設(shè)置為>=23枉圃,則必須按照Android谷歌的要求功茴,動態(tài)的申請權(quán)限,如果你暫時不打算支持動態(tài)權(quán)限申請孽亲,則targetSdkVersion最大只能設(shè)置為22.
  • 2 在AndroidManifest.xml中申請你需要的權(quán)限坎穿,包括普通權(quán)限和需要申請的特殊權(quán)限。

  • 3.開始申請權(quán)限返劲,此處分為3部玲昧。

  • (1)檢查是否由此權(quán)限checkSelfPermission(),如果已經(jīng)開啟篮绿,則直接做你想做的孵延。

  • (2)如果未開啟,則判斷是否可以顯示用戶界面并說明請求權(quán)限的理由亲配。shouldShowRequestPermissionRationale尘应。

  • (3)如果可以顯示(即返回true),則可以彈出對話框提示用戶申請權(quán)限原因吼虎,用戶確認后申請權(quán)限r(nóng)equestPermissions()犬钢,如果不可以(即返回false),分為兩種情況思灰,一是從來沒有申請過玷犹,則直接申請權(quán)限r(nóng)equestPermissions(),一種是申請過被用戶拒絕了洒疚,并且不允許在彈對話框歹颓,則打開本應(yīng)用信息界面坯屿,由用戶自己手動開啟這個權(quán)限。
    (這里是一部門代碼巍扛,底部有比較完善的代碼愿伴,整個demo可以在github中下載)。

單個權(quán)限申請.png
     /**
         * 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隔节,
        // 但是,如果用戶關(guān)閉了你申請的權(quán)限(如下圖寂呛,在安裝的時候怎诫,將一些權(quán)限關(guān)閉了),ActivityCompat.checkSelfPermission()則可能會導(dǎo)致程序崩潰(java.lang.RuntimeException: Unknown exception code: 1 msg null)贷痪,
        // 你可以使用try{}catch(){},處理異常幻妓,也可以判斷系統(tǒng)版本,低于23就不申請權(quán)限劫拢,直接做你想做的肉津。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");
    
    
                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();
//得到權(quán)限的時候,就可以在回調(diào)里面做你想做的事情了
                permissionGrant.onPermissionGranted(requestCode);
            }
        }
6.0以下系統(tǒng)的應(yīng)用程序安裝界面.png

備注2詹住C蒙场!

(1)checkSelfPermission:檢查是否擁有這個權(quán)限
(2)requestPermissions:請求權(quán)限熟吏,一般會彈出一個系統(tǒng)對話框距糖,詢問用戶是否開啟這個權(quán)限。
(3)shouldShowRequestPermissionRationale:在允許詢問時返回true 牵寺; 在權(quán)限通過 或者權(quán)限被拒絕并且禁止詢問時返回false 悍引,如果從來沒有詢問過,也是返回的false帽氓, 所以單純的使用shouldShowRequestPermissionRationale去做什么判斷趣斤,是沒用的。黎休。浓领。 所以說這個地方有坑,我的解決方法是奋渔,在回調(diào)里面處理镊逝,如果用戶拒絕了這個權(quán)限,則打開本應(yīng)用信息界面嫉鲸,由用戶自己手動開啟這個權(quán)限撑蒜。
(4)每個應(yīng)用都有自己的權(quán)限管理界面,里面有本應(yīng)用申請的權(quán)限以及各種狀態(tài),即使用戶已經(jīng)同意了你申請的權(quán)限座菠,他也隨時可以關(guān)閉

權(quán)限管理界面.png

一次申請多個權(quán)限

其實和申請一個權(quán)限是一樣的狸眼,只是requestPermissions(final @NonNull Activity activity,
final @NonNull String[] permissions, final int requestCode),里面的permissions給的參數(shù)多些而已。

申請多個權(quán)限.png
 /**
     * 一次申請多個權(quán)限
     */
    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);
        }

    }
  • 關(guān)于權(quán)限請求結(jié)果的回調(diào)浴滴。Activity實現(xiàn)ActivityCompat.OnRequestPermissionsResultCallback接口拓萌,重寫onRequestPermissionsResult方法。

     @Override
      public void onRequestPermissionsResult(final int requestCode, @NonNull String[] permissions,
                                             @NonNull int[] grantResults) {
          PermissionUtils.requestPermissionsResult(this, requestCode, permissions, grantResults, mPermissionGrant);
    
      }
    

整個申請權(quán)限工具類代碼

package com.example.android.system.runtimepermissions;

import android.Manifest;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by qianxiaoai on 2016/7/7.
 */
public class PermissionUtils {

    private static final String TAG = PermissionUtils.class.getSimpleName();
    public static final int CODE_RECORD_AUDIO = 0;
    public static final int CODE_GET_ACCOUNTS = 1;
    public static final int CODE_READ_PHONE_STATE = 2;
    public static final int CODE_CALL_PHONE = 3;
    public static final int CODE_CAMERA = 4;
    public static final int CODE_ACCESS_FINE_LOCATION = 5;
    public static final int CODE_ACCESS_COARSE_LOCATION = 6;
    public static final int CODE_READ_EXTERNAL_STORAGE = 7;
    public static final int CODE_WRITE_EXTERNAL_STORAGE = 8;
    public static final int CODE_MULTI_PERMISSION = 100;

    public static final String PERMISSION_RECORD_AUDIO = Manifest.permission.RECORD_AUDIO;
    public static final String PERMISSION_GET_ACCOUNTS = Manifest.permission.GET_ACCOUNTS;
    public static final String PERMISSION_READ_PHONE_STATE = Manifest.permission.READ_PHONE_STATE;
    public static final String PERMISSION_CALL_PHONE = Manifest.permission.CALL_PHONE;
    public static final String PERMISSION_CAMERA = Manifest.permission.CAMERA;
    public static final String PERMISSION_ACCESS_FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION;
    public static final String PERMISSION_ACCESS_COARSE_LOCATION = Manifest.permission.ACCESS_COARSE_LOCATION;
    public static final String PERMISSION_READ_EXTERNAL_STORAGE = Manifest.permission.READ_EXTERNAL_STORAGE;
    public static final String PERMISSION_WRITE_EXTERNAL_STORAGE = Manifest.permission.WRITE_EXTERNAL_STORAGE;

    private static final String[] requestPermissions = {
            PERMISSION_RECORD_AUDIO,
            PERMISSION_GET_ACCOUNTS,
            PERMISSION_READ_PHONE_STATE,
            PERMISSION_CALL_PHONE,
            PERMISSION_CAMERA,
            PERMISSION_ACCESS_FINE_LOCATION,
            PERMISSION_ACCESS_COARSE_LOCATION,
            PERMISSION_READ_EXTERNAL_STORAGE,
            PERMISSION_WRITE_EXTERNAL_STORAGE
    };

    interface PermissionGrant {
        void onPermissionGranted(int requestCode);
    }

    /**
     * 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微王,
        // 但是,如果用戶關(guān)閉了你申請的權(quán)限品嚣,ActivityCompat.checkSelfPermission(),會導(dǎo)致程序崩潰(java.lang.RuntimeException: Unknown exception code: 1 msg null)炕倘,
        // 你可以使用try{}catch(){},處理異常,也可以在這個地方翰撑,低于23就什么都不做罩旋,
        // 個人建議try{}catch(){}單獨處理,提示用戶開啟權(quán)限眶诈。
//        if (Build.VERSION.SDK_INT < 23) {
//            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");


            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);
        }
    }

    private static void requestMultiResult(Activity activity, String[] permissions, int[] grantResults, PermissionGrant permissionGrant) {

        if (activity == null) {
            return;
        }

        //TODO
        Log.d(TAG, "onRequestPermissionsResult permissions length:" + permissions.length);
        Map<String, Integer> perms = new HashMap<>();

        ArrayList<String> notGranted = new ArrayList<>();
        for (int i = 0; i < permissions.length; i++) {
            Log.d(TAG, "permissions: [i]:" + i + ", permissions[i]" + permissions[i] + ",grantResults[i]:" + grantResults[i]);
            perms.put(permissions[i], grantResults[i]);
            if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                notGranted.add(permissions[i]);
            }
        }

        if (notGranted.size() == 0) {
            Toast.makeText(activity, "all permission success" + notGranted, Toast.LENGTH_SHORT)
                    .show();
            permissionGrant.onPermissionGranted(CODE_MULTI_PERMISSION);
        } else {
            openSettingActivity(activity, "those permission need granted!");
        }

    }


    /**
     * 一次申請多個權(quán)限
     */
    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);
        }

    }


    private static void shouldShowRationale(final Activity activity, final int requestCode, final String requestPermission) {
        //TODO
        String[] permissionsHint = activity.getResources().getStringArray(R.array.permissions);
        showMessageOKCancel(activity, "Rationale: " + permissionsHint[requestCode], new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ActivityCompat.requestPermissions(activity,
                        new String[]{requestPermission},
                        requestCode);
                Log.d(TAG, "showMessageOKCancel requestPermissions:" + requestPermission);
            }
        });
    }

    private static void showMessageOKCancel(final Activity context, String message, DialogInterface.OnClickListener okListener) {
        new AlertDialog.Builder(context)
                .setMessage(message)
                .setPositiveButton("OK", okListener)
                .setNegativeButton("Cancel", null)
                .create()
                .show();

    }

    /**
     * @param activity
     * @param requestCode  Need consistent with requestPermission
     * @param permissions
     * @param grantResults
     */
    public static void requestPermissionsResult(final Activity activity, final int requestCode, @NonNull String[] permissions,
                                                @NonNull int[] grantResults, PermissionGrant permissionGrant) {

        if (activity == null) {
            return;
        }
        Log.d(TAG, "requestPermissionsResult requestCode:" + requestCode);

        if (requestCode == CODE_MULTI_PERMISSION) {
            requestMultiResult(activity, permissions, grantResults, permissionGrant);
            return;
        }

        if (requestCode < 0 || requestCode >= requestPermissions.length) {
            Log.w(TAG, "requestPermissionsResult illegal requestCode:" + requestCode);
            Toast.makeText(activity, "illegal requestCode:" + requestCode, Toast.LENGTH_SHORT).show();
            return;
        }

        Log.i(TAG, "onRequestPermissionsResult requestCode:" + requestCode + ",permissions:" + permissions.toString()
                + ",grantResults:" + grantResults.toString() + ",length:" + grantResults.length);

        if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Log.i(TAG, "onRequestPermissionsResult PERMISSION_GRANTED");
            //TODO success, do something, can use callback
            permissionGrant.onPermissionGranted(requestCode);

        } else {
            //TODO hint user this permission function
            Log.i(TAG, "onRequestPermissionsResult PERMISSION NOT GRANTED");
            //TODO
            String[] permissionsHint = activity.getResources().getStringArray(R.array.permissions);
            openSettingActivity(activity,  "Result" + permissionsHint[requestCode]);
        }

    }

    private static void openSettingActivity(final Activity activity, String message) {

        showMessageOKCancel(activity, message, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                Log.d(TAG, "getPackageName(): " + activity.getPackageName());
                Uri uri = Uri.fromParts("package", activity.getPackageName(), null);
                intent.setData(uri);
                activity.startActivity(intent);
            }
        });
    }


    /**
     * @param activity
     * @param isShouldRationale true: return no granted and shouldShowRequestPermissionRationale permissions, false:return no granted and !shouldShowRequestPermissionRationale
     * @return
     */
    public static ArrayList<String> getNoGrantedPermission(Activity activity, boolean isShouldRationale) {

        ArrayList<String> permissions = new ArrayList<>();

        for (int i = 0; i < requestPermissions.length; i++) {
            String requestPermission = requestPermissions[i];


            //TODO checkSelfPermission
            int checkSelfPermission = -1;
            try {
                checkSelfPermission = ActivityCompat.checkSelfPermission(activity, requestPermission);
            } catch (RuntimeException e) {
                Toast.makeText(activity, "please open those permission", Toast.LENGTH_SHORT)
                        .show();
                Log.e(TAG, "RuntimeException:" + e.getMessage());
                return null;
            }

            if (checkSelfPermission != PackageManager.PERMISSION_GRANTED) {
                Log.i(TAG, "getNoGrantedPermission ActivityCompat.checkSelfPermission != PackageManager.PERMISSION_GRANTED:" + requestPermission);

                if (ActivityCompat.shouldShowRequestPermissionRationale(activity, requestPermission)) {
                    Log.d(TAG, "shouldShowRequestPermissionRationale if");
                    if (isShouldRationale) {
                        permissions.add(requestPermission);
                    }

                } else {

                    if (!isShouldRationale) {
                        permissions.add(requestPermission);
                    }
                    Log.d(TAG, "shouldShowRequestPermissionRationale else");
                }

            }
        }

        return permissions;
    }

}

界面調(diào)用代碼

package com.example.android.system.runtimepermissions;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Toast;

import com.example.android.common.logger.Log;

/**
 * Created by qianxiaoai on 2016/7/8.
 */
public class PermissionActivity extends FragmentActivity implements ActivityCompat.OnRequestPermissionsResultCallback{
    private static final String TAG = PermissionActivity.class.getSimpleName();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permission);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        PermissionsFragment fragment = new PermissionsFragment();
        transaction.replace(R.id.content_fragment, fragment);
        transaction.commit();

    }

    /**
     * Called when the 'show camera' button is clicked.
     * Callback is defined in resource layout definition.
     */
    public void showCamera(View view) {
        Log.i(TAG, "Show camera button pressed. Checking permission.");
        PermissionUtils.requestPermission(this, PermissionUtils.CODE_CAMERA, mPermissionGrant);
    }

    public void getAccounts(View view) {
        PermissionUtils.requestPermission(this, PermissionUtils.CODE_GET_ACCOUNTS, mPermissionGrant);
    }

    public void callPhone(View view) {
        PermissionUtils.requestPermission(this, PermissionUtils.CODE_CALL_PHONE, mPermissionGrant);
    }

    public void readPhoneState(View view) {
        PermissionUtils.requestPermission(this, PermissionUtils.CODE_READ_PHONE_STATE, mPermissionGrant);
    }

    public void accessFineLocation(View view) {
        PermissionUtils.requestPermission(this, PermissionUtils.CODE_ACCESS_FINE_LOCATION, mPermissionGrant);
    }

    public void accessCoarseLocation(View view) {
        PermissionUtils.requestPermission(this, PermissionUtils.CODE_ACCESS_COARSE_LOCATION, mPermissionGrant);
    }

    public void readExternalStorage(View view) {
        PermissionUtils.requestPermission(this, PermissionUtils.CODE_READ_EXTERNAL_STORAGE, mPermissionGrant);
    }

    public void writeExternalStorage(View view) {
        PermissionUtils.requestPermission(this, PermissionUtils.CODE_WRITE_EXTERNAL_STORAGE, mPermissionGrant);
    }

    public void recordAudio(View view) {
        PermissionUtils.requestPermission(this, PermissionUtils.CODE_RECORD_AUDIO, mPermissionGrant);
    }


    private PermissionUtils.PermissionGrant mPermissionGrant = new PermissionUtils.PermissionGrant() {
        @Override
        public void onPermissionGranted(int requestCode) {
            switch (requestCode) {
                case PermissionUtils.CODE_RECORD_AUDIO:
                    Toast.makeText(PermissionActivity.this, "Result Permission Grant CODE_RECORD_AUDIO", Toast.LENGTH_SHORT).show();
                    break;
                case PermissionUtils.CODE_GET_ACCOUNTS:
                    Toast.makeText(PermissionActivity.this, "Result Permission Grant CODE_GET_ACCOUNTS", Toast.LENGTH_SHORT).show();
                    break;
                case PermissionUtils.CODE_READ_PHONE_STATE:
                    Toast.makeText(PermissionActivity.this, "Result Permission Grant CODE_READ_PHONE_STATE", Toast.LENGTH_SHORT).show();
                    break;
                case PermissionUtils.CODE_CALL_PHONE:
                    Toast.makeText(PermissionActivity.this, "Result Permission Grant CODE_CALL_PHONE", Toast.LENGTH_SHORT).show();
                    break;
                case PermissionUtils.CODE_CAMERA:
                    Toast.makeText(PermissionActivity.this, "Result Permission Grant CODE_CAMERA", Toast.LENGTH_SHORT).show();
                    break;
                case PermissionUtils.CODE_ACCESS_FINE_LOCATION:
                    Toast.makeText(PermissionActivity.this, "Result Permission Grant CODE_ACCESS_FINE_LOCATION", Toast.LENGTH_SHORT).show();
                    break;
                case PermissionUtils.CODE_ACCESS_COARSE_LOCATION:
                    Toast.makeText(PermissionActivity.this, "Result Permission Grant CODE_ACCESS_COARSE_LOCATION", Toast.LENGTH_SHORT).show();
                    break;
                case PermissionUtils.CODE_READ_EXTERNAL_STORAGE:
                    Toast.makeText(PermissionActivity.this, "Result Permission Grant CODE_READ_EXTERNAL_STORAGE", Toast.LENGTH_SHORT).show();
                    break;
                case PermissionUtils.CODE_WRITE_EXTERNAL_STORAGE:
                    Toast.makeText(PermissionActivity.this, "Result Permission Grant CODE_WRITE_EXTERNAL_STORAGE", Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
            }
        }
    };

    /**
     * Callback received when a permissions request has been completed.
     */
    @Override
    public void onRequestPermissionsResult(final int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        PermissionUtils.requestPermissionsResult(this, requestCode, permissions, grantResults, mPermissionGrant);
    }
}

xml布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:paddingLeft="@dimen/horizontal_page_margin"
              android:paddingRight="@dimen/horizontal_page_margin"
              android:paddingTop="@dimen/vertical_page_margin"
              android:paddingBottom="@dimen/vertical_page_margin"
              android:orientation="vertical"
    >

    <FrameLayout
        android:id="@+id/content_fragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Camera"
                    android:id="@+id/button_camera"
                    android:onClick="showCamera"/>

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="RECORD_AUDIO"
                    android:onClick="recordAudio"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="GET_ACCOUNTS"
                    android:onClick="getAccounts"/>

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="CALL_PHONE"
                    android:onClick="callPhone"/>
            </LinearLayout>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="PERMISSION_READ_PHONE_STATE"
                android:onClick="readPhoneState"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ACCESS_FINE_LOCATION"
                android:onClick="accessFineLocation"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ACCESS_COARSE_LOCATION"
                android:onClick="accessCoarseLocation"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="READ_EXTERNAL_STORAGE"
                android:onClick="readExternalStorage"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="WRITE_EXTERNAL_STORAGE"
                android:onClick="writeExternalStorage"/>

        </LinearLayout>
    </ScrollView>

</LinearLayout>

清單文件申請的權(quán)限

  <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.CALL_PHONE"/>
    <uses-permission android:name="android.permission.SEND_SMS"/>
    <uses-permission android:name="android.permission.READ_SMS"/>

    <uses-permission android:name="android.permission.GET_ACCOUNTS"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>

部分 資源文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="permissions">
        <item>@string/permission_recode_audio_hint</item>
        <item>@string/permission_get_accounts_hint</item>
        <item>@string/permission_read_phone_hint</item>
        <item>@string/permission_call_phone_hint</item>
        <item>@string/permission_camera_hint</item>
        <item>@string/permission_access_fine_location_hint</item>
        <item>@string/permission_access_coarse_location_hint</item>
        <item>@string/permission_read_external_hint</item>
        <item>@string/permission_white_external_hint</item>
    </string-array>
</resources>

 <string name="permission_get_accounts_hint">沒有此權(quán)限涨醋,無法開啟這個功能,請開啟權(quán)限逝撬。PERMISSION_GET_ACCOUNTS</string>
    <string name="permission_read_phone_hint">沒有此權(quán)限浴骂,無法開啟這個功能,請開啟權(quán)限球拦。PERMISSION_READ_PHONE_STATE</string>
    <string name="permission_call_phone_hint">沒有此權(quán)限靠闭,無法開啟這個功能,請開啟權(quán)限坎炼。PERMISSION_CALL_PHONE</string>
    <string name="permission_camera_hint">沒有此權(quán)限,無法開啟這個功能拦键,請開啟權(quán)限谣光。PERMISSION_CAMERA</string>
    <string name="permission_access_fine_location_hint">沒有此權(quán)限,無法開啟這個功能萄金,請開啟權(quán)限媚朦。PERMISSION_ACCESS_FINE_LOCATION</string>
    <string name="permission_access_coarse_location_hint">沒有此權(quán)限,無法開啟這個功能询张,請開啟權(quán)限孙乖。PERMISSION_ACCESS_COARSE_LOCATION</string>
    <string name="permission_read_external_hint">沒有此權(quán)限,無法開啟這個功能唯袄,請開啟權(quán)限。PERMISSION_READ_EXTERNAL_STORAGE</string>
    <string name="permission_white_external_hint">沒有此權(quán)限恋拷,無法開啟這個功能资厉,請開啟權(quán)限。PERMISSION_WRITE_EXTERNAL_STORAGE</string>
    <string name="permission_recode_audio_hint">沒有此權(quán)限蔬顾,無法開啟這個功能,請開啟權(quán)限酪我。PERMISSION_RECORD_AUDIO</string>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末且叁,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子欺矫,更是在濱河造成了極大的恐慌,老刑警劉巖穆趴,帶你破解...
    沈念sama閱讀 206,126評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件遇汞,死亡現(xiàn)場離奇詭異,居然都是意外死亡空入,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,254評論 2 382
  • 文/潘曉璐 我一進店門化戳,熙熙樓的掌柜王于貴愁眉苦臉地迎上來埋凯,“玉大人,你說我怎么就攤上這事掠廓。” “怎么了?”我有些...
    開封第一講書人閱讀 152,445評論 0 341
  • 文/不壞的土叔 我叫張陵,是天一觀的道長依鸥。 經(jīng)常有香客問我黄橘,道長,這世上最難降的妖魔是什么塞关? 我笑而不...
    開封第一講書人閱讀 55,185評論 1 278
  • 正文 為了忘掉前任帆赢,我火速辦了婚禮,結(jié)果婚禮上怠益,老公的妹妹穿的比我還像新娘瘾婿。我一直安慰自己,他們只是感情好抢呆,可當我...
    茶點故事閱讀 64,178評論 5 371
  • 文/花漫 我一把揭開白布笛谦。 她就那樣靜靜地躺著,像睡著了一般恳邀。 火紅的嫁衣襯著肌膚如雪灶轰。 梳的紋絲不亂的頭發(fā)上框往,一...
    開封第一講書人閱讀 48,970評論 1 284
  • 那天,我揣著相機與錄音,去河邊找鬼瓤鼻。 笑死,一個胖子當著我的面吹牛清焕,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播滚停,決...
    沈念sama閱讀 38,276評論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼粥惧,長吁一口氣:“原來是場噩夢啊……” “哼突雪!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起咏删,我...
    開封第一講書人閱讀 36,927評論 0 259
  • 序言:老撾萬榮一對情侶失蹤督函,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后锋叨,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體搓译,經(jīng)...
    沈念sama閱讀 43,400評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 35,883評論 2 323
  • 正文 我和宋清朗相戀三年豌鸡,在試婚紗的時候發(fā)現(xiàn)自己被綠了段标。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 37,997評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡蛇更,死狀恐怖赛糟,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情璧南,我是刑警寧澤司倚,帶...
    沈念sama閱讀 33,646評論 4 322
  • 正文 年R本政府宣布篓像,位于F島的核電站皿伺,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏奠滑。R本人自食惡果不足惜脂男,卻給世界環(huán)境...
    茶點故事閱讀 39,213評論 3 307
  • 文/蒙蒙 一宰翅、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧汁讼,春花似錦嘿架、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,204評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至南缓,卻和暖如春荧呐,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背倍阐。 一陣腳步聲響...
    開封第一講書人閱讀 31,423評論 1 260
  • 我被黑心中介騙來泰國打工峰搪, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人楣颠。 一個月前我還...
    沈念sama閱讀 45,423評論 2 352
  • 正文 我出身青樓咐蚯,卻偏偏與公主長得像,于是被迫代替她去往敵國和親矫膨。 傳聞我的和親對象是個殘疾皇子期奔,可洞房花燭夜當晚...
    茶點故事閱讀 42,722評論 2 345

推薦閱讀更多精彩內(nèi)容