一种可以将数字传递给重写函数 onRequestPermissionsResult 的方法?

A way by which a number can be passed to the overridden function onRequestPermissionsResult?

我正在尝试编写代码请求权限并进行 phone 调用。该数字由用户输入并由 editText 获取。我找不到这样做的方法,因为重写函数 onRequestPermissionsResult 也调用函数 makePhoneCall()



import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.core.app.ActivityCompat
import com.google.android.material.snackbar.Snackbar

const val PERMISSION_REQUEST_PHONE_CALL = 0

class MainActivity : AppCompatActivity(), ActivityCompat.OnRequestPermissionsResultCallback {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val callSupport = findViewById<Button>(R.id.button)
        val enterNumber = findViewById<EditText>(R.id.enterPhone)
        val number = enterNumber.text.toString()
        callSupport.setOnClickListener {
            makePhoneCallAfterPermission(it,number)
        }
    }
    private fun makePhoneCall(number:String){
        val intent = Intent().apply{
            action = Intent.ACTION_CALL
            data = Uri.parse("tel: $number")
        }
        startActivity(intent)
    }
    private fun makePhoneCallAfterPermission(view: View, number: String){
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED){
            makePhoneCall(number)
        }
        else{
            requestCallPermission(view,number)
        }
    }

    private fun requestCallPermission(view: View, number: String){
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.CALL_PHONE)){
            val snack = Snackbar.make(view, "The app requires your permission to make phone call .", Snackbar.LENGTH_INDEFINITE)
            snack.setAction("OK", View.OnClickListener {
                ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CALL_PHONE), PERMISSION_REQUEST_PHONE_CALL)
            })
            snack.show()
        }
        else{
            ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CALL_PHONE), PERMISSION_REQUEST_PHONE_CALL)
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
        if (requestCode == PERMISSION_REQUEST_PHONE_CALL){
            if (grantResults.isNotEmpty() && grantResults[0]== PackageManager.PERMISSION_GRANTED){
                makePhoneCall()
            }
            else{
                Toast.makeText(this, "Permission denied to make a phone call .",Toast.LENGTH_SHORT).show()
            }
        }
    }

}

任何关于如何实现结果的帮助都会非常有帮助。

您不能从 super class 更改函数,因为调用该函数的是 super class,而不是您。但是你可以通过添加一个 class 字段来解决这个问题,这样你就可以通过 class.

中的所有非静态方法访问这个字段