Android 6.0运行时权限

现在github上有不少关于6.0的封装库,也很好用。比如:https://github.com/yanzhenjie/AndPermission,添加compile用就行了

compile 'com.yanzhenjie:permission:1.0.7'

compile 的时可能会报错,按照提示下载解压即可,下载一下24的SDK版本,compileSdkVersion 24

这里以获取相机权限和读写SD卡文件为例


AndPermission.with(this).requestCode(300).permission(Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE).callback(this).start();



 @PermissionYes(300)
    private void getPermissionYes(List<String> grantedPermissions) {
       //用户同意权限,打开系统相机
        startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE),1);
    }

    @PermissionNo(300)
    private void getPermissionNo(List<String> deniedPermissions) {
        //用户拒绝了权限
    }

然后onActivityResult回调获取Bitmap:
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 1:
                if (data == null) {
                    return;
                }
                Bitmap bitmap = data.getExtras().getParcelable("data");
                imageView.setImageBitmap(bitmap);
                savePic(bitmap);
                break;
        }
    }

————————————————————————————————————————————————————————————————————————
补充:最近看到一个比较精简的6.0权限的封装工具类,写个Demo试了下,用起来还是蛮方便的,在这里分享一下。
XPermissionUtils代码:

package com.example.administrator.demo.utils;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.content.ContextCompat;

import java.util.ArrayList;
import java.util.List;

public class XPermissionUtils {
    private static int mRequestCode = -1;
    private static OnPermissionListener mOnPermissionListener;
    public interface OnPermissionListener {
        void onPermissionGranted();
        void onPermissionDenied();
    }

    @TargetApi(Build.VERSION_CODES.M)
    public static void requestPermissions(Context context, int requestCode, String[] permissions, OnPermissionListener listener) {
        if (context instanceof Activity) {
            mOnPermissionListener = listener;
            List<String> deniedPermissions = getDeniedPermissions(context, permissions);
            if (deniedPermissions.size() > 0) {
                mRequestCode = requestCode;
                ((Activity) context).requestPermissions(deniedPermissions.toArray(new String[deniedPermissions.size()]), requestCode);
            } else {
                if (mOnPermissionListener != null)
                    mOnPermissionListener.onPermissionGranted();
            }
        } else {
            throw new RuntimeException("Context must be an Activity");
        }
    }

    /** * 获取请求权限中需要授权的权限 */
    private static List<String> getDeniedPermissions(Context context, String... permissions) {
        List<String> deniedPermissions = new ArrayList<>();
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_DENIED) {
                deniedPermissions.add(permission);
            }
        }
        return deniedPermissions;
    }

    /** * 请求权限结果,对应Activity中onRequestPermissionsResult()方法。 */
    public static void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (mRequestCode != -1 && requestCode == mRequestCode) {
            if (mOnPermissionListener != null) {
                if (verifyPermissions(grantResults)) {
                    mOnPermissionListener.onPermissionGranted();
                } else {
                    mOnPermissionListener.onPermissionDenied();
                }
            }
        }
    }

    /** * 验证所有权限是否都已经授权 */
    private static boolean verifyPermissions(int[] grantResults) {
        for (int grantResult : grantResults) {
            if (grantResult != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }
}

就是上面这个,下面说一下怎么用!
首先,在你的基类里设置监听回调,比如我的是这样的:

package com.example.administrator.demo;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;

import com.example.administrator.demo.utils.XPermissionUtils;

/** * 基类 */
public class MyActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        XPermissionUtils.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

然后继承基类就可以了!比如我要获取相机权限。
核心代码:

public void requestCameraPermission(View view){
        XPermissionUtils.requestPermissions(this, 1, new String[]{Manifest.permission.CAMERA}, new XPermissionUtils.OnPermissionListener() {
            @Override
            public void onPermissionGranted() {
                Toast.makeText(MainActivity.this,"用户同意了权限,打开相机",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onPermissionDenied() {
                //用户拒绝权限,弹一个对话框让用户去设置里手动设置
                showDialog();
            }
        });
    }

你可能感兴趣的:(Android 6.0运行时权限)