How do I operate the Service all the time?
5 answers
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 to share
• 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 to share