How do I operate the Service all the time?

I have some process that I would like to run in the background all the time.

So I did it Service

.

My question is, is there a way to prevent killing this service from the user? (even if he uses a third party app)

+2


a source to share


5 answers


No, it’s impossible . This is also a bad idea .



+9


a source


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 receive regular onStart events from AlarmManager. If your service is stopped, it will be restarted. Therefore, you can say that it will be infinite. A more complete sample can be found in the Photostream sample app http://code.google.com/p/apps-for-android/ .

+5


a source


• An initial service can use the startForeground (int, Notification) API to place the service in a foreground state where the system thinks it is something the user is actively aware of and therefore not a candidate for low memory kill. (It is theoretically possible for the service to be killed by extreme memory pressure from the current foreground application, but in practice this should not be a concern.)

+1


a source


Send the broadcast in the onDestory method of the service, restart the service in the onReceive method of the broadcast receiver. But make sure the resources that your service is under control.

+1


a source


0


a source







All Articles