美文网首页
Android 6.0运行时动态权限

Android 6.0运行时动态权限

作者: 虎三呀 | 来源:发表于2018-05-04 13:23 被阅读0次

    前言

    运行时权限在这里就不介绍了。具体的可以移驾官方文档,或看其他博客,这篇文章只会教你如何使用动态权限。

    文章目录:

    一、危险权限列表
    二、官方运行时权限示例
    三、运行时权限封装

    一、危险权限列表

    在这边给出官方地址:正常权限和危险权限
    表 1. 危险权限和权限组。

    权限组 权限
    CALENDAR
    READ_CALENDAR
    WRITE_CALENDAR
    CAMERA
    CAMERA
    CONTACTS
    READ_CONTACTS
    WRITE_CONTACTS
    GET_ACCOUNTS
    LOCATION
    ACCESS_FINE_LOCATION
    ACCESS_COARSE_LOCATION
    MICROPHONE
    RECORD_AUDIO
    PHONE
    READ_PHONE_STATE
    CALL_PHONE
    READ_CALL_LOG
    WRITE_CALL_LOG
    ADD_VOICEMAIL
    USE_SIP
    PROCESS_OUTGOING_CALLS
    SENSORS
    BODY_SENSORS
    SMS
    SEND_SMS
    RECEIVE_SMS
    READ_SMS
    RECEIVE_WAP_PUSH
    RECEIVE_MMS
    STORAGE
    READ_EXTERNAL_STORAGE
    WRITE_EXTERNAL_STORAGE

    二、动态权限申请官方示例

    public class MainActivity extends AppCompatActivity {
    
        private MainActivity thisActivity;
        private final int MY_PERMISSIONS_REQUEST_READ_CONTACTS = 1024;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            thisActivity = this;
            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                //开始检查权限
                if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.READ_CONTACTS)
                        != PackageManager.PERMISSION_GRANTED) {
                    // 判断是否拒绝过权限
                    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
                            Manifest.permission.READ_CONTACTS)) {
                        //向用户解释 为什么要权限,然后再次尝试权限请求
                    } else {
                        //权限框第一次弹出,不需要解释,直接请求(当然,解释解释更好!)
                        ActivityCompat.requestPermissions(thisActivity,
                                new String[]{Manifest.permission.READ_CONTACTS},
                                MY_PERMISSIONS_REQUEST_READ_CONTACTS);
                    }
                } else {
                    //执行此权限该做的事情
                    Toast.makeText(thisActivity, "权限授权处:1", Toast.LENGTH_SHORT).show();
                }
            } else {
                //执行此权限该做的事情
                Toast.makeText(thisActivity, "权限授权处:1", Toast.LENGTH_SHORT).show();
            }
        }
    
        @Override
        public void onRequestPermissionsResult(int requestCode, String[] permissions, @NonNull int[] grantResults) {
            switch (requestCode) {
                case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
                    // If request is cancelled, the result arrays are empty.
                    if (grantResults.length > 0
                            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        //执行此权限该做的事情
                        Toast.makeText(thisActivity, "权限授权处:2", Toast.LENGTH_SHORT).show();
                    } else {
                        //权限被拒绝了
                    }
                    return;
                }
                // 其他权限的键值
                default:
            }
        }
    }
    

    注:即使获得权限也不要过度依赖它,因为用户可能使用其他渠道禁用权限,调用没有权限代码会抛出SecurityException异常

    三 、运行时权限封装

    public class PermissionUtil {
        public static final int REQUEST_CODE_REQUEST_PERMISSION = 1027;
        private static final String PACKAGE_URL_SCHEME = "package:";
        private static final int REQUEST_CODE_REQUEST_SETTING = 1028;
    
        public interface PermissionListener {
            void allGranted();                                          //申请的权限已全部被允许;
        }
    
        public static class PermissionTool {
            private PermissionListener mListener;
    
            public PermissionTool(PermissionListener listener) {
                mListener = listener;
            }
    
            public void checkAndRequestPermission(Activity activity, String... needPermissions) {
                boolean isAllGranted = true;
                for (String needPermission : needPermissions) {
                    if (ActivityCompat.checkSelfPermission(activity, needPermission)
                            != PackageManager.PERMISSION_GRANTED) {
                        isAllGranted = false;
                    }
                    Log.d("state", ActivityCompat.checkSelfPermission(activity, needPermission) + "");
                }
                if (isAllGranted) {
                    mListener.allGranted();
                } else {
                    ActivityCompat.requestPermissions(activity, needPermissions, REQUEST_CODE_REQUEST_PERMISSION);
                }
    
            }
    
            public void onRequestPermissionResult(final Activity activity,
                                                  int requestCode, String[] permissions, int[] grantResults) {
                if (requestCode == REQUEST_CODE_REQUEST_PERMISSION) {
                    boolean allGrant = true;
                    for (int i = 0; i < grantResults.length; i++) {
                        if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                            allGrant = false;
                        }
                        Log.d("PermissionTool", "onRequestPermissionResult(): " + grantResults[i] + "permission" + permissions[i]);
                    }
                    if (allGrant) {
                        mListener.allGranted();
                    } else {
                        Toast.makeText(activity, "部分所需权限未开启,将影响功能正常使用,请开启权限!", Toast.LENGTH_LONG).show();
                        startAppSettings(activity);
                    }
                }
            }
    
            public void startAppSettings(Activity activity) {
                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                intent.setData(Uri.parse(PACKAGE_URL_SCHEME + activity.getPackageName()));
                activity.startActivityForResult(intent, REQUEST_CODE_REQUEST_SETTING);
            }
        }
    }
    

    使用实例:

    public class MainActivity extends AppCompatActivity {
    
        private PermissionUtil.PermissionTool permissionTool;
        private String[] requestPermissions = new String[]{
                Manifest.permission.BLUETOOTH,
                Manifest.permission.BLUETOOTH_ADMIN,
                Manifest.permission.INTERNET,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION
        };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                permissionTool = new PermissionUtil.PermissionTool(new PermissionUtil.PermissionListener() {
                    @Override
                    public void allGranted() {
                        initView();
                    }
                });
                permissionTool.checkAndRequestPermission(MainActivity.this, requestPermissions);
            } else {
                initView();
            }
        }
    
        private void initView() {
            //在这里做界面初始化
        }
    
        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            permissionTool.onRequestPermissionResult(this, requestCode, permissions, grantResults);
        }
    }
    

    附录:权限-小知识点.

    硬件权限检查
    <uses-feature android:name="android.hardware.camera" android:required="false" />
    

    如果声明 android:required="false",说明允许您的应用安装在没有该功能的设备上。不过运行的时候必须调用PackageManager.hasSystemFeature()方法检查是否有此硬件。

    Activity权限检查

    在标签上使用android:permission属性,开启该Activity需要使用权限,如果没有权限则抛出SecurityException异常

    Service 权限检查

    在标签上使用android:permission属性,开启该Service 需要使用权限,如果没有权限则抛出SecurityException异常

    Broadcast权限检查

    发送广播的时候附带权限,在接收该广播的时候也需要相应权限才能接收,在标签上使用android:permission属性,接收该Broadcast使用权限,如果没有权限则接收不到广播

    本文有不当之处还望多指出,更多权限相关请移步官网

    相关文章

      网友评论

          本文标题:Android 6.0运行时动态权限

          本文链接:https://www.haomeiwen.com/subject/yaxdrftx.html