Android 6.0运行时权限处理

运行时权限介绍

Android 6.0在原有的AndroidManifest.xml声明权限的基础上增加了动态申请运行时权限。新增的运行时权限有 身体传感器日历摄像头通讯录地理位置麦克风电话短信SD卡存储

运行时权限处理方式

Android targetSdkVersion在23以下(不包括23),不需要动态申请运行时权限,所以可以通过降低targetSdkVersion的方式可以解决运行时权限申请的问题。这种方式不推荐。还有一种方式就是通过动态申请的方式解决。Android 6.0新增了一套关于权限申请的新的API。checkSelfPermission(...)requestPermissions(...)来进行动态申请权限。在onRequestPermissionsResult()进行回调处理权限申请的结果。

代码示例如下:

package com.syz.example.permission;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;

/**
 * Created by SYZ on 2016/12/30.
 */

public class BaseActivity extends Activity {

    /**
     * 运行时权限 request code
     */
    private static final int RUNTIME_PERMISSION_REQUEST_CODE = 0X0000;

    /**
     * SD卡读写权限 request code
     */
    private static final int WRITE_EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE = 0X0001;
    /**
     * 摄像头权限 request code
     */
    private static final int CAMERA_PERMISSION_REQUEST_CODE = 0x0002;
    /**
     * 麦克风权限 request code
     */
    private static final int AUDIO_PERMISSION_REQUEST_CODE = 0X0003;
    /**
     * 日历权限 request code
     */
    private static final int CALENDAR_PERMISSION_REQUEST_CODE = 0x0004;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestPermission();
    }

    /**
     * 请求运行时权限
     * 注意:同一时刻只能发一次权限请求
     */
    private void requestPermission() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            /**申请WRITE_EXTERNAL_STORAGE权限*/
            int hasExternalStoragePermission = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
            /** audio 权限*/
            int hasAudioPermission = ContextCompat.checkSelfPermission(this, android.Manifest.permission.RECORD_AUDIO);
            /** camera 权限*/
            int hasVideoPermission = ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA);

//            if (hasExternalStoragePermission != PackageManager.PERMISSION_GRANTED) {
//                //申请WRITE_EXTERNAL_STORAGE权限
//                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
//                        WRITE_EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE);
//            }
//
//
//            if (hasAudioPermission != PackageManager.PERMISSION_GRANTED ){
//                requestPermissions(new String[]{android.Manifest.permission.RECORD_AUDIO},
//                        AUDIO_PERMISSION_REQUEST_CODE);
//            }
//
//            if (hasVideoPermission != PackageManager.PERMISSION_GRANTED) {
//                requestPermissions(new String[]{android.Manifest.permission.CAMERA},
//                        CAMERA_PERMISSION_REQUEST_CODE);
//            }

            /** 音频麦克风,视频权限*/
            if (hasAudioPermission != PackageManager.PERMISSION_GRANTED || hasVideoPermission != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[] { android.Manifest.permission.RECORD_AUDIO, android.Manifest.permission.CAMERA },
                        RUNTIME_PERMISSION_REQUEST_CODE);
            }
        }

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        doWithPermissionResult(requestCode,permissions,grantResults);
    }

    /**
     * 运行时权限申请结果处理
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    protected void doWithPermissionResult(int requestCode, String[] permissions, int[] grantResults){

//        if (requestCode == WRITE_EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE){//申请WRITE_EXTERNAL_STORAGE权限是否通过
//            if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
//                // Permission Granted
//            }else {
//                // Permission Denied
//            }
//        }

//        if (requestCode == AUDIO_PERMISSION_REQUEST_CODE){//音频权限
//            if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
//                // Permission Granted
//            }else {
//                // Permission Denied
//            }
//        }

//        if (requestCode == CAMERA_PERMISSION_REQUEST_CODE) {//摄像头权限
//            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//                // Permission Granted
//            } else {
//                // Permission Denied
//            }
//        }

        if (requestCode == RUNTIME_PERMISSION_REQUEST_CODE) {//音频,摄像头权限
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission Granted
            } else {
                // Permission Denied
            }

            if (grantResults[1] == PackageManager.PERMISSION_GRANTED){
                // Permission Granted
            } else {
                // Permission Denied
            }
        }
    }
}

你可能感兴趣的:(Android)