9

I am confused right now , about service concept of running and stopping:

what i want to do:

  • Start Location service at the very start of application.

  • Keep getting location updates and store them to shared preference

  • Stop the service in onDestroy of Application scope!

So far i have searched and studied we can only do following things with service(correct me if i'm wrong):

  • Stop the service automatically by binding it to related activities/fragments/views , when all of them destroyed service unbind itself automatically so we can call stopself method in unbind

  • return START_NOT_STICKY in onStartCommand to tell OS , don't recreate it , and create intent local service , after completion of some work it will destroy itself.

  • Stopping the service manually , by declaring it's intent in some kind of static scope and stopping the service in on onActivityDestroyed of Application class [I am not sure what will happen? , maybe service will destroy each time any activity will be destroyed ? or it will be destroyed only when overall application get's destroyed?]

Either way , i am bit confused and beat , been trying to adjust my Location service with given details for 2 days

Egor Neliuba
  • 14,784
  • 7
  • 59
  • 77
Zulqurnain Jutt
  • 1,083
  • 3
  • 15
  • 41

5 Answers5

4

If you start your Service using START_NOT_STICKY, then your app will kill your service once your entire application is closed from background i.e. you cleaned your app from home screen.

Here START_NOT_STICKY states that you need not recreate service in case it is been killed.

If this is not the case then you have to manually kill it by your self.

Like

Intent lintent = new Intent(context, LocationService.class);
context.stopService(lintent);

You can use this code at point where your application kills.

That's it. You are good to go with this.

AndroidHacker
  • 3,596
  • 1
  • 25
  • 45
3

First of all, launch the "LocationService" on your app start:

public class MyApp extends Application {

    private static final String TAG = "MyApp";

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "App started up");
        startService(new Intent(this, MyLocationService.class));
    }
}

Second : As you said, the Service should better run with the "START_NOT_STICKY" flag

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_NOT_STICKY;
    }

Thrid:

Once the system kills your app, the Service will automatically be killed, so no problems at all.

There is no onDestroy() method on the Application object, the only similar event is onTerminated() and it is not being launched on production devices.

onTerminate

Added in API level 1 void onTerminate () This method is for use in emulated process environments. It will never be called on a production Android device, where processes are removed by simply killing them; no user code (including this callback) is executed when doing so.

More information: https://developer.android.com/reference/android/app/Application.html#onTerminate()

Btw, If you want the MyLocationService to send updates of the location to your app (when it is open), you should consider to use Otto or EventBus (I recommend you this last one because of the simplicity to use it). You can even configure the @Suscriber to receive updates of old retrieved locations if you want.

zapotec
  • 2,628
  • 4
  • 31
  • 53
0

I will try to explain in a easiest way :) There are two type of service IntentService and Service IntentService when started will kill itself once it treated the content of it onHandleIntent method as for Service well this one will not end until you give it the command to do so even if your start it using the context of an activity. It will also stop when the application is stopped in an extreme case (by system (Settings/application/YourApp/stop app OR an app crash)

thunder413
  • 585
  • 3
  • 10
0

Easiest way is First of all start IntentService with AlarmManager at some repeating time (10 mintutes), then in onHandleIntent() get Location and store into preference.

No Need bind to your activity, the IntentService automatically stops itself after saved in preferences.

pavan kvch
  • 153
  • 1
  • 13
-3

Yes you can stop the service in onDestroy() of the activity:

@Override
public void onDestroy(){
Log.v("SERVICE","Service killed");
service.stop();
super.onDestroy();  

}