zoukankan      html  css  js  c++  java
  • Android搞定权限申请

    0x00 前言

    使用EasyPermissions库进行申请权限

    打开App时就申请权限,如果用户拒绝权限后,会循环申请

    如果永久拒绝后,会跳转到设置里继续申请

    效果图:

    效果图

    注:不讲原理,先教你怎么实现


    0x01 引入依赖

    appbuild.gradle里面,添加EasyPermissions的依赖

    implementation 'pub.devrel:easypermissions:2.0.0'
    

    添加后点击 File -> Sync Project with Gradle Files 重新构建一些项目


    0x02 创建PermissionActivity

    创建一个PermissionActivity用于获取权限,布局随意

    可以把它设为启动Activity,也可以由SplashActivity转至PermissionActivity


    创建完成后,让PermissionActivity实现EasyPermissions.PermissionCallbacks,并重新下面三个方法

    PermissionActivity复制下面的内容即可

    import androidx.annotation.NonNull;
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    
    import java.util.List;
    
    import pub.devrel.easypermissions.EasyPermissions;
    
    public class PermissionActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_permission);
        }
    
        // 重新下面三个方法
        
        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
        }
    
        @Override
        public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
    
        }
    
        @Override
        public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
    
        }
    }
    

    0x03 三个方法

    其中onRequestPermissionsResult是权限申请后执行的方法,默认就写成上面那样就行

    onPermissionsGranted是权限授权时的回调

    onPermissionsDenied是权限被拒绝时的回调


    0x04 申请权限

    假如要申请存储权限

    首先在AndroidManifest.xml里面加上这两行

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    

    然后,在PermissionActivity里定义两个常量

    // 指定申请存储权限时,一个标识符,用于在成功或失败回调中判断申请成功的或失败的是哪几个权限
    static final int STORAGE_CALL_BACK_CODE = 0;
    // 存储权限
    static final String[] perms_storage = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
    

    onCreate里面加上申请权限的代码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permission);
    
        if (EasyPermissions.hasPermissions(this, perms_storage)) {
            Toast.makeText(this, "已经有存储权限", Toast.LENGTH_SHORT).show();
        } else {
            EasyPermissions.requestPermissions(this, "请给我权限", STORAGE_CALL_BACK_CODE, perms_storage);
        }
    }
    

    0x05 处理授权成功

    onPermissionsGranted方法

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        //noinspection SwitchStatementWithTooFewBranches
        switch (requestCode) {
            // 如果存储权限申请通过
            case STORAGE_CALL_BACK_CODE:
                Toast.makeText(this, "已同意存储权限", Toast.LENGTH_SHORT).show();
                // pass为通过后执行的方法,注意下面有个finish(),pass()中就不要再加finish()了
                pass();
                finish();
                break;
        }
    }
    

    0x06 处理授权失败

    onPermissionsDenied方法

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        //noinspection SwitchStatementWithTooFewBranches
        switch (requestCode) {
            case STORAGE_CALL_BACK_CODE:
                Toast.makeText(this, "已拒绝存储权限", Toast.LENGTH_SHORT).show();
                ActivityCompat.requestPermissions(this, perms_storage, STORAGE_CALL_BACK_CODE);
                break;
        }
        // 如果权限被永久拒绝,就提示到设置页面中开启
        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
            Toast.makeText(this, "权限已被永久拒绝", Toast.LENGTH_SHORT).show();
            new AppSettingsDialog
                .Builder(this)
                .setTitle("权限已被永久拒绝")
                .setRationale("该应用需要此权限,否则无法正常使用,是否打开设置")
                .setPositiveButton("确定")
                .setNegativeButton("取消")
                .build()
                .show();
        }
    }
    

    0x07 完整代码

    PermissionActivity

    import androidx.annotation.NonNull;
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.core.app.ActivityCompat;
    
    import android.Manifest;
    import android.content.Intent;
    import android.os.Bundle;
    import android.widget.Toast;
    
    import java.util.List;
    
    import pub.devrel.easypermissions.AppSettingsDialog;
    import pub.devrel.easypermissions.EasyPermissions;
    
    public class PermissionActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks {
    
        static final int STORAGE_CALL_BACK_CODE = 0;
        static final String[] perms_storage = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_permission);
    
            if (EasyPermissions.hasPermissions(this, perms_storage)) {
                Toast.makeText(this, "已经有存储权限", Toast.LENGTH_SHORT).show();
            } else {
                EasyPermissions.requestPermissions(this, "请给我权限", STORAGE_CALL_BACK_CODE, perms_storage);
            }
        }
    
        void pass() {
            startActivity(new Intent(this, MainActivity.class));
        }
    
        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
        }
    
        @Override
        public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
            //noinspection SwitchStatementWithTooFewBranches
            switch (requestCode) {
                // 如果存储权限申请通过
                case STORAGE_CALL_BACK_CODE:
                    Toast.makeText(this, "已同意存储权限", Toast.LENGTH_SHORT).show();
                    // pass为通过后执行的方法,注意下面有个finish(),pass()中就不要再加finish()了
                    pass();
                    finish();
                    break;
            }
        }
    
        @Override
        public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
            //noinspection SwitchStatementWithTooFewBranches
            switch (requestCode) {
                case STORAGE_CALL_BACK_CODE:
                    Toast.makeText(this, "已拒绝存储权限", Toast.LENGTH_SHORT).show();
                    ActivityCompat.requestPermissions(this, perms_storage, STORAGE_CALL_BACK_CODE);
                    break;
            }
            // 如果权限被永久拒绝,就提示到设置页面中开启
            if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
                Toast.makeText(this, "权限已被永久拒绝", Toast.LENGTH_SHORT).show();
                new AppSettingsDialog
                        .Builder(this)
                        .setTitle("权限已被永久拒绝")
                        .setRationale("该应用需要此权限,否则无法正常使用,是否打开设置")
                        .setPositiveButton("确定")
                        .setNegativeButton("取消")
                        .build()
                        .show();
            }
        }
    }
    

    0x08 项目代码

    项目在github上面,有用的话,可以给一个star

    https://github.com/tanyiqu/android-permission-demo

  • 相关阅读:
    多测师肖老师_设计用例方法之场景法___(4.6)
    多测师肖老师_设计用例方法之正交表___(4.5)
    多测师肖老师_设计用例方法之因果图___(4.4)
    多测师肖老师_设计用例方法之边界值___(4.3)
    多测师肖老师_设计用例方法之状态迁移法___(4.7)
    多测师肖老师_设计用例方法之等价类___(4.2)
    多测师肖老师_设计用例方法之微信发红包xmind图___(5.1)
    Python+Appium自动化环境搭建
    QQ传文件测试要点
    Python算法(一)冒泡排序
  • 原文地址:https://www.cnblogs.com/tanyiqu/p/14172423.html
Copyright © 2011-2022 走看看