gpt4 book ai didi

android - 在其他类上覆盖 fun onRequestPermissionsResult

转载 作者:搜寻专家 更新时间:2023-11-01 09:26:25 24 4
gpt4 key购买 nike

我写了Permission类,这里有askMicrophonePermission方法。

askMicrophonePermission中,我使用了ActivityCompat.requestPermissions功能。

但我不能覆盖 onRequestPermissionsResult in Permission类。

如果我想在 Permission覆盖 onRequestPermissionsResult,而不是在 MainActivity 中。

怎么写??这是我的代码。

主要 Activity :

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

Permission().askMicrophonePermission(this@MainActivity)
}
}

权限:

class Permission{
private val tag = "Permission message"
private val userMicrophonePermissionAgreeCode = 1

fun askMicrophonePermission(context: Context){

val currentMicrophonePermission = ActivityCompat.checkSelfPermission(context,android.Manifest.permission.RECORD_AUDIO)


if(currentMicrophonePermission != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(context as Activity, arrayOf(android.Manifest.permission.RECORD_AUDIO), userMicrophonePermissionAgreeCode)
}

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
when(requestCode){
userMicrophonePermissionAgreeCode -> {
if( grantResults[0] == PackageManager.PERMISSION_GRANTED )
Log.i(tag,"Agree microphone permission")
else
Log.i(tag,"Not agree microphone permission")
}
}
}
}

最佳答案

onRequestPermissionsResultFragmentActivity 的一个方法。由于您的 Permission 没有扩展 FragmentActivity,因此您不能直接在 Permission 中覆盖 onRequestPermissionsResult

您可以将权限结果委托(delegate)给Permission

class MainActivity : AppCompatActivity() {

private lateinit var permission: Permission

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

permission = Permission()
permission.askMicrophonePermission(this@MainActivity)
}

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
permission.handlePermissionsResult(resquestCode, permissions, grantResults)
}
}

权限:

class Permission{
private val tag = "Permission message"
private val userMicrophonePermissionAgreeCode = 1

fun askMicrophonePermission(context: Context){

val currentMicrophonePermission = ActivityCompat.checkSelfPermission(context,android.Manifest.permission.RECORD_AUDIO)


if(currentMicrophonePermission != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(context as Activity, arrayOf(android.Manifest.permission.RECORD_AUDIO), userMicrophonePermissionAgreeCode)
}

fun handlePermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
when(requestCode){
userMicrophonePermissionAgreeCode -> {
if( grantResults[0] == PackageManager.PERMISSION_GRANTED )
Log.i(tag,"Agree microphone permission")
else
Log.i(tag,"Not agree microphone permission")
}
}
}
}

关于android - 在其他类上覆盖 fun onRequestPermissionsResult,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50206419/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com