0

I am working on a app that during boot time starts an activity that logs in to my server (needs an activity to log in through facebook) using a service (initiated with startService). This service establishes XMPP listeners and does nothing after that, just waits for connection. I want this service to run all the time the device is up.

The problem is that the activity stops after a while and my service is also stopped. The service returns START_STICKY so I was expecting it to hang around. Also the service doesn't do anything except wait for connection.

The activity has the properties: android:excludeFromRecents="true" android:noHistory="true" android:launchMode="singleInstance"

so that it does not show up in the task list (when user long presses the home button).

The activity is stopped when the user long presses the home button and the service also ends. I am thinking its possible that the application exited, that's why the service also ends. I could not find any way to keep the activity from not stopping. Maybe its stopping because of the above properties.

So what can I do to keep the service running all the time. How can I keep the application from being removed. I read somewhere that if I keep a while loop running in the service then START_STICKY can keep the service around?? I can use AlarmManager to start the service but I don't want it to stop easily and then have to restart it every time.

I don't want to run a foreground service. I can not run the service in a different process since I am using existing code that does not do IPC. Any help will be appreciated. Thanks.

1 Answers1

0

There are two things to keep running a service indefinitely; create the service using startService() and return START_STICKY from onStartCommand(). You seem to be doing this both. With these two steps, the service may be shut down by the system but it should restart almost immediately.

The only suggestion I have is to create a separate thread in the service. This is because by default, started services run in the application main thread. If the service is constantly doing certain task, it may block the main thread and kill the application. Google doc has an example of implementing this:

http://developer.android.com/guide/components/services.html#ExtendingService

kmaini
  • 164
  • 5