欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

动态权限

程序员文章站 2022-07-15 14:59:42
...
//权限管理,动态获取权限
    compile 'com.tbruyelle.rxpermissions:rxpermissions:[email protected]'
    compile 'io.reactivex:rxjava:1.1.3'
 <!--动太获取权限-->
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.RECEIVE_MMS" />
    <uses-permission android:name="android.permission.READ_CALL_LOG" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.READ_CALENDAR" />
    //点击按钮获取单个权限
    public void dan(View view) {
        //这个请求事件我写在点击事件里面,
        //点击button之后RxPermissions会为我们申请运行时权限
        RxPermissions.getInstance(MainActivity.this)
                .request(Manifest.permission.WRITE_EXTERNAL_STORAGE)//这里填写所需要的权限
                .subscribe(new Action1<Boolean>() {
                    @Override
                    public void call(Boolean aBoolean) {
                        if (aBoolean) {//true表示获取权限成功(注意这里在android6.0以下默认为true)
                            Toast.makeText(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE + ":" + "获取成功", Toast.LENGTH_SHORT).show();
                            Log.i("permissions", Manifest.permission.WRITE_EXTERNAL_STORAGE + ":" + "获取成功");
                        } else {
                            Toast.makeText(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE + ":" + "获取失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }
    //点击按钮获取多个权限
    public void duo(View view) {
        //分别申请多个权限
        RxPermissions.getInstance(MainActivity.this)
                //分别申请多个权限时,使用requestEach
                .requestEach(Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.RECORD_AUDIO,
                        Manifest.permission.CAMERA)
                .subscribe(new Action1<Permission>() {
                    @Override
                    public void call(Permission permission) {
                        if (permission.name.equals(Manifest.permission.ACCESS_FINE_LOCATION)) {
                            //当ACCESS_FINE_LOCATION权限获取成功时,permission.granted=true
                            Toast.makeText(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION + ":" + permission.granted, Toast.LENGTH_SHORT).show();
                            Log.i("permissions", Manifest.permission.ACCESS_FINE_LOCATION + ":" + permission.granted);
                        }
                        if (permission.name.equals(Manifest.permission.RECORD_AUDIO)) {
                            //当RECORD_AUDIO 权限获取成功时,permission.granted=true
                            Toast.makeText(MainActivity.this, Manifest.permission.RECORD_AUDIO + ":" + permission.granted, Toast.LENGTH_SHORT).show();
                            Log.i("permissions", Manifest.permission.RECORD_AUDIO + ":" + permission.granted);
                        }
                        if (permission.name.equals(Manifest.permission.CAMERA)) {
                            //当CAMERA权限获取成功时,permission.granted=true
                            Toast.makeText(MainActivity.this, Manifest.permission.CAMERA + ":" + permission.granted, Toast.LENGTH_SHORT).show();
                            Log.i("permissions", Manifest.permission.CAMERA + ":" + permission.granted);
                        }
                    }
                });
    }

 

相关标签: a'