Reputation: 21616
I have some process that I would like to run in the background the whole time.
So I made it a Service
.
My question is: is there anyway to prevent from the user from killing this service? (even if he uses third party app)
Upvotes: 3
Views: 7236
Reputation: 5608
Send a broadcast in the onDestory method of the service, restart the service in the onReceive method the the broadcast receiver. But make sure the resources your service are under control
Upvotes: 1
Reputation: 8488
•A started service can use the startForeground(int, Notification) API to put the service in a foreground state, where the system considers it to be something the user is actively aware of and thus not a candidate for killing when low on memory. (It is still theoretically possible for the service to be killed under extreme memory pressure from the current foreground application, but in practice this should not be a concern.)
Upvotes: 1
Reputation: 43422
Maybe this can help you:
final Intent intent = new Intent(context, YourService.class);
final PendingIntent pending = PendingIntent.getService(context, 0, intent, 0);
final AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarm.cancel(pending);
long interval = 30000;//milliseconds
alarm.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),interval, pending);
This way your service will be receiving regular onStart events from AlarmManager. If your service is stopped it will be restarted. So you may say it will be running infinitely. More complete sample can be found in Photostream sample application http://code.google.com/p/apps-for-android/.
Upvotes: 5
Reputation: 5203
See: http://developer.android.com/reference/android/app/Service.html#ProcessLifecycle
Upvotes: 0