What is the proper way to stop a service running as foreground
- From your activity, call
startService(intent)
and pass it some data representing a key to stop the service. - From your service, call
stopForeground(true)
- and then
stopSelf()
right after it.
How to properly stop a foreground service?
if you want to stop your service when you are clearing your application from the recent task, you have to define an attribute stopWithTask
for service in the manifest file like this as shown below
<service
android:enabled="true"
android:name=".ExampleService"
android:exported="false"
android:stopWithTask="true" />
then you can override onTaskRemoved method in the service , this will be called when the application's task is cleared
@Override
public void onTaskRemoved(Intent rootIntent) {
System.out.println("onTaskRemoved called");
super.onTaskRemoved(rootIntent);
//do something you want
//stop service
this.stopSelf();
}
How to end a foreground service only when app has been closed
I've found a solution, that at least seems to work for my situation. It's also a simple fix. In the same class that I override OnStartCommand, I've also done an override of OnTaskRemoved which is fired when a task from the service has been removed by the user. So when a user swipes to close the app, OnTaskRemoved will be called, where you can handle closing the service.
public override void OnTaskRemoved(Intent rootIntent)
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.N)
{
StopForeground(StopForegroundFlags.Remove);
}
else
{
StopForeground(true);
}
StopSelf();
base.OnDestroy();
System.Diagnostics.Process.GetCurrentProcess().Kill();
}
How to stop a foreground service from the notification in android?
You don't need to create BroadCast
to stop Service
. Try this
private static final String ACTION_STOP_LISTEN = "action_stop_listen";
Intent intent = new Intent(this, ClosingBackGroundService.class);
intent.setAction(ACTION_STOP_LISTEN);
PendingIntent actionIntent = PendingIntent.getService(this, 123, intent, PendingIntent.FLAG_UPDATE_CURRENT);
addAction(R.mipmap.ic_launcher,"Close Services",actionIntent)
In onStartCommand
check your Intent
action
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null && ACTION_STOP_LISTEN.equals(intent.getAction())) {
stopForeground(true);
stopSelf();
return START_NOT_STICKY;
}
// your code
}
Related Topics
Just Upgraded My Os X to Yosemite, Android Device Chooser Not Showing Device - What to Do
Is There a Maximum Size to Android Internal Storage Allocated for an App
Combining Wrap_Content on Parent and Fill_Parent on Child
How to Play an Audio File on a Voice Call in Android
How to Call a Wcf Service Using Ksoap2 on Android
Get The Displayed Size of an Image Inside an Imageview
How to Get Total Ram Size of a Device
How to Force Audio Through The Speakers When Headphones Are Plugged In
How to Put My Libraries in Front of Android.Jar by Editing Build.Gradle in Android-Studio
Detecting The Scrolling Direction in The Adapter (Up/Down)
Findbugs Idea - Classnotfoundexception Com.Google.Wireless.Android.Sdk.Stats.Intellijindexingstats
How to Measure Upload/Download Speed and Latency in Android Wifi Connection
Reasons That The Passed Intent Would Be Null in Onstartcommand
Switch Back/Front Camera on Fly
How to Make Space Between Linearlayout Children
How to Remove Black Background Between Start New Activity During Slide_Left Animation
How to Get/Set Media Volume (Not Ringtone Volume) in Android