天天看點

Android 權限管理

1. 危險權限

Android 權限管理

圖1.png

請求擷取到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). 示例效果

Android 權限管理

圖2.gif

3. RxPermissions

1). 添加依賴

RxAndroid
// 動态權限管理
    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)
  }
}
           

3). 效果示範

Android 權限管理

圖3.gif

代碼下載下傳

繼續閱讀