一、AndroidManifest.xml 中列出需要的权限
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.test.vegeta">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
...>
</application>
</manifest>
二、判断 Android 系统版本
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//此处做动态权限申请
} else {
//低于23 不需要特殊处理
}
三、申请权限并处理回调
RxPermission项目地址
实例化
RxPermissions rxPermissions = new RxPermissions(this); // where this is an Activity instance
请求权限
rxPermissions
.request(Manifest.permission.CAMERA,
Manifest.permission.READ_PHONE_STATE)
.subscribe(granted -> {
if (granted) {//在已有权限的情况下,granted为true
// All requested permissions are granted
} else {
// At least one permission is denied
}
});