android - Service crashes on 'death' of Activity -
i have activity starts service.
in activity:
startservice(new intent(this, myservice.class));
in service, onstart():
/* show notification */ int icon = r.drawable.icon; tickertext = getresources().getstring(r.string.app_name); long when = system.currenttimemillis(); contenttitle = getresources().getstring(r.string.app_name); contenttext = getresources().getstring(r.string.running); intent notificationintent = new intent(this, activityclass).setaction(intent.action_main).setflags(intent.flag_activity_clear_top | intent.flag_activity_single_top); contentintent = pendingintent.getactivity(this, 0, notificationintent, 0); notification = new notification(icon, tickertext, when); notification.setlatesteventinfo(this, contenttitle, contenttext, contentintent); notification.flags = notification.flag_ongoing_event; ((notificationmanager) getsystemservice(context.notification_service)).notify(0, notification); startforeground(0, notification);
the service has timer work every 3-4 minutes, , broadcasts info activity if possible. when activity closed, service should keep doing work.
when run app, , go homescreen, service keeps running. after while these logcat messages, , service stops:
07-03 16:55:09.440: info/activitymanager(583): process com.myapp (pid 11665) has died. 07-03 16:55:09.440: warn/activitymanager(583): scheduling restart of crashed service com.myapp/.myservice in 5000ms
how can prevent this?
when run app, , go homescreen, service keeps running. after while these logcat messages, , service stops... how can prevent this?
you don't, speaking.
services not designed run forever. many developers have started services , never stopped them. and, in case, no reason. having service hang around in memory watching clock tick wasteful, , particularly because developers things android "crashes" services after stick around long.
in case, please switch use alarmmanager
, in conjunction intentservice
, service shuts down on own accord when there no more work done.
Comments
Post a Comment