简体   繁体   中英

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.

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:

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?

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

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.

Also, why is the value of phoneNumber in the listener always empty?

phoneNumber is populated for incoming calls only, and only if you app has both READ_CALL_LOG and READ_PHONE_STATE permissions, see here .

If you need to last called number you can use the Calls.getLastOutgoingCall API a few seconds after an outgoing call has ended.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM