java - I am stuck in scheduling task through Alarm Manager -
i want schedule 10 alarm daily start , stop service. past alarm trigger, search lot on google did't correct solution. here code please check , me.
intent start_intent,stop_intent; list<calendar> calander_list; static pendingintent pendingintent; calendar[] startcal,stopcal; alarmmanager[] startalarm,stopalarm; pendingintent[] startintent,stopintent; sharedpreferences sp; random rd; context context;
global variable sp have stored time.
public alarm_schedul(context cont) { context = cont; rd = new random(); calander_list = new arraylist<calendar>(); startalarm=new alarmmanager[5]; stopalarm=new alarmmanager[5]; startcal=new calendar[5]; stopcal=new calendar[5]; startintent=new pendingintent[5]; stopintent=new pendingintent[5]; start_intent=new intent(context,alarm_reciver.class); start_intent.putextra("alarmtype","start"); stop_intent=new intent(context,alarm_reciver.class); stop_intent.putextra("alarmtype","stop"); sp = context.getsharedpreferences("salah", context.mode_private); setup_calander(); init(); }
alarm_schedul(context cont) class constructor.
public void setup_calander() { fajar(); zuhar(); ashar(); magrab(); ishia(); } public void fajar() { string time = "", h = "", m = ""; int index = 0; time = sp.getstring("fajar_from", "00:00"); index = time.indexof(":"); h = time.substring(0, index); m = time.substring(index + 1, time.length()); //set calander startcal[0]=calendar.getinstance(); startcal[0].settimeinmillis(system.currenttimemillis()); startcal[0].set(calendar.hour_of_day, integer.parseint(h)); startcal[0].set(calendar.minute, integer.parseint(m)); calander_list.add(startcal[0]); //==== time = sp.getstring("fajar_to", "00:00"); index = time.indexof(":"); h = time.substring(0, index); m = time.substring(index + 1, time.length()); stopcal[0]=calendar.getinstance(); stopcal[0].settimeinmillis(system.currenttimemillis()); stopcal[0].set(calendar.hour_of_day, integer.parseint(h)); stopcal[0].set(calendar.minute, integer.parseint(m)); } public void zuhar() { string time="",h="",m=""; int index=0; time=sp.getstring("zuhar_from", "00:00"); index=time.indexof(":"); h =time.substring(0, index); m=time.substring(index + 1, time.length()); startcal[1]=calendar.getinstance(); startcal[1].settimeinmillis(system.currenttimemillis()); startcal[1].set(calendar.hour_of_day, integer.parseint(h)); startcal[1].set(calendar.minute, integer.parseint(m)); calander_list.add(startcal[1]); //==== time=sp.getstring("zuhar_to", "00:00"); index=time.indexof(":"); h =time.substring(0, index); m=time.substring(index + 1, time.length()); stopcal[1]=calendar.getinstance(); stopcal[1].settimeinmillis(system.currenttimemillis()); stopcal[1].set(calendar.hour_of_day, integer.parseint(h)); stopcal[1].set(calendar.minute, integer.parseint(m)); } public void ashar() { string time="",h="",m=""; int index=0; time=sp.getstring("asar_from", "no values"); index=time.indexof(":"); h =time.substring(0, index); m=time.substring(index + 1, time.length()); startcal[2]=calendar.getinstance(); startcal[2].settimeinmillis(system.currenttimemillis()); startcal[2].set(calendar.hour_of_day, integer.parseint(h)); startcal[2].set(calendar.minute, integer.parseint(m)); calander_list.add(startcal[2]); //==== time=sp.getstring("asar_to", "no values"); index=time.indexof(":"); h =time.substring(0, index); m=time.substring(index + 1, time.length()); stopcal[2]=calendar.getinstance(); stopcal[2].settimeinmillis(system.currenttimemillis()); stopcal[2].set(calendar.hour_of_day, integer.parseint(h)); stopcal[2].set(calendar.minute, integer.parseint(m)); } public void magrab() { string time="",h="",m=""; int index=0; time=sp.getstring("magrab_from", "no values"); index=time.indexof(":"); h =time.substring(0, index); m=time.substring(index + 1, time.length()); startcal[3]=calendar.getinstance(); startcal[3].settimeinmillis(system.currenttimemillis()); startcal[3].set(calendar.hour_of_day, integer.parseint(h)); startcal[3].set(calendar.minute, integer.parseint(m)); calander_list.add(startcal[3]); //==== time=sp.getstring("magrab_to", "no values"); index=time.indexof(":"); h =time.substring(0, index); m=time.substring(index + 1, time.length()); stopcal[3]=calendar.getinstance(); stopcal[3].settimeinmillis(system.currenttimemillis()); stopcal[3].set(calendar.hour_of_day, integer.parseint(h)); stopcal[3].set(calendar.minute, integer.parseint(m)); } public void ishia() { string time="",h="",m=""; int index=0; time=sp.getstring("ishia_from", "no values"); index=time.indexof(":"); h =time.substring(0, index); m=time.substring(index + 1, time.length()); startcal[4]=calendar.getinstance(); startcal[4].settimeinmillis(system.currenttimemillis()); startcal[4].set(calendar.hour_of_day, integer.parseint(h)); startcal[4].set(calendar.minute, integer.parseint(m)); calander_list.add(startcal[4]); //==== time=sp.getstring("ishia_to", "no values"); index=time.indexof(":"); h =time.substring(0, index); m=time.substring(index + 1, time.length()); stopcal[4]=calendar.getinstance(); stopcal[4].settimeinmillis(system.currenttimemillis()); stopcal[4].set(calendar.hour_of_day, integer.parseint(h)); stopcal[4].set(calendar.minute, integer.parseint(m)); }
setup calender
public void init() { for(int i=0; i<5; i++) { startalarm[i]= (alarmmanager) context.getsystemservice(context.alarm_service); startintent[i]=pendingintent.getbroadcast(context,i,start_intent,pendingintent.flag_update_current); startalarm[i].setrepeating(alarmmanager.rtc_wakeup,startcal[i].gettimeinmillis(),alarmmanager.interval_day,startintent[i]); } for(int i=0; i<5; i++) { stopalarm[i]= (alarmmanager) context.getsystemservice(context.alarm_service); stopintent[i]=pendingintent.getbroadcast(context,i+5,stop_intent,pendingintent.flag_update_current); stopalarm[i].setrepeating(alarmmanager.rtc_wakeup,stopcal[i].gettimeinmillis(),alarmmanager.interval_day,stopintent[i]); } }
init()
method use initialize alarm manager.
Comments
Post a Comment