Android 动态权限,简单封装一个PermissionUtil,手动申请权限

可以申请多个权限,有一个失败则返回失败

package com.zhangyu.mytestwlmusic;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;

public class PermissionUtil {

    private static final String TAG = "PermissionUtil";
    private final int REQUEST_CODE_PERMISSION = 111;

    private Activity activity;

    public PermissionUtil(Activity activity) {
        this.activity = activity;
    }

    /**
     * 需要获取的危险权限
     */
    String[] permissions = new String[]{
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA
    };

    /**
     * 请求权限
     */
    public void requestAllPermission() {
        //判断是否已经赋予权限
        for (String permission : permissions) {
            Log.e(TAG, "requestAllPermission: " + permission + "," + ContextCompat.checkSelfPermission(activity, permission));
            //如果有一个权限没有拿到
            if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
                //申请全部权限
                ActivityCompat.requestPermissions(activity, permissions, REQUEST_CODE_PERMISSION);
                break;
            }
        }
    }

    /**
     * 结果的判断
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {

        for (int i = 0; i < permissions.length; i++) {
            String p = permissions[i];
            int g = grantResults[i];
            Log.e(TAG, "onRequestPermissionsResult: " + p + "," + g);
        }

        switch (requestCode) {
            case REQUEST_CODE_PERMISSION: {
                // If request is cancelled, the result arrays are empty.
                boolean isGranted = true;
                for (int i = 0; i < grantResults.length; i++) {
                    //有一个失败则失败。
                    if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                        isGranted = false;
                        break;
                    }
                }
                if (isGranted) {
                    Log.d(TAG, "onRequestPermissionsResult granted");
                    // permission was granted, yay! Do the
                    // contacts-related task you need to do.
                    //createFile();

                } else {
                    Log.e(TAG, "onRequestPermissionsResult denied");
                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                    showWaringDialog();
                }
                return;
            }

            // other 'case' lines to check for other
            // permissions this app might request
        }
    }

    /**
     * 未获取到权限提醒
     */
    private void showWaringDialog() {
        AlertDialog dialog = new AlertDialog.Builder(activity)
                .setTitle("警告!")
                .setMessage("请前往设置中打开相关权限,否则功能无法正常运行!")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 一般情况下如果用户不授权的话,功能是无法运行的,做退出处理
                        activity.finish();
                    }
                }).show();
    }

}

MainActivity

    private PermissionUtil permissionUtil = new PermissionUtil(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        permissionUtil.requestAllPermission();
    }

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

你可能感兴趣的:(Android基础)