覆盖其他类的onRequestPermissionsResult乐趣

时间:2018-05-07 02:43:09

标签: android kotlin

我写了权限类,这里有 askMicrophonePermission 方法

askMicrophonePermission 中,我使用 ActivityCompat.requestPermissions 功能

但我无法在权限覆盖 onRequestPermissionsResult 类。

如果我想在权限覆盖 onRequestPermissionsResult , 在 MainActivity

如何写?这是我的代码。

MainActivity:

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")
            }
        }
    }
}

2 个答案:

答案 0 :(得分:2)

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)
    }
}

许可:

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")
            }
        }
    }
}

答案 1 :(得分:0)

仅扩展权限类上的片段活动

class Permission() : FragmentActivity()

我在项目中的工作方式

class AskPermission(val activity:Activity): FragmentActivity() {

private val code: Int =0

fun ask(context: Context, permission: String) {
    if (ContextCompat.checkSelfPermission(
            context,
            permission
        ) == PackageManager.PERMISSION_GRANTED
    ) {
        Toast.makeText(context, "Already Granted", Toast.LENGTH_LONG)
            .show()
    } else {
        requestStoragePermission(context)
    }
}

private fun requestStoragePermission(context: Context) {
    if (ActivityCompat.shouldShowRequestPermissionRationale(
            activity,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
        )
    ) {
        ActivityCompat.requestPermissions(context as Activity, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),code)
    }
    else
    {
        ActivityCompat.requestPermissions(context as Activity, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),code)
    }
}

override fun onRequestPermissionsResult(
    requestCode: Int,
    permissions: Array<out String>,
    grantResults: IntArray
) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults)
    if(grantResults[0]==PackageManager.PERMISSION_GRANTED)
    {

    }
    else
    {

    }
}
}