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
.
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.