2

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)

Sufian
  • 6,405
  • 16
  • 66
  • 120
rayman
  • 20,786
  • 45
  • 148
  • 246

5 Answers5

9

No, it is not possible. It is also a bad idea.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • yes.. but iam not talking about regular app, i`am talking about some system which i "burn" in the phone system.. and i need this system to work as service all the time, and not letting the user disable that service.. i though about AlarmManager.. what do you think? thanks, ray. – rayman Apr 21 '10 at 10:53
  • 3
    It is not possible to have an everlasting Android service. You are welcome to write daemons in C/C++ as part of the firmware, and those are unkillable as far as I understand it, but you are better asking questions related to that on the official Android open source project Google Groups at http://source.android.com/discuss – CommonsWare Apr 21 '10 at 10:58
  • Hi @CommonsWare, is a good idea for example : chronic medication –  Oct 26 '15 at 23:25
  • And is possible with a timing infinit with alarmmanager (for example date 99/99/99999) if you close app (can you make another service) –  Oct 26 '15 at 23:27
  • @delive: The user can always force-stop your app, which among other things removes your scheduled alarms. – CommonsWare Oct 26 '15 at 23:28
5

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/.

Fedor
  • 43,261
  • 10
  • 79
  • 89
  • That will not survive a "task killer", at least for current versions of Android. The API the task killers exploit also wipe out a service's alarms. – CommonsWare Apr 21 '10 at 11:20
1

•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.)

Sushil
  • 8,250
  • 3
  • 39
  • 71
1

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

Isaac Sekamatte
  • 5,500
  • 1
  • 34
  • 40
0

See: http://developer.android.com/reference/android/app/Service.html#ProcessLifecycle

Rob Kent
  • 5,183
  • 4
  • 33
  • 54
  • I already have, but how would i keep my service safe, so third party appz like: appKiller, wont be able to kill it anyway? Tnanks, ray, – rayman Apr 21 '10 at 10:35
  • As stated above, you cannot guarantee that your service will always be around. What I did with one of mine was for the service to do its work in the background then set an alarm so that it is woken up when it next needs to work. The it shuts itself down. Otherwise register some filters for system events that your service needs to respond to. – Rob Kent Apr 22 '10 at 09:01