android permission适配

1、Google官方文档

permission的适配,主要是适配官方的所谓Dangerous permissions and permission groups

2、权限适配类PermissionAdapter
import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

/**
 * Created by LuoZheng on 2016/8/23.
 */
public class PermissionAdapter {

    private static final int REQ_PERMISSION_SMS = 0x1;
    private static final int REQ_PERMISSION_PHONE = 0x2;
    private static final int REQ_PERMISSION_LOCATION = 0x3;
    private static final int REQ_PERMISSION_CONTACTS = 0x4;
    private static final int REQ_PERMISSION_CAMERA = 0x5;
    private static final int REQ_PERMISSION_MICROPHONE = 0x6;
    private static final int REQ_PERMISSION_STORAGE = 0x7;

    private static PermissionAdapter instance;
    private Activity context;
    private PermissionVerifyCallBack permissionCallback;

    public static PermissionAdapter getInstance(Activity context){
        if(instance == null){
            synchronized (PermissionAdapter.class){
                instance = new PermissionAdapter(context);
            }
        }
        return instance;
    }

    private PermissionAdapter(Activity context){
        this.context = context;
    }

    public boolean verifyPermissions(int[] grantResults) {
        // At least one result must be checked.
        if (grantResults.length < 1) {
            return false;
        }

        // Verify that each required permission has been granted, otherwise return false.
        for (int result : grantResults) {
            if (result != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

    public interface PermissionVerifyCallBack {
        void onPermissionGet();
        void onPermissionLost();
    }

    public void requestMicrophonePermission(PermissionVerifyCallBack callback) {
        this.permissionCallback = callback;
        if (ContextCompat.checkSelfPermission(context,
                Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(context, new String[] {
                    Manifest.permission.RECORD_AUDIO
            }, REQ_PERMISSION_MICROPHONE);
        } else {
            this.permissionCallback.onPermissionGet();
        }
    }

    public void requestCameraPermission(PermissionVerifyCallBack callback) {
        this.permissionCallback = callback;
        if (ContextCompat.checkSelfPermission(context,
                Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(context, new String[] {
                    Manifest.permission.CAMERA
            }, REQ_PERMISSION_CAMERA);
        } else {
            this.permissionCallback.onPermissionGet();
        }
    }

    public void requestContactsPermission(PermissionVerifyCallBack callback) {
        this.permissionCallback = callback;
        if (ContextCompat.checkSelfPermission(context,
                Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(context, new String[] {
                    Manifest.permission.GET_ACCOUNTS
            }, REQ_PERMISSION_CONTACTS);
        } else {
            this.permissionCallback.onPermissionGet();
        }
    }

    public void requestLocationPermission(PermissionVerifyCallBack callback) {
        this.permissionCallback = callback;
        if (ContextCompat.checkSelfPermission(context,
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(context, new String[] {
                    Manifest.permission.ACCESS_FINE_LOCATION
            }, REQ_PERMISSION_LOCATION);
        } else {
            this.permissionCallback.onPermissionGet();
        }
    }

    public void requestPhonePermission(PermissionVerifyCallBack callback) {
        this.permissionCallback = callback;
        if (ContextCompat.checkSelfPermission(context,
                Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(context, new String[] {
                    Manifest.permission.READ_PHONE_STATE
            }, REQ_PERMISSION_PHONE);
        } else {
            this.permissionCallback.onPermissionGet();
        }
    }

    public void requestStoragePermission(PermissionVerifyCallBack callback) {
        this.permissionCallback = callback;
        if (ContextCompat.checkSelfPermission(context,
                Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(context,
                Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(context, new String[] {
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_EXTERNAL_STORAGE
            }, REQ_PERMISSION_STORAGE);
        } else {
            this.permissionCallback.onPermissionGet();
        }
    }

    public void requestSmsPermission(PermissionVerifyCallBack callback) {
        this.permissionCallback = callback;
        if (ContextCompat.checkSelfPermission(context,
                Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(context,
                Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(context,
                Manifest.permission.RECEIVE_SMS) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(context, new String[] {
                    Manifest.permission.SEND_SMS,
                    Manifest.permission.READ_SMS, Manifest.permission.RECEIVE_SMS
            }, REQ_PERMISSION_SMS);
        } else {
            this.permissionCallback.onPermissionGet();
        }
    }

    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        switch (requestCode) {
            case REQ_PERMISSION_SMS:
            case REQ_PERMISSION_STORAGE:
            case REQ_PERMISSION_PHONE:
            case REQ_PERMISSION_LOCATION:
            case REQ_PERMISSION_CONTACTS:
            case REQ_PERMISSION_CAMERA:
            case REQ_PERMISSION_MICROPHONE:
                if (permissionCallback != null) {
                    if (verifyPermissions(grantResults)) {
                        permissionCallback.onPermissionGet();
                    } else {
                        permissionCallback.onPermissionLost();
                    }
                    permissionCallback = null;
                }
                break;
            default:
                break;
        }
    }
}

BaseActivity

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
            @NonNull int[] grantResults) {
      PermissionAdapter.getInstance(this).onRequestPermissionsResult(requestCode,permissions,grantResults);
    }

使用

PermissionAdapter.getInstance(UserSettingsActivity.this).requestCameraPermission(new PermissionAdapter.PermissionVerifyCallBack() {
                    @Override
                    public void onPermissionGet() {
                        //......
                    }

                    @Override
                    public void onPermissionLost() {
                        ToastUtils.makeToast(getString(R.string.permission_lost));
                    }
                });

你可能感兴趣的:(android permission适配)