NickUnuchek
NickUnuchek

Reputation: 12857

How to restart Service after Force Stop of app

Here is my Service

public class SService extends Service {

      public void onCreate() {
        super.onCreate();

        someTask();
      }

      public int onStartCommand(Intent intent, int flags, int startId) {

        someTask();

        return START_STICKY;
      }


      public IBinder onBind(Intent intent) {

        return null;
      }

After force stop of app (Settings->Application->Force Stop App) my Service doesn't run. How to solve it?

Upvotes: 2

Views: 9852

Answers (2)

Braunster
Braunster

Reputation: 456

Are you sure that the service isn't restarting? The START_STICKY you put in the return should do the trick some time. It takes some time till the system restart it, you can put a log and wait to make sure it's getting restarted.

Upvotes: 2

Zohra Khan
Zohra Khan

Reputation: 5302

As per the Android document

Starting from Android 3.1, the system's package manager keeps track of applications that
are in a stopped state and provides a means of controlling their launch from background 
processes and other applications.

Note that an application's stopped state is not the same as an Activity's stopped
state. The system manages those two stopped states separately.

Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intents. It 
does this to prevent broadcasts from background services from inadvertently or unnecessarily
launching components of stoppped applications. A background service or application can 
override this behavior by adding the FLAG_INCLUDE_STOPPED_PACKAGES flag to broadcast intents
that should be allowed to activate stopped applications.

On Force stop of app, Android just kill the process ID. No warnings, callbacks are given to service/activities. As per the Android document, When the app is killed there are chances that it calls onPause().

When I tried in my app, even onPause() was not called. I think the only way is use to that intent flag and send it from another app as suggested by Chris.

Upvotes: 2

Related Questions