with a BroadCastReceiver, I execute a service at the smartphone boot:
public class BootReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, MyService.class);
context.startService(startServiceIntent);
}
}
MyService:
private Runnable myRunnable = new Runnable() {
public void run() {
parsing.cancel(true);
parsing = new Parsing();
parsing.execute();
handler.postDelayed(this, timeoutUpdate);
}
};
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
handler.removeCallbacks(myRunnable);
handler.postDelayed(myRunnable, 1000);
return Service.START_STICKY;
}
The service is really executed at the boot, but if I set a timeout of 1 hour between executions, service is not executed (maybe the system is killing it). Otherwise, if I set 60 sec between repetition, all works. How can I do it? Thanks.
You may run the service in the foreground using startForeground().
But bear in mind that a foreground service must provide a notification for the status bar (read here), and that the notification cannot be dismissed unless the service is either stopped or removed from the foreground.
Note: This still does not absolutely guarantee that the service won't be killed under extremely low memory conditions. It only makes it less likely to be killed.
OR
If you dont want to run the service in the foreground then you can run service in a periodic intervals using AlarmManager
Update
Cancel the registered event by using