Android 敏感权限申请

动态权限管理是Android6.0(Build.VERSION_CODES.M = Api23)推出的,提醒用户当前APP所需要的权限,防止滥用。这些权限一般分为三种:(1)普通权限:直接manifest清单文件中写上注册就行了 (2)危险权限:需要动态申请 (3)特殊权限:此处暂不说明。
注意:这里我们就按照原生,和git上比较出名的第三方权限管理工具,EasyPermissions和RxPermissions


以下是EasyPermissions的代码样例:基本形成闭环,就是禁止后二次打开弹框太丑,谷歌代码的锅

package com.example.testperms;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import java.util.List;

import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;
import pub.devrel.easypermissions.PermissionRequest;

public class MainActivity extends AppCompatActivity  implements EasyPermissions.PermissionCallbacks {

    private String TAG = "MainActivity";
    private static final int REQUEST_CODE_RUNTIME_PERMS = 0;
    boolean isDenied = false;

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

        methodRequiresTwoPermission();
    }

    @AfterPermissionGranted(REQUEST_CODE_RUNTIME_PERMS)
    private void methodRequiresTwoPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            String[] perms = {Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE};
            if (EasyPermissions.hasPermissions(this, perms)) {
                // Already have permission, do the thing
                // ...
            } else {
                // Do not have permissions, request them now
                EasyPermissions.requestPermissions(this, "文件读写权限",
                        REQUEST_CODE_RUNTIME_PERMS, perms);
//                EasyPermissions.requestPermissions(
//                        new PermissionRequest.Builder(this, REQUEST_CODE_RUNTIME_PERMS, perms)
//                                .setRationale(R.string.str_rw_perms)
//                                .setPositiveButtonText(R.string.rationale_ask_ok)
//                                .setNegativeButtonText(R.string.rationale_ask_cancel)
////                                .setTheme(R.style.Theme_AppCompat)
//                                .build());
            }
        }
    }

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

        // Forward results to EasyPermissions
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
        if (requestCode == REQUEST_CODE_RUNTIME_PERMS) {
            // 成功获取运行时授权
        } else {
            // 获取运行时权限失败
        }
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
        Log.d(TAG, "onPermissionsGranted:" + requestCode + ":" + perms.size());

    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
//        methodRequiresTwoPermission();
        Log.d(TAG, "onPermissionsDenied:" + requestCode + ":" + perms.size());

        // (Optional) Check whether the user denied any permissions and checked "NEVER ASK AGAIN."
        // This will display a dialog directing them to enable the permission in app settings.
        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
            new AlertDialog.Builder(this)
                    .setTitle("权限提示")
                    .setMessage("本项认证需要外置存储权限,点击确认后在新页面滑动到最下方,在权限管理处,开启外置存储权限")
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            finish();
                        }
                    })
                    .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // 跳转到APP设置页面
                            Intent intent = new Intent();
                            intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                            Uri uri = Uri.fromParts("package", getPackageName(), null);
                            intent.setData(uri);
                            startActivity(intent);
                        }
                    })
                    .create().show();
        }
    }
}

Android 敏感权限申请

上一篇:SQLServer连接查询之Cross Apply和Outer Apply的区别及用法


下一篇:Java数据结构Map,List,Set及Queue相关的类图