Take the 2-minute tour ×

I am launching an activity to make a phone call, but when I pressed the 'end call' button, it does not go back to my activity. Can you please tell me how can I launch a call activity which comes back to me when 'End call' button is pressed? This is how I'm making the phone call:

    String url = "tel:3334444";
    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
share|improve this question

17 Answers 17

use a PhoneStateListener to see when the call is ended. you will most likely need to trigger the listener actions to wait for a the call to start (wait until changed from PHONE_STATE_OFFHOOK to PHONE_STATE_IDLE again) and then write some code to bring your app back up on the IDLE state.

you may need to run the listener in a service to ensure it stays up and your app is restarted. some example code:

EndCallListener callListener = new EndCallListener();
TelephonyManager mTM = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);

Listener definition:

private class EndCallListener extends PhoneStateListener {
    public void onCallStateChanged(int state, String incomingNumber) {
        if(TelephonyManager.CALL_STATE_RINGING == state) {
            Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
        if(TelephonyManager.CALL_STATE_OFFHOOK == state) {
            //wait for phone to go offhook (probably set a boolean flag) so you know your app initiated the call.
            Log.i(LOG_TAG, "OFFHOOK");
        if(TelephonyManager.CALL_STATE_IDLE == state) {
            //when this state occurs, and your flag is set, restart your app
            Log.i(LOG_TAG, "IDLE");
share|improve this answer
Don't forget the permission. ;) –  Gp2mv3 Jan 26 '12 at 17:58
As Gp2mv3 has noted, don't forget to add the READ_PHONE_STATE permission to the AndroidManifest.xml. –  Neeko Jun 30 '12 at 19:06
@moonlightcheese Can you add code to come back to our app from call app? –  Geek Jul 15 '13 at 17:16

This is regarding the question asked by Starter.

The problem with your code is that you are not passing the number properly.

The code should be:

private OnClickListener next = new OnClickListener() {

     public void onClick(View v) {
        EditText num=(EditText)findViewById(R.id.EditText01); 
        String number = "tel:" + num.getText().toString().trim();
        Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(number)); 

Do not forget to add the permission in manifest file.

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


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

for emergency number in case DIAL is used.

share|improve this answer
I'm having a hard time to see how your code is different from the code in the original question –  Elijah Saounkine Aug 22 '11 at 21:35
The code is not different. You need to add the permission in the manifest file. –  Pria Mar 1 '12 at 12:08
android.permission.CALL_PRIVILEGED Permission is only granted to system apps not available at app level. –  Shubh Mar 2 at 11:25

We had the same problem and managed to solve it by using a PhoneStateListener to identify when the call ends, but additionally we had to finish() the original activity before starting it again with startActivity, otherwise the call log would be in front of it.

There's an example code demonstrating how it works here.

share|improve this answer
you can avoid this by using a different method. if you build a ContentObserver that observes the android call log, then the app will not start until the call log change is made. i actually had to dump the PhoneStateListener in favor of this model, as my app needed call log data, and the listener was returning before those changes were made. pastebin.com/bq2s9EVa –  moonlightcheese Sep 28 '11 at 15:48
@André: your link appears to be broken –  aggregate1166877 Oct 11 '12 at 12:28
I would give you a million reputation (if I had a lot:)) Thanks for making my day! –  keybee Aug 22 '13 at 18:06
The problem with links I say! –  Dheeraj Bhaskar Mar 20 at 19:38
the link please !!! –  S.Thiongane Apr 8 at 12:33

I found the EndCallListener the most functional example, to get the behaviour described (finish(), call, restart) I added a few SharedPreferences so the Listener had a reference to manage this behaviour.

My OnClick, initialise and EndCallListener only respond to calls from app. Other calls ignored.

import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

public class EndCallListener extends PhoneStateListener {

private String TAG ="EndCallListener";
private int     LAUNCHED = -1;

SharedPreferences prefs = PreferenceManager

SharedPreferences.Editor _ed = prefs.edit();

    public void onCallStateChanged(int state, String incomingNumber) {
    String _prefKey = myActivity.mApp                          
    _bPartyNumber = myActivity.mApp                           

    int mLastCallState = prefs.getInt(_prefKey, LAUNCHED);

    //Save current call sate for next call

        if(TelephonyManager.CALL_STATE_RINGING == state) {
            Log.i(TAG, " >> RINGING, number: " + incomingNumber);
        if(TelephonyManager.CALL_STATE_IDLE == state && mLastCallState != LAUNCHED ) {

            if (incomingNumber.equals(_bPartyNumber) == true) {
                //Call relates to last app initiated call
            Intent  _startMyActivity =  


                Log.i(TAG, "IDLE >> Starting MyActivity with intent");
                Log.i(TAG, "IDLE after calling "+incomingNumber);



add these to strings.xml

<string name="main_show_phone_call_list">android.intent.action.SHOW_PHONE_CALL_LIST</string>
<string name="last_phone_call_state_key">activityLpcsKey</string>
<string name="last_phone_call_bparty_key">activityLpbpKey</string>

and something like this in your Manifest if you need to return to the look and feel before the call

  <activity android:label="@string/app_name" android:name="com.myPackage.myActivity" 
        android:configChanges="keyboardHidden" >
            <action android:name="android.intent.action.MAIN" />
            <action android:name="android.intent.action.SHOW_PHONE_CALL_LIST" />
            <category android:name="android.intent.category.LAUNCHER" />

and put these in your 'myActivity'

public static Activity mApp=null; //Before onCreate()
onCreate( ... ) {
if (mApp == null) mApp = this; //Links your resources to other classes
    //Test if we've been called to show phone call list
    Intent _outcome = getIntent();
    String _phoneCallAction = mApp.getResources().getString(R.string.main_show_phone_call_list);
    String _reqAction = _outcome.getAction();//Can be null when no intent involved

         //Decide if we return to the Phone Call List view
         if (_reqAction != null &&_reqAction.equals(_phoneCallAction) == true) {
                         //DO something to return to look and feel

        myListView.setOnItemClickListener(new OnItemClickListener() { //Act on item when selected
             public void onItemClick(AdapterView<?> a, View v, int position, long id) {

                 String _bPartyNumber = "tel:"+myListView.getString(myListView.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 

                 //Provide an initial state for the listener to access.

                 //Setup the listener so we can restart myActivity
                    EndCallListener _callListener = new EndCallListener();
                    TelephonyManager _TM = (TelephonyManager)mApp.getSystemService(Context.TELEPHONY_SERVICE);

                    _TM.listen(_callListener, PhoneStateListener.LISTEN_CALL_STATE);

                         Intent _makeCall = new Intent(Intent.ACTION_CALL, Uri.parse(_bPartyNumber));

                 _makeCall.setComponent(new ComponentName("com.android.phone","com.android.phone.OutgoingCallBroadcaster"));
              //Wait for call to enter the IDLE state and then we will be recalled by _callListener

}//end of onCreate()

use this to initilaise the behaviour for your onClick in myActivity e.g. after onCreate()

private void initialiseCallStatePreferences(String _BParty) {
    final int LAUNCHED = -1;
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(
    SharedPreferences.Editor _ed = prefs.edit();

    String _prefKey = mApp.getString(R.string.last_phone_call_state_key),
           _bPartyKey = mApp.getString(R.string.last_phone_call_bparty_key);

    //Save default call state before next call


You should find that clicking your list of phone numbers finishes your activty, makes the call to the number and returns to your activty when the call ends.

Making a call from outside your app while it's still around won't restart your activty (unless it's the same as the last BParty number called).


share|improve this answer
Sorry but this code looks somewhat ugly. Thanks for the answer though –  Pierre Jan 9 at 19:46

you can use startActivityForResult()

share|improve this answer

If you are going to use a listener you will need to add this permission to the manifest as well.

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
share|improve this answer

This is solution from my point of view:

public void onClick(View view) {
	if(view == ok){
		Intent intent = new Intent(Intent.ACTION_CALL);
		intent.setData(Uri.parse("tel:" + num));


Of course in Activity (class) definition you have to implement View.OnClickListener .

share|improve this answer

Inside PhoneStateListener after seeing the call is finished better use:

Intent intent = new Intent(CallDispatcherActivity.this, CallDispatcherActivity.class);

Where CallDispatcherActivity is the activity where the user has launched a call (to a taxi service dispatcher, in my case). This just removes Android telephony app from the top, the user gets back instead of ugly code I saw here.

share|improve this answer
And don't forget to remove the listener after the phone call is done,like this: ((TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE)).listen(this, LISTEN_NONE); –  Dmitri Novikov Jun 28 '12 at 15:59
I've tried using your approach but the activity (called ControlPanel) is not being reactivated. The display continues to show the phone dialer interface and the loggers on the onResume and onNewIntent entry points within ControlPanel are completely silent. Here's the intent: Intent intentRestart = new Intent(ControlPanel.this, ControlPanel.class);. I should point out that the PhoneStateListener is also within ControlPanel. I.e., My goal is to restore the UI to the state it was in prior to initiating the phone call. Any suggestions? –  PeteH Apr 21 '13 at 5:39
Try logging inside your PhoneStateListener implementation. –  Dmitri Novikov Apr 22 '13 at 9:01

To return to your Activity, you will need to listen to TelephonyStates. On that listener you can send an Intent to re-open your Activity once the phone is idle.

At least thats how I will do it.

share|improve this answer
will you please share your code? –  Pierre Jan 9 at 19:41

Perfect tutorial here! Always check this blog because it has many excellent tutorials!

share|improve this answer

Try using:


at the end of activity. It will redirect you to your previous activity.

share|improve this answer

When PhoneStateListener is used, one need to make sure PHONE_STATE_IDLE following a PHONE_STATE_OFFHOOK is used to trigger the action to be done after the call. If the trigger happens upon seeing PHONE_STATE_IDLE, you will end up doing it before the call. Because you will see the state change PHONE_STATE_IDLE -> PHONE_STATE_OFFHOOK -> PHONE_STATE_IDLE.

share|improve this answer
isn't it possible the app stops when the ongoing call screen opens? –  lisovaccaro Jan 21 '13 at 19:30
Listener object once set to listen the TelephonyManager with ((TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE)).listen(new PhoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE) will continue to listen the phone state and be active until explicitly stopped with ((TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE)).listen(this, LISTEN_NONE) –  PonMaran Sep 14 at 4:42

Add this is your xml: android:autoLink="phone"

share|improve this answer
Please elaborate. "Which xml"? –  Anas Azeem Oct 19 '13 at 11:29

// in setonclicklistener put this code:

EditText et_number=(EditText)findViewById(R.id.id_of_edittext); 
String my_number = et_number.getText().toString().trim();
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(my_number)); 

// give permission for call in manifest:

share|improve this answer
  Intent callIntent = new Intent(Intent.ACTION_CALL);  

 **Add permission :**

 <uses-permission android:name="android.permission.CALL_PHONE" />          
share|improve this answer

@Dmitri Novikov, FLAG_ACTIVITY_CLEAR_TOP clears any active instance on top of the new one. So, it may end the old instance before it completes the process.

share|improve this answer

When starting your call, it looks fine.

There is a difference between android 11+ and down in bringing your app to the front though.

Android 10 or less you need to start a new intent, android 11+ you simply use BringTaskToFront

In the call state IDLE:

if (Build.VERSION.SDK_INT >= 11) {
    ActivityManager am = (ActivityManager) activity.getSystemService(Activity.ACTIVITY_SERVICE);
    am.moveTaskToFront(MyActivity.MyActivityTaskId, ActivityManager.MOVE_TASK_WITH_HOME);
} else {
    Intent intent = new Intent(activity, MyActivity.class);

I set the MyActivity.MyActivityTaskId when making the call on my activity like so, it this doesnt work, set this variable on the parent activity page of the page you want to get back to.

MyActivity.MyActivityTaskId = this.getTaskId();

MyActivityTaskId is a static variable on my activity class

public static int MyActivityTaskId = 0;

I hope this will work for you. I use the above code a bit differently, I open my app as soon as the call is answered sothat the user can see the details of the caller.

I have set some stuff in the AndroidManifest.xml as well:

/*Dont really know if this makes a difference*/
<activity android:name="MyActivity" android:taskAffinity="" android:launchMode="singleTask" />

and permissions:

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

Please ask questions if or when you get stuck.

share|improve this answer

protected by Brad Larson Jan 23 at 22:42


Would you like to answer one of these unanswered questions instead?
