Skip to content

Commit

Permalink
定义权限申请code码
Browse files Browse the repository at this point in the history
  • Loading branch information
neilyhe committed Jun 21, 2024
1 parent 1e31479 commit 02117ec
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,10 @@ abstract class BaseActivity : AppCompatActivity() {
return true
}

protected fun requestPermission(permissions: Array<String>, requestCode: Int = 102) {
protected fun requestPermission(
permissions: Array<String>,
requestCode: Int = DEFAULT_REQUEST_PERMISSIONS_CODE
) {
ActivityCompat.requestPermissions(this, permissions, requestCode)
}

Expand All @@ -263,7 +266,7 @@ abstract class BaseActivity : AppCompatActivity() {
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == 102 || requestCode == 103) {
if (requestCode == DEFAULT_REQUEST_PERMISSIONS_CODE || requestCode == REQUEST_BLUETOOTH_PERMISSIONS_CODE) {
for (i in permissions.indices) {
if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
permissionDenied(permissions[i])
Expand Down Expand Up @@ -357,4 +360,9 @@ abstract class BaseActivity : AppCompatActivity() {
}
})
}

companion object{
const val DEFAULT_REQUEST_PERMISSIONS_CODE = 102 //默认权限申请code码
const val REQUEST_BLUETOOTH_PERMISSIONS_CODE = 104 //蓝牙权限申请code码
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ class DeviceCategoryActivity : PActivity(), MyCallback, CRecyclerView.RecyclerI
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == 102) {
if (requestCode == DEFAULT_REQUEST_PERMISSIONS_CODE) {
permissionDialog?.dismiss()
permissionDialog = null
for (i in permissions.indices) {
Expand Down Expand Up @@ -582,7 +582,7 @@ class DeviceCategoryActivity : PActivity(), MyCallback, CRecyclerView.RecyclerI
not_found_dev.visibility = View.GONE
return
}
requestPermission(permissions, 103)
requestPermission(permissions, REQUEST_BLUETOOTH_PERMISSIONS_CODE)
permissionDialog = PermissionDialog(this@DeviceCategoryActivity, R.mipmap.permission_location ,getString(R.string.permission_location_lips), getString(R.string.permission_location_ssid_ble))
permissionDialog!!.show()

Expand Down

0 comments on commit 02117ec

Please sign in to comment.