Well, it would sure help if you didn't assassinate the process the
service is running in! You do not want to call System.exit().

Now, one thing to realize about Android is that processes are killed
when not needed -- or sometimes even when needed -- and then restarted
later, re-creating the object as needed. Since Android components
(services, activities, etc.) share the same process, it's up to
Android to figure out when it's appropriate to kill the process;
that's why you should not call System.exit().;

If you'd like a service to get recreated, you'll need to start the
service using Context.startService(Intent) , as you appear to be
doing, and then in the service's onStartCommand(Intent, int, int)
method, return Service.START_STICKY, or Service.START_REDLIVER_INTENT.
Then so long as the service hasn't been stopped, if the process with
the service gets killed or crashes, it will be restarted by the
system.

Now, you say you want to make sure the service runs indefinitely. I
hope you don't mean that too literally -- as in keeping the CPU
running. Your service should only actually run in response to specific
events. These can include timer events, but you want to keep any
activity to a minimum.

In fact, you'd like to have the service stop, and the process be
allowed to be deleted, if there's a way. If there's a broadcast intent
that you can listen for, you can set up a broadcast receiver, and
start your service from that, and then when the work is done, have the
service stop itself. This will reduce how much impact your service has
on the system and make your application a lot more friendly.

Unfortunately, that's not always an option.

On Apr 8, 11:16 pm, Deva <[email protected]> wrote:
> Hi All,
> I'm trying to create a service that can run in backgroud. To start
> this service i'm using a activity to invoke startservice() API. This
> will start the service but when i exit the activity by invoking
> finish() and system.exit(), I see that service created/started by the
> activity is also terminated. How can i make sure service runs
> indefinitely even after terminating activity that started the service?
>
> Regards,
> Deva Kumar S

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

To unsubscribe, reply using "remove me" as the subject.

Reply via email to