本文共 6106 字,大约阅读时间需要 20 分钟。
/** * 拍照按钮 */ 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, 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() } } }
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, 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() } } }}
// 动态权限管理 implementation 'com.tbruyelle.rxpermissions2:rxpermissions:0.9.5@aar' implementation 'io.reactivex.rxjava2:rxandroid:2.0.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) }}
转载地址:http://fuuta.baihongyu.com/