用于电话通话状态的Android TelephonyManager监听器

时间:2020-07-25 06:03:10

标签: android kotlin telephonymanager android-audiomanager

我想为我的应用启动电话时设置一个侦听器。我需要知道通话状态,以便可以使用AudioManager启用免提电话。我不确定如何通过AudioManager创建监听器。我发现的所有示例都在Java中而不是Kotlin中。以下是相关的当前代码:

class PhoneCall : AppCompatActivity() {
    private fun placeCall() {
        if(ActivityCompat.checkSelfPermission(this, android.Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
            val telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
            telephonyManager.listen(CallListener, PhoneStateListener.LISTEN_CALL_STATE) //Not sure how to create CallListener? Is it a new class or object or something else?
            val callIntent = Intent(Intent.ACTION_CALL, Uri.parse(phoneNumber))
            startActivity(callIntent)
        }
    }
}

0 个答案:

没有答案