4

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Aspen
  • 91
  • 2
  • 7
  • I would argue this isn't really how things are supposed to work in android. Don't try to do this. You're bucking the system and trying to bend it and make it do things it's not supposed to. – Kurtis Nusbaum Nov 10 '11 at 03:55
  • 2
    @KurtisNusbaum: why is that so? I can think of at least two or three legitimate reasons why I would like to start a call in the background so as to not disturb whatever I'm doing inside a task, mainly if paired with a call using the speaker. I understand the concern for security and scam dialers, but still... the call is always accessible in the notification bar. It's two touches away from the user (and that's why I had my previous answer that I *rightfully* deleted). Perhaps I'm being naive, but I'm being honest: if why, then why not? – davidcesarino Nov 10 '11 at 06:14
  • @David, I forgot that the call gets put up in the notification bar which does make it accessible to the user. So point well taken. I guess this is ok to do then. – Kurtis Nusbaum Nov 10 '11 at 16:25
  • Yes, that's what I thought. Anyway, that answer (mine) didn't work, at least not for the dialer. Searching more I've found this: http://stackoverflow.com/questions/3401840/android-how-to-start-an-activity-in-the-background/3401855#3401855 The last chance for this question would be if Android implements a way to dial without bringing the Dialer activity to the front. But I'm starting to doubt because you wouldn't see whom the app is calling. Truth is, after everything is said and done, this may indeed be better left alone. – davidcesarino Nov 10 '11 at 18:28

2 Answers2

1

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) {

       }
      }
     };  

}

Ranjit Mishra
  • 440
  • 4
  • 8
0

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.

Philip Pearl
  • 1,523
  • 16
  • 26