akashi seijuro
akashi seijuro

Reputation: 11

Get the results of phone calling

I am using callIntent.setData(Uri.parse(phone)) to call a number.

What is the best way to get a value of the result(i.e. the receiver rejected or not answering)?

Basically, I have an ArrayList which includes a series of phone numbers. I want to call the first one and then stop if it succeeds. Otherwise it continues calling the second one, and so on.

Upvotes: 1

Views: 124

Answers (1)

Alexandre Martin
Alexandre Martin

Reputation: 1502

For the benefits of all of us, I'll first explain how to call someone from an Android app, and then, how to receive a result.

Phone Call

You need to create an intent, to set its URI data as the phone number you need to call and start the activity.

Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:8195550504"));
startActivity(callIntent);

Get result back

To get informations about incoming and outgoing calls, you need to ask for permission :

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

Then, create a TelephonyManager object and start a listener to receive any call informations :

TelephonyManager TelephonyMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

TelephonyMgr.listen(new TeleListener(), PhoneStateListener.LISTEN_CALL_STATE);

If any changes are detected from a phone call, this function is triggered :

public void onCallStateChanged(int state, String incomingNumber)

and gives you informations about the incoming number and the phone state.

There are three states :

IDLE (no call incoming), OFFHOOK (line is busy), RINGING (call is incoming).

You could then use a switch statement to react to it :

switch (state){
    case TelephonyManager.CALL_STATE_IDLE:                      
       //CALL_STATE_IDLE;
       Toast.makeText(getApplicationContext(), "CALL_STATE_IDLE", Toast.LENGTH_LONG).show();
       break;

    case TelephonyManager.CALL_STATE_OFFHOOK:
       //CALL_STATE_OFFHOOK;
       Toast.makeText(getApplicationContext(), "CALL_STATE_OFFHOOK", Toast.LENGTH_LONG).show();
       break;

    case TelephonyManager.CALL_STATE_RINGING:
       //CALL_STATE_RINGING
       Toast.makeText(getApplicationContext(), incomingNumber, Toast.LENGTH_LONG).show();   
       Toast.makeText(getApplicationContext(), "CALL_STATE_RINGING", Toast.LENGTH_LONG).show();
        break;

   default:
        break;
}

If you register a RINGING state but not a OFFHOOK, you know that the call was unsuccessful (and you should call the next number in your list). If the OFFHOOK state follow the RINGING one, the call was successful.

Take a look at this link to create an outgoing call logger and read informations about the sent phone call using PRECISE_CALL_STATE :

Cannot detect when outgoing call is answered in Android

  • Tim S. Stack Overflow

Upvotes: 2

Related Questions