如何使用导航组件管理权限请求代码? [英] How to Manage the permission request code using Navigation Component?
问题描述
如何使用导航组件管理权限请求代码?
How to Manage the permission request code using Navigation Component?
我参考了太多网站 &堆栈链接,没有一个工作,仍然显示已弃用的 Waring
I referred too many website & stack links, none of it worked, still showing deprecated Waring
Google 参考链接:https://developer.android.com/training/permissions/requesting#manage-request-code-yourself
代码::在片段中使用以下代码
Code: :Using below code in fragment
private fun checkMultiplePermissions() {
// check permission first
if (ActivityCompat.checkSelfPermission(requireContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// request the permission
requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), 100)
} else {
proceedAfterPermission() // has the permission.
}
}
override fun onRequestPermissionsResult(requestCode: Int,
permissions: Array<String>, grantResults: IntArray) {
myLog("log", "location code : $requestCode")
when (requestCode) {
100 -> {
// If request is cancelled, the result arrays are empty.
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted.
proceedAfterPermission() // permission was granted.
myLog("log", "location granted")
} else {
// permission denied.
myLog("log", "location denied")
}
return
}
}
}
Fragment.kt:完整代码
import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
class FragmentPermission : Fragment() {
private var binding: FragmentPermisionBinding ?= null
// Kotlin
//implementation "androidx.fragment:fragment-ktx:1.3.4"
//https://developer.android.com/training/permissions/requesting#manage-request-code-yourself
// <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View {
binding = FragmentPermisionBinding.inflate(inflater, container, false)
if (ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// request the permission
requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), 100)
} else {
//proceedAfterPermission() // has the permission.
}
return binding!!.root
}
override fun onRequestPermissionsResult(requestCode: Int,
permissions: Array<String>, grantResults: IntArray) {
myLog("location3", "location code : $requestCode")
when (requestCode) {
100 -> {
// If request is cancelled, the result arrays are empty.
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted.
//proceedAfterPermission() // permission was granted.
myLog("location3", "location granted")
} else {
// permission denied.
myLog("location3", "location denied")
}
return
}
}
}
override fun onDestroyView() {
super.onDestroyView()
binding=null
}
}
推荐答案
requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), 100)
此方法已弃用.使用 registerForActivityResult(ActivityResultContract, ActivityResultCallback) 为 ActivityResultContract 传入 ActivityResultContracts.RequestMultiplePermissions 对象并在回调中处理结果.
This method is deprecated. use registerForActivityResult(ActivityResultContract, ActivityResultCallback) passing in a ActivityResultContracts.RequestMultiplePermissions object for the ActivityResultContract and handling the result in the callback.
requestPermissions()
API 已弃用,取而代之的是 registerForActivityResult(ActivityResultContract, ActivityResultCallback)
传入一个 ActivityResultContracts.RequestMultiplePermissions
对象用于 ActivityResultContract
并在回调中处理结果.
requestPermissions()
API is deprecated, and replaced by registerForActivityResult(ActivityResultContract, ActivityResultCallback)
passing in a ActivityResultContracts.RequestMultiplePermissions
object
for the ActivityResultContract
and handling the result in the callback.
首先是 activity-ktx build.gradle
新 API 需要模块级依赖:
First the activity-ktx build.gradle
module level dependency is needed for the the new API:
implementation 'androidx.activity:activity-ktx:1.3.0-beta01'
然后需要实现ActivityResultCallback
回调并将其作为registerForActivityResult
.此接口具有处理用户对权限请求对话框的响应的回调.
Then ActivityResultCallback
callback need to be implemented and passed as a parameter of the registerForActivityResult
. This interface has callbacks that handle the user response to the permission request dialog.
要显示权限对话框,请在 registerForActivityResult
函数返回的 ActivityResultLauncher
实例上调用 launch()
方法.
To display the permissions dialog, call the launch()
method on the instance of ActivityResultLauncher
that is returned by the registerForActivityResult
function.
因此,创建一个 ActivityResultLauncher
&实现回调以处理用户响应:
So, create an instance of ActivityResultLauncher
& implement the callback to handle the user response:
private val requestPermission =
registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
if (isGranted) { // Do something if permission granted
Log.d("LOG_TAG", "permission granted by the user")
} else { // Do something as the permission is not granted
Log.d("LOG_TAG", "permission denied by the user")
}
}
然后使用launch
函数请求权限:
Then request the permission by using the launch
function:
if (ContextCompat.checkSelfPermission(
requireContext(),
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
requestPermission.launch(Manifest.permission.ACCESS_FINE_LOCATION)
}
更新
对于一次请求多个权限:您需要传递数组启动()的权限.ActivityResultCallback
返回一个 Map
,以权限为键,授权状态为值:
For requesting multiple of permissions at once: You need to pass array of
permission to launch(). ActivityResultCallback
returns a Map<String, Boolean>
with permission as key and its grant status as value:
然后使用 ActivityResultContracts.RequestMultiplePermissions()
如下:
Then use ActivityResultContracts.RequestMultiplePermissions()
as follows:
private val requestMultiplePermissions =
registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
permissions.entries.forEach {
Log.e("LOG_TAG", "${it.key} = ${it.value}")
}
}
// Usage:
requestMultiplePermissions.launch(
arrayOf(
Manifest.permission.READ_CONTACTS,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.CAMERA
)
)
将其应用于共享代码:
class FragmentPermission : Fragment() {
private var binding: FragmentPermisionBinding ?= null
//...
private val requestPermission =
registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
if (isGranted) { // permission is granted
proceedAfterPermission()
} else {
// handle permission denial
}
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View {
binding = FragmentPermisionBinding.inflate(inflater, container, false)
if (ContextCompat.checkSelfPermission(
requireContext(),
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
requestPermission.launch(Manifest.permission.ACCESS_FINE_LOCATION)
} else {
proceedAfterPermission()
}
return binding!!.root
}
//.... rest of your code
}
这篇关于如何使用导航组件管理权限请求代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!