Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the proper way to stop a service running as foreground

I am trying to stop a service which is running as foreground service.

The current issue is that when I call stopService() the notification still stays.

So in my solution I have added a receiver which I am registering to inside the onCreate()

Inside the onReceive() method I call stopforeground(true) and it hides the notification. And then stopself() to stop the service.

Inside the onDestroy() I unregistered the receiver.

Is there a more proper way to handle this? because stopService() simply doesn't work.

@Override public void onDestroy(){   unregisterReceiver(receiver);   super.onDestroy(); } 
like image 981
user1940676 Avatar asked Dec 31 '13 12:12

user1940676


People also ask

How do you stop a foreground service?

To remove the service from the foreground, call stopForeground() . This method takes a boolean, which indicates whether to remove the status bar notification as well. Note that the service continues to run. If you stop the service while it's running in the foreground, its notification is removed.

How do I stop foreground notification from its own?

You should put some flag inside your loop/computation method and call " return; " , and than You may stop service by stopself() or just wait untill it finish itself.

How do I stop a service started?

A started service must manage its own lifecycle. That is, the system doesn't stop or destroy the service unless it must recover system memory and the service continues to run after onStartCommand() returns. The service must stop itself by calling stopSelf() , or another component can stop it by calling stopService() .

What does run foreground service mean?

Foreground services are an advanced Android concept which allows you to display notifications to your users when running long lived background tasks. The notification acts like any other notification, however it cannot be removed by the user and lives for the duration of the service.


2 Answers

From your activity call startService(intent) and pass it some data that will represent a key to stop the service.

From your service call stopForeground(true) and then stopSelf() right after it.

like image 60
Ilya Gazman Avatar answered Sep 19 '22 21:09

Ilya Gazman


to start and stop a foreground service from an activity use :

//start     Intent startIntent = new Intent(MainActivity.this, ForegroundService.class);     startIntent.setAction(Constants.ACTION.STARTFOREGROUND_ACTION);     startService(startIntent); //stop     Intent stopIntent = new Intent(MainActivity.this, ForegroundService.class);     stopIntent.setAction(Constants.ACTION.STOPFOREGROUND_ACTION);     startService(stopIntent); 

in your foreground service - use (at least) this code:

@Override  public int onStartCommand(Intent intent, int flags, int startId) {     if (intent.getAction().equals(Constants.ACTION.STARTFOREGROUND_ACTION)) {         Log.i(LOG_TAG, "Received Start Foreground Intent ");         // your start service code     }     else if (intent.getAction().equals( Constants.ACTION.STOPFOREGROUND_ACTION)) {         Log.i(LOG_TAG, "Received Stop Foreground Intent");     //your end servce code         stopForeground(true);         stopSelfResult(startId);     }     return START_STICKY; } 
like image 42
Elad Avatar answered Sep 23 '22 21:09

Elad