Android 检测不成功 phone 稍后再试
Android detect unsuccessful phone call and try again later
有没有办法在一段时间后重试错过或不成功的外拨 phone 电话?我正在使用 ACTION_CALL
意图启动 phone 调用并将其连接到 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 的设置如下:
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
}
}
如果不是成功的 phone 呼叫连接,我如何等待确定的时间间隔并再次尝试呼叫?还有,为什么listener中phoneNumber
的值总是空的?
不同于通过 IDLE
-> RINGING
-> OFFHOOK
的来电,
在出站呼叫中,它总是直接从 IDLE
-> OFFHOOK
跳转,即使它正在响铃(在另一侧)。
因此,如果您所说的“成功”是指 phone 呼叫已被另一方接听,PhoneStateListener
将无济于事,因为当对方接电话
Also, why is the value of phoneNumber in the listener always empty?
phoneNumber
仅针对来电填充,并且仅当您的应用同时具有 READ_CALL_LOG
和 READ_PHONE_STATE
权限时,see here.
如果您需要最后拨打的号码,您可以在拨出电话结束后几秒钟使用 Calls.getLastOutgoingCall
API。
有没有办法在一段时间后重试错过或不成功的外拨 phone 电话?我正在使用 ACTION_CALL
意图启动 phone 调用并将其连接到 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 的设置如下:
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
}
}
如果不是成功的 phone 呼叫连接,我如何等待确定的时间间隔并再次尝试呼叫?还有,为什么listener中phoneNumber
的值总是空的?
不同于通过 IDLE
-> RINGING
-> OFFHOOK
的来电,
在出站呼叫中,它总是直接从 IDLE
-> OFFHOOK
跳转,即使它正在响铃(在另一侧)。
因此,如果您所说的“成功”是指 phone 呼叫已被另一方接听,PhoneStateListener
将无济于事,因为当对方接电话
Also, why is the value of phoneNumber in the listener always empty?
phoneNumber
仅针对来电填充,并且仅当您的应用同时具有 READ_CALL_LOG
和 READ_PHONE_STATE
权限时,see here.
如果您需要最后拨打的号码,您可以在拨出电话结束后几秒钟使用 Calls.getLastOutgoingCall
API。