6.0权限请求AndroidPermission

6.0权限请求AndroidPermission

AndroidPermission是一个Android6.0+动态权限请求库,已兼容到Android14.0。
GitHub仓库地址

引入

gradle

allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}

implementation 'com.github.DL-ZhangTeng:RequestPermission:1.4.0'

使用默认透明背景PermissionActivity执行实际权限请求

public class MainActivity extends AppCompatActivity {

    private AndroidPermission androidPermission;
    private String[] permissions;

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

    public void onClick(View view) {
        permissions = new String[]{Permission.MANAGE_EXTERNAL_STORAGE, Permission.CAMERA};
        androidPermission = new AndroidPermission.Builder()
                .with(this)
                .permission(permissions)
                .callback(new Callback() {
                    @Override
                    public void success(Activity activity) {
                        Toast.makeText(MainActivity.this, "success", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void failure(Activity activity) {
                        Toast.makeText(MainActivity.this, "failure", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void nonExecution(Activity activity) {
                        Toast.makeText(MainActivity.this, "nonExecution", Toast.LENGTH_SHORT).show();
                    }
                })
                .build();

        Boolean isRequestPermissions = (Boolean) SPUtilsKt.getFromSP(this, "isRequestPermissions", false, "currentUser");
        if (Boolean.TRUE.equals(isRequestPermissions)) {
            //用于再次请求权限
            androidPermission.retryExecute();
        } else {
            //用于初次请求权限
            androidPermission.execute();
            SPUtilsKt.putToSP(this, "isRequestPermissions", true, "currentUser");
        }
    }
}

使用自定义的页面执行实际请求(实现Request接口)

public class MainActivity extends AppCompatActivity implements Request {

    private AndroidPermission androidPermission;
    private String[] permissions;

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

    public void onClick(View view) {
        permissions = new String[]{Permission.MANAGE_EXTERNAL_STORAGE, Permission.CAMERA};
        androidPermission = new AndroidPermission.Builder()
                .with(this)
                .request(this)
                .permission(permissions)
                .callback(new Callback() {
                    @Override
                    public void success(Activity activity) {
                        Toast.makeText(MainActivity.this, "success", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void failure(Activity activity) {
                        Toast.makeText(MainActivity.this, "failure", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void nonExecution(Activity activity) {
                        Toast.makeText(MainActivity.this, "nonExecution", Toast.LENGTH_SHORT).show();
                    }
                })
                .build();

        Boolean isRequestPermissions = (Boolean) SPUtilsKt.getFromSP(this, "isRequestPermissions", false, "currentUser");
        if (Boolean.TRUE.equals(isRequestPermissions)) {
            //用于再次请求权限
            androidPermission.retryExecute();
        } else {
            //用于初次请求权限
            androidPermission.execute();
            SPUtilsKt.putToSP(this, "isRequestPermissions", true, "currentUser");
        }
    }

    @Override
    public void requestPermissions(Context context, int permissionCode, Callback callback) {
        //如果Android11存储权限与其它Android6权限同时请求时,先请求MANAGE_EXTERNAL_STORAGE权限
        if (Build.VERSION.SDK_INT >= 30) {
            if (VerifyUtils.hasManageExternalStorage(permissions)) {
                requestPermissions(permissions, permissionCode);
            } else {
                Intent intent1 = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
                intent1.setData(Uri.parse("package:" + getPackageName()));
                startActivityForResult(intent1, permissionCode);
            }
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(permissions, permissionCode);
        }
    }

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

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //自定义request时执行结果
        androidPermission.onActivityResult(requestCode, resultCode, data);
    }
}

混淆

-keep public class com.zhangteng.**.*{ *; }

历史版本

版本更新更新时间
v1.4.0Android14的新增权限READ_MEDIA_VISUAL_USER_SELECTED2023/6/19 at 00:00
v1.3.0权限请求被拒绝再次请求权限跳转设置页面2023/4/10 at 18:00
v1.2.2Android12、13的新增权限兼容2023/3/3 at 12:00
v1.2.1Android11的存储写入权限拒绝结果过滤2023/2/14 at 0:07
v1.2.0重新构建2022/1/26 at 12:42
v1.1.5兼容Android11的存储权限2021/6/11 at 15:29
v1.1.4支持自定义Request25 April 2021
v1.1.0迁移到androidx22 July 2020
v1.0.0内存优化15 May 2020

赞赏

如果您喜欢AndroidPermission,或感觉AndroidPermission帮助到了您,可以点右上角“Star”支持一下,您的支持就是我的动力,谢谢您

联系我

邮箱:763263311@qq.com/ztxiaoran@foxmail.com

License

MIT License

Copyright © [2020] [Swing]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值