1

I make some researsh and I found that we can make a service running in bakground with Android services.

Now I want to keep the service running even if I close my main application (and remove it from the recent apps list). And then if I want to see the service status, I have to reopen my application and check the status of the background service

1) Is it possible to do that in Android

2) If yes How I can do that? I Just need a global steps. No need for details.

Please note theat my background service should access to networks and disks

MOHAMED
  • 41,599
  • 58
  • 163
  • 268

2 Answers2

2

You can override Service.onStartCommand() callback and the key part here is a new result code returned by the function, telling the system what it should do with the service if its process is killed while it is running:

START_STICKY if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), then leave it in the started state but don't retain this delivered intent. Later the system will try to re-create the service. Because it is in the started state, it will guarantee to call onStartCommand(Intent, int, int) after creating the new service instance; if there are not any pending start commands to be delivered to the service, it will be called with a null intent object.

just this code snippet

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // We want this service to continue running until it is explicitly
    // stopped, so return sticky.
    return START_STICKY;
}

Check this service api changes especially the section of Service lifecycle changes.

Ahmed Hegazy
  • 12,395
  • 5
  • 41
  • 64
1
1) Is it possible to do that in Android

After working on Android background service for years, in my opinion the answer for your 1st question is YES.

2) If yes How I can do that? I Just need a global steps. No need for details.

I would recommend you to run a service and return START_STICKY from onStartCommand(Intent, int, int). Doing this will make sure that your service is recreated only if it was KILLED by the SYSTEM (i.e. Android OS). I have seen, developers suggesting to use a seperate Alarm to periodically check the service status and re-run the service if it has been stopped by some reason.

But, users have control on their device and they can stop the service anytime. This DOESN'T mean that if the user close the application and remove it from the recent apps list, then the service will be KILLED. The service is destroyed only when the user Forcce Stop the app from Application Manager. In such a case, your service cannot be started back untill the app is Launched again by the user and also the Alarms are stopped by the system.

Hope it helps!!

abhi
  • 1,412
  • 19
  • 25
  • Also, try to start the service in separate thread, so that when you will destroy your activity the service will not be affected. It will run without any interruption. Check this answer: http://stackoverflow.com/questions/9177212/creating-background-service-in-android/13263052#13263052. – abhi Dec 25 '14 at 23:31