1. 危险权限
请求获取到Permission Group中的任一权限,则该组内其他权限自动被允许.
2. 系统Api使用
1). 创建界面布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mazaiting.permissiondemo.MainActivity">
<Button
android:onClick="takePicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="拍照"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>
2). 点击按钮打开相机
/**
* 拍照按钮
*/
fun takePicture(view: View) {
// 检测权限
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
// 用户拒绝了权限,并且点击了不再提醒
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
// 已经禁止提示
AlertDialog.Builder(this)
.setTitle("提示")
.setMessage("权限已拒绝,是否需要重新开启")
.setPositiveButton("确定") { dialog: DialogInterface?, which: Int ->
// 设置防止出现不再提示页面,进入权限管理页面
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
val uri = Uri.fromParts("package", packageName, null)
intent.data = uri
startActivityForResult(intent, REQUEST_PERMISSION_SETTING)
}
.setNegativeButton("取消", null)
.create()
.show()
} else {
// 无权限
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.CAMERA),
REQUEST_PERMISSION
)
}
} else {
// 有权限
openCamera()
}
}
/**
* 打开相机
*/
private fun openCamera() {
val openCameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivity(openCameraIntent)
}
3). 权限结果回调
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
// 判断请求码
if (REQUEST_PERMISSION == requestCode) {
// 判断grantResults数组不为空
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// 用户同意授权
openCamera()
} else {
// 用户拒绝授权
Toast.makeText(this, "用户拒绝了授权", Toast.LENGTH_SHORT).show()
}
}
}
4). 完整代码
class MainActivity1: AppCompatActivity() {
// 存储静态量
companion object {
/**权限请求*/
val REQUEST_PERMISSION = 0x100
/**权限设置页返回*/
val REQUEST_PERMISSION_SETTING = 0x101
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
/**
* 拍照按钮
*/
fun takePicture(view: View) {
// 检测权限
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
// 用户拒绝了权限,并且点击了不再提醒
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
// 已经禁止提示
AlertDialog.Builder(this)
.setTitle("提示")
.setMessage("权限已拒绝,是否需要重新开启")
.setPositiveButton("确定") { dialog: DialogInterface?, which: Int ->
// 设置防止出现不再提示页面,进入权限管理页面
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
val uri = Uri.fromParts("package", packageName, null)
intent.data = uri
startActivityForResult(intent, REQUEST_PERMISSION_SETTING)
}
.setNegativeButton("取消", null)
.create()
.show()
} else {
// 无权限
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.CAMERA),
REQUEST_PERMISSION
)
}
} else {
// 有权限
openCamera()
}
}
/**
* 打开相机
*/
private fun openCamera() {
val openCameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivity(openCameraIntent)
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
// 判断请求码
if (REQUEST_PERMISSION == requestCode) {
// 判断grantResults数组不为空
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// 用户同意授权
openCamera()
} else {
// 用户拒绝授权
Toast.makeText(this, "用户拒绝了授权", Toast.LENGTH_SHORT).show()
}
}
}
}
5). 示例效果
3. RxPermissions
1). 添加依赖
// 动态权限管理
implementation 'com.tbruyelle.rxpermissions2:rxpermissions:0.9.5@aar'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
2). 示例代码
class MainActivity : AppCompatActivity() {
// 定义权限类
private lateinit var mRxPermission: RxPermissions
// 存储静态量
companion object {
/**权限设置页返回*/
val REQUEST_PERMISSION_SETTING = 0x101
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mRxPermission = RxPermissions(this)
}
/**
* 拍照按钮
*/
fun takePicture(view: View) {
mRxPermission
.requestEachCombined(
Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE
)
.subscribe { permission ->
when {
permission.granted -> openCamera()
permission.shouldShowRequestPermissionRationale -> showSetting()
else -> {
Toast.makeText(this, "被拒绝", Toast.LENGTH_SHORT).show()
showSetting()
}
}
}
}
/**
* 显示权限设置页
*/
private fun showSetting() {
// 已经禁止提示
AlertDialog
.Builder(this)
.setTitle("提示")
.setMessage("权限已拒绝,是否需要重新开启")
.setPositiveButton("确定") { dialog: DialogInterface?, which: Int ->
// 设置防止出现不再提示页面,进入权限管理页面
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
val uri = Uri.fromParts("package", packageName, null)
intent.data = uri
startActivityForResult(intent, REQUEST_PERMISSION_SETTING)
}
.setNegativeButton("取消", null)
.create()
.show()
}
/**
* 打开相机
*/
private fun openCamera() {
val openCameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivity(openCameraIntent)
}
}