覆盖其他类上的 fun onRequestPermissionsResult

Gre*_*reg 4 android kotlin

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

askMicrophonePermission中,我使用ActivityCompat.requestPermissions 函数。

但我无法覆盖 Permission类中的onRequestPermissionsResult

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

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

主要活动:

class MainActivity : AppCompatActivity() {

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

        Permission().askMicrophonePermission(this@MainActivity)
    }
}
Run Code Online (Sandbox Code Playgroud)

允许:

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")
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Dew*_*eed 6

onRequestPermissionsResult是一种来自 的方法FragmentActivity。由于您Permission没有扩展FragmentActivity,因此您无法onRequestPermissionsResult直接在Permission.

您可以将权限结果委托给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)
    }
}
Run Code Online (Sandbox Code Playgroud)

允许:

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")
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)