android - How to match a string to the device time -
when device time between or equal starting time , ending time, want show me chogadianame
. method i'm fetching time:
/*{"chogadia":[{"chogadianame":1,"starttime":"5:31:19 am","endtime":"7:14:15 am","effect":"inauspicious chogadia"},{"chogadianame":3,"starttime":"8:57:10 am","endtime":"10:40:5 am","effect":"auspicious chogadia"},{"chogadianame":4,"starttime":"10:40:5 am","endtime":"12:22:59 am","effect":"auspicious chogadia"}]}*/ public class chogadiaparser { public static arraylist<chogadia> mlist=new arraylist<chogadia>(); public static chogadia mchogadia; public static string response,chogadia; public static string lucky="auspicious chogadia"; public static string unlucky="inauspicious chogadia"; public static void groupresult(string url){ try{ jsonarray jarray; jsonobject jobject; response=getjsonobject.sendrequest(url); if(response == null){ return; } jobject=new jsonobject(response); jarray=jobject.getjsonarray("chogadia"); mlist.clear(); for(int i=0;i<jarray.length();i++){ mchogadia=new chogadia(); jobject=jarray.getjsonobject(i); mchogadia.setchogadianame(jobject.getstring("chogadianame")); mchogadia.setstarttime(jobject.getstring("starttime")); mchogadia.setendtime(jobject.getstring("endtime")); mchogadia.seteffect(jobject.getstring("effect")); mlist.add(mchogadia); if(mathctime(jobject.getstring("starttime"),jobject.getstring("endtime"))){ system.out.println("matched name is: " + jobject.getstring("chogadianame")); break; // break loop } } }catch(exception e){ e.printstacktrace(); } } private static boolean mathctime(string stime,string etime) { simpledateformat ft = new simpledateformat("hh:mm:ss"); try { date ct = new date(); date st = ft.parse(stime); date et=ft.parse(etime);; long currenttime = ((ct.gethours()*60)*60) + (ct.getminutes()*60) + (ct.getseconds()); long starttime = ((st.gethours()*60)*60) + (st.getminutes()*60) + (st.getseconds()); long endtime = ((et.gethours()*60)*60) + (et.getminutes()*60) + (et.getseconds()); if(currenttime>=starttime || currenttime<=endtime){ return true; }else{ return false; } } catch (exception e) { } return false; } }
you should use timertask in service
the timertask class represents task run @ specified time. task may run once or repeatedly.
or can use broadcastreceiver watches intent.action_time_tick. android beam intent.action_time_tick every minute. can catch in receiver , want.
broadcast action: current time has changed. sent every minute. can not receive through components declared in manifests, exlicitly registering context.registerreceiver().
also, in code
currenttime>=starttime || currenttime<=endtime
should be
currenttime>=starttime && currenttime<=endtime
Comments
Post a Comment