Android is killing my service? -


with broadcastreceiver, execute service @ 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 executed @ boot, if set timeout of 1 hour between executions, service not executed (maybe system killing it). otherwise, if set 60 sec between repetition, works. how can it? thanks.

you may run service in foreground using startforeground().

a foreground service service that's considered user actively aware of , not candidate system kill when low on memory.

but bear in mind foreground service must provide notification status bar (read here), , notification cannot dismissed unless service either stopped or removed foreground.

note: still not absolutely guarantee service won't killed under extremely low memory conditions. makes less killed.

or

if dont want run service in foreground can run service in periodic intervals using alarmmanager

intent intent = new intent(context, myservice.class); pendingintent pintent = pendingintent.getservice(this, 0, intent, 0); alarmmanager alarm = (alarmmanager)context.getsystemservice(context.alarm_service); alarm.setrepeating(alarmmanager.rtc_wakeup, cal.gettimeinmillis(), alarmmanager.interval_hour, pintent); 

update

cancel registered event using

    intent intent = new intent(context, myservice.class);     pendingintent pintent = pendingintent.getservice(this, 0, intent, 0);     alarmmanager alarm =     (alarmmanager)context.getsystemservice(context.alarm_service);    alarmmanager.cancel(pintent); 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -