Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I programmatically do a one ring call and how can I tell if the line I'm calling is off?

Tags:

android

I want my Android app to be able to tell if a given phone number is working by doing a one ring call (i.e. I call the number, wait for the first ring to sound on my side, then hung up). If there's no ring (i.e. the phone disconnected) I want to know it too. It's kind of a ping on a phone number. If this is possible, how could it be done?

like image 372
Frank Avatar asked Aug 26 '17 11:08

Frank


1 Answers

The sounds you are referring to is called 'Ringback'. 'Dial Tone' is the sound you hear when picking up a connected telephone which is not on a call.

Android Telephony classes don't give SDK applications access to listen to call audio, so monitoring ringback does not appear possible unless resorting to the NDK (although some people have reported success in listening to incoming and outgoing call audio with hacks)

The issue with your approach is that even If you manage to get the audio, you will need to listen to the sound on the line for a pause - this is not reliable because some providers read automated messages for invalid statuses and others even allow the user to upload their own ringback sounds (such as a song).

The best option for you is to listen for when TelephonyManager.EXTRA_STATE_RINGING begins and then wait for some arbitrary amount of time before hanging up. Otherstates such as CALL_STATE_OFFHOOK are not relevant for your situation.

public void onReceive(Context context, Intent intent) {
    if (timer == null && TelephonyManager.EXTRA_STATE_RINGING.equals(intent.getStringExtra(TelephonyManager.EXTRA_STATE))) {
        timer = new Handler(); //member variable
        timer.postDelayed(new Runnable() {
            @Override
            public void run() {
                hangUp();
            }
        }, 1500); //arbitrary delay
    }
}
like image 168
Nick Cardoso Avatar answered Oct 26 '22 17:10

Nick Cardoso