美文网首页
动态申请权限

动态申请权限

作者: 金卡戴珊ugly | 来源:发表于2019-07-26 17:21 被阅读0次

用过的两种权限申请方式:1系统api ,2RxJava链式写法
Manifest文件中权限,都是危险权限

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

首先是第一种系统api方式

public class LoginActivity {
    private PermissionsTool pManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        pManager = new PermissionsTool(this);
        pManager.requestSdcardPermission();
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions
            , @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case REQUEST_READWRITE_EXTERNAL_STORAGE:
                if (permissions.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    CommonUtil.showLog("");
                } else {
                    CommonUtil.showToast(this, "您拒绝了访问存储空间权限,可能导致程序不能正常运行,请允许该权限。");
                }
                pManager.requestManagePhonePermission();
                break;
            case REQUEST_MODIFY_PHONE_STATE:
                if (permissions.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    CommonUtil.showLog("");
                } else {
                    CommonUtil.showToast(this, "您拒绝了访问手机设置的权限,可能导致程序不能                  正常运行,请允许该权限。");
                }
                break;
        }
    }
}

PermissionsTool.java

public class PermissionsTool {
    public final static int REQUEST_READWRITE_EXTERNAL_STORAGE = 10000;
    public final static int REQUEST_RECORD_AUDIO = 10001;
    public final static int REQUEST_MODIFY_PHONE_STATE = 10002;
    private Activity act;

    public PermissionsTool(Activity a) {
        this.act = a;
    }

    @TargetApi(23)
    public void requestSdcardPermission() {
        if (ContextCompat.checkSelfPermission(act,
                Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(act,
                Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(act,
                    new String[]{
                            Manifest.permission.READ_EXTERNAL_STORAGE
                            , Manifest.permission.WRITE_EXTERNAL_STORAGE
                    }, REQUEST_READWRITE_EXTERNAL_STORAGE);
        }
    }

    @TargetApi(23)
    public void requestManagePhonePermission() {
        if (ContextCompat.checkSelfPermission(act,
                Manifest.permission.MODIFY_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(act,
                    new String[]{
                            Manifest.permission.MODIFY_PHONE_STATE
                    }, REQUEST_MODIFY_PHONE_STATE);
        }
    }

    @TargetApi(23)
    public void requestRecordAudioPermission() {
        if (ContextCompat.checkSelfPermission(act,
                Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(act,
                    new String[]{
                            Manifest.permission.RECORD_AUDIO
                    }, REQUEST_RECORD_AUDIO);
        }
    }
}

接下来是第二种RXjava方式,Activity中代码

//申请权限
PermissionsUtil pUtil = new PermissionsUtil(this);
pUtil.checkReadWriteSDCardPermission(null);
pUtil.checkManagePhonePermission();
pUtil.checkRecordAudioPermission(null);

PermissionsUtil.java

public class PermissionsUtil {
    private RxPermissions rxPermissions;
    private Context mContext;

    public interface PermissionListener {
        void onPermission(boolean isPermission);
    }

    public PermissionsUtil(Context mContext) {
        this.mContext = mContext;
        rxPermissions = new RxPermissions((Activity) mContext);
    }

    public void checkReadWriteSDCardPermission(final PermissionListener listener) {
        rxPermissions.request(Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean aBoolean) {
                if (!aBoolean) {
                    showAlert(mContext.getString(R.string.permission_sd));
                }
                if (listener != null) {
                    listener.onPermission(aBoolean);
                }
            }
        });
    }

    public void checkRecordAudioPermission(final PermissionListener listener) {
        rxPermissions.request(Manifest.permission.RECORD_AUDIO).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean aBoolean) {
                if (!aBoolean) {
                    showAlert(mContext.getString(R.string.permission_audio_record));
                }
                if (listener != null) {
                    listener.onPermission(aBoolean);
                }
            }
        });
    }

    public void checkManagePhonePermission() {
        if(rxPermissions.isGranted(Manifest.permission.MODIFY_PHONE_STATE)){ return;}
        rxPermissions.request(Manifest.permission.MODIFY_PHONE_STATE).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean aBoolean) {
                if (!aBoolean) {
                    showAlert(mContext.getString(R.string.permission_audio_record));
                }
            }
        });
    }

    private void showAlert(String content) {
        if (mContext != null) {
            CommonUtil.showToast(mContext, content);
            gotoPermissionSetting();
        }
    }

    /**
     * 跳转到权限设置界面
     */
    private void gotoPermissionSetting() {
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
        intent.setData(Uri.fromParts("package", mContext.getPackageName(), null));
        mContext.startActivity(intent);
    }
}

build.gradle文件

api 'io.reactivex:rxjava:1.1.0'
implementation 'com.tbruyelle.rxpermissions:rxpermissions:0.9.2@aar'

相关文章

网友评论

      本文标题:动态申请权限

      本文链接:https://www.haomeiwen.com/subject/xcmdrctx.html