0

I have the following code :

public static void dialNumber(Context context, String phoneNumber)
{
    _logger.info("Calling " + phoneNumber);    
    Intent intent = new Intent(ACTION_CALL, Uri.fromParts("tel", phoneNumber, null));

    context.startActivity(intent);
}

This starts a call to the given phoneNumber. I want to put some logic when this call finishes. how can i do that. How to know if the call finishes. I tried using startActivityForResult, but i cant call this function on a Context.

Thanks

grunk
  • 14,718
  • 15
  • 67
  • 108
rohit sharma
  • 171
  • 1
  • 9

2 Answers2

1

check BroadcastReceiver receiver and system's broadcast messages. This kind of events (starting call, ending call, etc) are usually sent by broadcast message. And, if you have receiver, you can get and do whatever you want. for instance, android.intent.action.PHONE_STATE might work for you. http://androidexample.com/Introduction_To_Broadcast_Receiver_Basics/index.php?view=article_discription&aid=60&aaid=85

I think you get get information that you want through intent.getExtras

    public void onReceive(Context context, Intent intent) {

            intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
            String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
            String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
            int state = 0;
            if(stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)){
                state = TelephonyManager.CALL_STATE_IDLE;
            }
            else if(stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
                state = TelephonyManager.CALL_STATE_OFFHOOK;
            }
            else if(stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)){
                state = TelephonyManager.CALL_STATE_RINGING;
            }

    }
Adem
  • 9,402
  • 9
  • 43
  • 58
  • Hi,Broadcast receiver seems to be good. But when i tried implementing it. The onReceive method gets called both on starting of the call and ending of the call. But i want to put my logic only inside ending of he call. Also this will get called on all calls thru my phone. But i want to intercept it only for one particular call. – rohit sharma Nov 28 '14 at 08:51
  • Check "Extras" of intent. There are some helpful information. I updated my answer – Adem Nov 28 '14 at 13:20
  • Hi, Thanks for help. This does solve my initial question. But i want to add something more. I had an Activity A which started the phone call activity and on finishing of the call activity now i am inside onReceive method. As Activity A is now resumed after the call i want to call a method of Activity A from inside of onReceive() method. How can i do this ? Thanks – rohit sharma Nov 30 '14 at 10:59
  • you should save state of Activity. then relaunch it. check onSaveInstanceState and onRestoreInstanceState – Adem Nov 30 '14 at 12:56
  • Hi, Unable to understand how onSaveInstanceState will help in calling method of the Activity from within onReceive() method. As i read onSaveInstanceState is used when the Activity gets killed. In this case the Activity may not necessary be killed. – rohit sharma Nov 30 '14 at 16:38
  • sorry, I thought activity is died. but still, you should save your activity's state and reload with onSaveInstanceState. it is not guaranteed to activity live until call finishes. if lets assume it becomes alive then you can use singleInstance option in your activity and start activity in regular way, like context.startActivity. to set singleInstance, open AndroidManifest.xml and add android:launchMode="singleInstance" into your activity tag. with this option, when you start activity, previous one will be populated – Adem Nov 30 '14 at 21:30
0

Maybe this is worth checking out. You can use startActivityForResult to get some kinda feedback from a started activity once it is done doing things.

Link to solved question

Community
  • 1
  • 1
cgew85
  • 427
  • 3
  • 9