Friday, September 15, 2017

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

Leave a Comment

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?

1 Answers

Answers 1

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     } } 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment