android - Services stops after some instance -
i implementing app kind of app tracker requires run service time in background. service called when application opened , stops when stopself() called. service consists of thread runs time. used run perfect. last few days stops after sometime. when coming app's ui after task service stops! can suggest me solution?
public int onstartcommand(intent intent, int flags, int startid) { log.e("service","started"); if(intent != null && intent.getaction() != null) { day=intent.getextras().getint("day")+5; toast.maketext(this, "day" +day, toast.length_long).show(); apps=intent.getstringarraylistextra("apps"); items=apps.size(); timer=intent.getintegerarraylistextra("timer"); run[] a=new run[items]; t=new thread[items]; for(int i=0;i<items;i++) { int sec=0; app=apps.get(i).tostring(); time=timer.get(i); a[i]=new run(app, time,sec); log.e("apptime",app+ " "+time); t[i]=new thread(a[i]); t[i].start(); } } return super.onstartcommand(intent, flags, startid); }
try implementing foreground service. foreground service
foreground service displays notification , never stopped until want.
implement code snippet in service's oncreate
notification notification = new notification(r.drawable.icon, gettext(r.string.ticker_text), system.currenttimemillis()); intent notificationintent = new intent(this, exampleactivity.class); pendingintent pendingintent = pendingintent.getactivity(this, 0, notificationintent, 0); notification.setlatesteventinfo(this, gettext(r.string.notification_title), gettext(r.string.notification_message), pendingintent); startforeground(ongoing_notification_id, notification);
Comments
Post a Comment