Easypermissions使用

参考文档:https://github.com/googlesamples/easypermissions
最近应需求,需要在App中添加动态权限,学习了一波Easypermissions,特此记录。
点到为止,撸起来
1.GitHub走起来,进行准备工作,添加依赖,设置targetSdkVersion,这点很重要,本人就因为未设置好这个浪费了好长时间,还以为代码写的有问题。

//最低23
targetSdkVersion 23

//动态权限Easypermissions
compile 'pub.devrel:easypermissions:0.1.9'

2.开始使用 EasyPermissions, 必须让你的Activity或者Fragment 重写onRequestPermissionsResult方法:

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        // 将结果转发到EasyPermissions
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

3.请求权限,getPermission()方法

    @AfterPermissionGranted(QX_XX)
    public void getPermission() {
      //perms内填写需要动态申请的权限Manifest.permission.ACCESS_COARSE_LOCATION为作者随意写的(根据需要)
      String[] perms = {Manifest.permission.ACCESS_COARSE_LOCATION};
        //动态权限
        if (EasyPermissions.hasPermissions(this, perms)) {
        //有对应权限的操作
        } else {  
        //没有对应权限的操作,"XXX权限"即为申请的权限名称                                                   EasyPermissions.requestPermissions(getActivity, "XXX权限",
                    QX_XX, perms);
        }

    }

4.请求成功和失败的回调方法(实现EasyPermissions.PermissionCallbacks接口)

    //成功
    @Override
    public void onPermissionsGranted(int requestCode, List list) {
        // Some permissions have been granted
        // ...
    }

    //失败
    @Override
    public void onPermissionsDenied(int requestCode, List list) {
        // Some permissions have been denied
        // ...
       if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
           new AppSettingsDialog.Builder(this).build().show();
        }
    }

请求失败的这个回调方法用不了了,百度搜索,用来直接跳转到应用权限设置的页面,可将代码改为

    //失败
    @Override
    public void onPermissionsDenied(int requestCode, List list) {
        // Some permissions have been denied
        // ...
        AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(MainTwoActivity.this);
        normalDialog
                .setMessage("没有该权限,此应用程序可能无法正常工作。打开应用设置以修改应用权限")
                .setTitle("必需权限")
                .setPositiveButton("设置", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent localIntent = new Intent();
                        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        if (Build.VERSION.SDK_INT >= 9) {
                            localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                            localIntent.setData(Uri.fromParts("package", getPackageName(), null));
                        } else if (Build.VERSION.SDK_INT <= 8) {
                            localIntent.setAction(Intent.ACTION_VIEW);
                            localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
                            localIntent.putExtra("com.android.settings.ApplicationPkgName", getPackageName());
                        }
                        startActivity(localIntent);
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                })
                .show();
    }

这样基本就没什么问题了,github上面的以下代码,个人以为使用的时候可以不添加。具体作用我也没搞清楚,有知道的可以评论告诉我

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE) {
        // Do something after user returned from app settings screen, like showing a Toast.
        Toast.makeText(this, R.string.returned_from_app_settings_to_activity, Toast.LENGTH_SHORT)
                .show();
    }
}

加油!!!多学习,多记录!马上周末了,美滋滋!

你可能感兴趣的:(Android重难点学习)