10

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?

Frank
  • 2,777
  • 5
  • 18
  • 30
  • 1
    Just so you know, dial tone usually refers to the sound on one's own line that sounds before you start dialing to let you know your own line is connected, it has nothing to do with the number you're calling. I think you're meaning to say "By doing a one ring call" and "if there's no ring." Also, when you call a phone, sometimes if the phone is disconnected it rings twice before giving the SIT and the "The number you have dialed is not in service" message. There are several more cases. Source: I work in a call center. – Davy M Aug 26 '17 at 11:45
  • Thanks! I appreciate the corrections :) – Frank Aug 26 '17 at 12:17
  • Maybe I'm reading it wrong, but isn't that code for _incoming_ calls? My idea is sending calls... – Frank Sep 09 '17 at 10:51
  • FYI: To achieve this type of functionality you will have to pretty much write your own dialer app. – Budius Sep 09 '17 at 11:55
  • I would suggest you to check if the receivers mobile/device is online or not by checking network info and using this info to check/deduct if the receiver's device/mobile is online and/or can receive calls or not.. Hope this helps . – shadygoneinsane Sep 15 '17 at 10:52

1 Answers1

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
    }
}
Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124