2

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?

NickUnuchek
  • 11,794
  • 12
  • 98
  • 138
  • **You are not supposed to be able to restart** after the user has force stopped you, as that is considered to be an expression of the user's will which outweighs your desire as a developer. – Chris Stratton Jan 30 '14 at 17:38
  • 1
    @Nick Check this blog http://android-developers.blogspot.in/2010/04/multitasking-android-way.html – Zohra Khan Jan 31 '14 at 18:10

2 Answers2

2

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.

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Braunster
  • 466
  • 4
  • 6
2

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.

Zohra Khan
  • 5,182
  • 4
  • 25
  • 34
  • 2
    This will not work on recent Android versions. Once the user has force stopped the app, broadcast receivers are disabled until the user chooses to manually relaunch it. – Chris Stratton Jan 30 '14 at 17:39
  • @Chris Please check my edited answer. Thanks Chris for correcting me. – Zohra Khan Jan 31 '14 at 06:41
  • If the poster is accurate in referring to the force stop of the application, then this would not work unless you have some other app to send the broadcast from. – Chris Stratton Jan 31 '14 at 07:09