0

I am trying to build an app that can be used for calling. My CallActivity is declared singleTop in the manifest file. I have a foreground service (CallService) which is started as soon as the app goes to the background while the user is on a call, since the device must not sleep during a call.

The notification for my CallService allows the user to either resume the call or hangup. My goal is to have the user press a button on the notification and hangup the ongoing call without bringing the app to the foreground.

I have tried using PendingIntent.getActivity() to start the CallActivity once the app is in background, from the CallService. But I have not been able to hangup the call yet. Here is some code...

 Intent returnToCallIntent = new Intent(this, CallActivity.class);
 PendingIntent returnPendingIntent = PendingIntent.getActivity(this, 0, returnToCallIntent, 0);

 Intent hangUpCallIntent = new Intent(this, CallActivity.class);
 hangUpCallIntent.putExtra("ACTION_FINISH_ACTIVITY", true);
 PendingIntent hangUpPendingIntent = PendingIntent.getActivity(this, 0, hangUpCallIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Right now both pending intents resolve to the same action which is hanging up the call while bring the app to the foreground. I figured out that this is happening because the 2 intents only differ in their extras and hence android does not distinguish them, i.e. intent#filterEquals() does not see any difference between them.

But the more important question is how can I finish() the CallActivity and have it pop off the backstack silently, without bringing it to the foreground. Also, after the CallActivity has been stopped, I need to stop the CallService in the background. So when the user taps the app in the recents screen, she/he should see the activity which was prior to the CallActivity on the backstack.

PS: Logic to hang up the call has been done in onNewIntent() method in CallActivity.

  • `System.exit(1);` See [How to exit an Android app programmatically?](https://stackoverflow.com/questions/17719634/how-to-exit-an-android-app-programmatically) – Jon Goodwin Mar 25 '19 at 01:57
  • Thanks @JonGoodwin but `System.exit()` seems to be a bit harsh. I tried the solution suggested by @David Wasser below and it seems to work well. – pendingIntent Mar 25 '19 at 14:45
  • `System.exit()` is definitely not what you want. Android interprets that as "your app crashed". – David Wasser Mar 25 '19 at 15:27

1 Answers1

1

You can have your Activity register an anonymous BroadcastReceiver that listens for a specific broadcast Intent. When your Service wants to finish the Activity, it can just send the broadcast Intent that the Activity is listening for.

In onReceive() of the BroadcastReceiver, just call finish(). This won't bring the Activity to the foreground.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • 1
    Thank you @David Wasser. The solution works really well. Unfortunately my upvote will not reflect until I get more reputation :( – pendingIntent Mar 25 '19 at 14:48
  • You can accept the answer by clicking the green checkmark next to the answer. That will help others with a similar problem. It will also get the question off the list of unanswered questions, as well as giving me some reputation points. – David Wasser Mar 25 '19 at 15:26