[英]Android detect unsuccessful phone call and try again later
Is there a way to retry a missed or unsuccessful outbound phone call after a period of time?有没有办法在一段时间后重试错过或不成功的呼出电话? I am initiating a phone call using the
ACTION_CALL
intent and have it connected to a PhoneStateListener.我正在使用
ACTION_CALL
意图发起电话呼叫并将其连接到 PhoneStateListener。
class PlaceCall : AppCompatActivity() {
private fun outboundCall() {
val telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
telephonyManager.listen(CallListener(context), PhoneStateListener.LISTEN_CALL_STATE)
val callIntent = Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber))
startActivity(callIntent)
}
}
CallListener is setup like this: CallListener 设置如下:
class CallListener(cont: Context) : PhoneStateListener() {
private var context: Context = cont
private var incoming: Boolean = false
private var prevState: Int = TelephonyManager.CALL_STATE_IDLE
override fun onCallStateChanged(state: Int, phoneNumber: String?) {
val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
when(state) {
TelephonyManager.CALL_STATE_RINGING -> { incoming = true }
TelephonyManager.CALL_STATE_IDLE -> {
if(prevState == TelephonyManager.CALL_STATE_RINGING) { //Missed call?
}
TelephonyManager.CALL_STATE_OFFHOOK -> { Log.d("DEBUG", "calling $phoneNumber") }
}
}
prevState = state
}
}
How can I wait a determined interval and try the call again if it is anything other than a successful phone call connection?如果不是成功的电话呼叫连接,我如何等待确定的时间间隔并再次尝试呼叫? Also, why is the value of
phoneNumber
in the listener always empty?另外,为什么监听器中
phoneNumber
的值总是空的?
Unlike an incoming call which goes through IDLE
-> RINGING
-> OFFHOOK
, in outbound calls it always jumps directly from IDLE
-> OFFHOOK
even while it's ringing (on the other side).与通过
IDLE
-> RINGING
-> OFFHOOK
的来电不同,在出站呼叫中,即使在响铃时(在另一侧),它也总是直接从IDLE
-> OFFHOOK
跳转。
So if by "successful" you mean to say a phone call that had been picked up by the other side, PhoneStateListener
won't help you, as there's no additional state sent when the other side picks up.因此,如果您所说的“成功”是指对方接听的电话,
PhoneStateListener
不会帮助您,因为对方接听时不会发送额外的 state。
Also, why is the value of phoneNumber in the listener always empty?
另外,为什么监听器中phoneNumber的值总是空的?
phoneNumber
is populated for incoming calls only, and only if you app has both READ_CALL_LOG
and READ_PHONE_STATE
permissions, see here . phoneNumber
仅为来电填充,并且仅当您的应用同时具有READ_CALL_LOG
和READ_PHONE_STATE
权限时, 请参见此处。
If you need to last called number you can use the Calls.getLastOutgoingCall
API a few seconds after an outgoing call has ended.如果您需要最后一个被叫号码,您可以在拨出电话结束几秒钟后使用
Calls.getLastOutgoingCall
API。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.