Aspen
Aspen

Reputation: 91

Android: how can I launch phone call in my activity, and then back to it?

I can launch the phone call by Intent:

    String url = "tel:3334444";
    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));

But it will stay in the phone call screen. What I want is staying at my app activity. Is it possible that launching the phone call in background? Or return to the previous activity immediately.

Upvotes: 4

Views: 4041

Answers (2)

Ranjit Mishra
Ranjit Mishra

Reputation: 450

You need to implement Phonecall state in side of your activity

// //Handling phone states

private PhoneStateListener phoneListener = new PhoneStateListener() {
      public void onCallStateChanged(int state, String incomingNumber) {
       try {
        switch (state) {
        case TelephonyManager.CALL_STATE_RINGING:
         Toast.makeText(activity.this, "CALL_STATE_RINGING", Toast.LENGTH_SHORT).show();
         mediaPlayer.pause();
         break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
        Toast.makeText(activity.this, "CALL_STATE_OFFHOOK", Toast.LENGTH_SHORT).show();
         mediaPlayer.pause();
         break;
        case TelephonyManager.CALL_STATE_IDLE:
        Toast.makeText(activity.this, "CALL_STATE_IDLE", Toast.LENGTH_SHORT).show();
         mediaPlayer.start();
         break;
        default:
        Toast.makeText(activity.this, "default", Toast.LENGTH_SHORT).show();

        }
       } catch (Exception e) {

       }
      }
     };  

}

Upvotes: 1

Philip Pearl
Philip Pearl

Reputation: 1533

In a background thread (probably needs to be in a foreground service) or on a regular (quickly) repeating alarm poll ActivityManager.getRunningTasks(). The first task is the top-most. Check the topActivity on this task to see if it is the InCallScreen (note on some Ericsson phones this is replaced with a custom class). If it is, bring your activity to the front.

You'll need to use TelephonyManager to watch for on-hook to stop your background thread or alarm if the call is abandoned.

Upvotes: 0

Related Questions