run android application at a time -
i new in android.
i use bellow code run application @ special time. times when mobile phone locked , screen off , service stops , not work.
public class timeservice extends service { ibinder mbinder; timer t; public ibinder onbind(intent intent) { return mbinder; } public void ondestroy(){ t.cancel(); } public void ontaskremoved(intent rootintent){ intent restartserviceintent = new intent(getapplicationcontext(), this.getclass()); restartserviceintent.setpackage(getpackagename()); pendingintent restartservicependingintent = pendingintent.getservice(getapplicationcontext(), 1, restartserviceintent, pendingintent.flag_one_shot); alarmmanager alarmservice = (alarmmanager) getapplicationcontext().getsystemservice(context.a larm_service); alarmservice.set( alarmmanager.elapsed_realtime, systemclock.elapsedrealtime() + 1000, restartservicependingintent); super.ontaskremoved(rootintent); } public int onstartcommand(intent intent, int flags, int startid) { final dbhelper db=new dbhelper(this); int time[]=db.getalarmtime(); calendar c = calendar.getinstance(); dateformat dateformat = new simpledateformat("yyyy-mm-dd"); int hour = c.get(calendar.hour_of_day); int minute=c.get(calendar.minute); int rt=(time[0]*3600+time[1]*60)-(hour*3600+minute*60)*1000; t=new timer(); t.schedule( new timertask() { public void run() { //codes... stopservice(i); startservice(i); } },rt ); return start_sticky; } }
when device goes sleep state, code not called, suggest use alarmmanager handle code, instead of service. can put code inside receiver's onreceive method, , if need can acquire wakelock until code executed.
Comments
Post a Comment