android - Schedule several automatic tasks using AlarmManager and Calendar -


i have piece of code...

desc.setonclicklistener(new onclicklistener(){     public void onclick(view arg0){         alert.settitle("download");         alert.seticon(r.drawable.go_down_3_32x32);         alert.setmessage("¿donwload?");         alert.setpositivebutton("yes", new dialoginterface.onclicklistener(){             public void onclick(dialoginterface dialog, int id){                 asyncform2 af2 = new asyncform2();                 af2.execute(idu, mdbh);             }         });         alert.setnegativebutton("no", new dialoginterface.onclicklistener(){             public void onclick(dialoginterface dialog, int id){                 dialog.cancel();             }         });         alertdialog alert0 = alert.create();         alert0.show();     } }); 

where desc variable of type imageview, alert variable of type alertdialog.builder, idu integer , mdbh instance of databasehelper (my class handle sqlite database). have background asynctask...

class asyncform2 extends asynctask<object,string,string>{     int idu;     databasehelper db;     downloaddata dd;     protected void onpreexecute(){         pdialog2 = new progressdialog(screen2.this);         pdialog2.setmessage("loading...");         pdialog2.setindeterminate(false);         pdialog2.setcancelable(true);         pdialog2.show();     }     protected string doinbackground(object... params) {         idu = integer.parseint(params[0].tostring());         db = (databasehelper) params[1];         dd = new downloaddata(idu,db);         if(dd.download(idu) == true){             return "ok";         }else{             return "err";         }     }     protected void onpostexecute(string result){         if(result == "ok"){             pdialog2.dismiss();             toast.maketext(getapplicationcontext(), "success", toast.length_short).show();             setalarms();             toast.maketext(getapplicationcontext(), "alarms set!!", toast.length_short).show();         }else{             vibrator vibrator = (vibrator) getsystemservice(context.vibrator_service);             vibrator.vibrate(200);             toast.maketext(getapplicationcontext(), "error", toast.length_short).show();         }     } } 

with this, connection server set , data need app work downloaded... in method onpostexcecute method setalarms called , following:

private void setalarms(){     // todo auto-generated method stub     alarmmanager = (alarmmanager) getsystemservice(alarm_service);     intent intent = new intent(getapplicationcontext(), alarmreceiver.class);     intent.putextra("id_user", idu);     string name1,name2;     string splits[];     int namecolumnindex1, namecolumnindex2;     mdbh.open();     cursor itemcursor = null;     itemcursor = mdbh.getforms(idu);     if(itemcursor.movetofirst() == false){         itemcursor.close();         mdbh.close();     }else{         for(itemcursor.movetofirst();!itemcursor.isafterlast();itemcursor.movetonext()){             pendingintent sender = pendingintent.getbroadcast(screen.this, 0, intent, 0);             calendar cal = gregoriancalendar.getinstance();             namecolumnindex1 = itemcursor.getcolumnindexorthrow(databasehelper.form_sync_duration);             namecolumnindex2 = itemcursor.getcolumnindexorthrow(databasehelper.form_sync_hour);             name1 = itemcursor.getstring(namecolumnindex1);             name2 = itemcursor.getstring(namecolumnindex2);             splits = name2.split(":");             if(name1.equals("daily")){                 cal.set(calendar.hour_of_day, integer.parseint(splits[0]));                 cal.set(calendar.minute, integer.parseint(splits[1]));                 cal.set(calendar.second, integer.parseint(splits[2]));             }             if(name1.equals("weekly")){                 cal.set(calendar.hour_of_day, integer.parseint(splits[0]));                 cal.set(calendar.minute, integer.parseint(splits[1]));                 cal.set(calendar.second, integer.parseint(splits[2]));                 if(cal.get(calendar.day_of_month)+7 > cal.getactualmaximum(calendar.day_of_month)){                     cal.set(calendar.day_of_month, (cal.get(calendar.day_of_month)+7) - cal.getactualmaximum(calendar.day_of_month));                     if(cal.get(calendar.month) + 1 >= cal.getactualmaximum(calendar.month)+1){                         cal.set(calendar.month, (cal.get(calendar.month) + 1) - (cal.getactualmaximum(calendar.month)+1));                         cal.set(calendar.year, cal.get(calendar.year) + 1);                     }else{                         cal.set(calendar.month,cal.get(calendar.month)+1);                     }                 }else{                     cal.set(calendar.day_of_month, cal.get(calendar.day_of_month)+7);                     cal.set(calendar.month,cal.get(calendar.month)+1);                 }             }             if(name1.equals("fortnightly")){                 cal.set(calendar.hour_of_day, integer.parseint(splits[0]));                 cal.set(calendar.minute, integer.parseint(splits[1]));                 cal.set(calendar.second, integer.parseint(splits[2]));                 if(cal.get(calendar.day_of_month)+15 > cal.getactualmaximum(calendar.day_of_month)){                     cal.set(calendar.day_of_month, (cal.get(calendar.day_of_month)+15) - cal.getactualmaximum(calendar.day_of_month));                     if(cal.get(calendar.month) + 1 > cal.getactualmaximum(calendar.month)+1){                         cal.set(calendar.month, (cal.get(calendar.month) + 1) - (cal.getactualmaximum(calendar.month)+1));                         cal.set(calendar.year, cal.get(calendar.year) + 1);                     }else{                         cal.set(calendar.month, cal.get(calendar.month) + 1);                     }                 }else{                     cal.set(calendar.day_of_month, calendar.day_of_month+15);                     cal.set(calendar.month, cal.get(calendar.month) + 1);                 }             }             if(name1.equals("monthly")){                 cal.set(calendar.hour_of_day, integer.parseint(splits[0]));                 cal.set(calendar.minute, integer.parseint(splits[1]));                 cal.set(calendar.second, integer.parseint(splits[2]));                 if(cal.get(calendar.day_of_month) + cal.getactualmaximum(calendar.day_of_month) > cal.getactualmaximum(calendar.day_of_month)){                     cal.set(calendar.day_of_month, (cal.get(calendar.day_of_month) + cal.getactualmaximum(calendar.day_of_month)) - cal.getactualmaximum(calendar.day_of_month));                     if(cal.get(calendar.month) + 1 > cal.getactualmaximum(calendar.month)+1){                         cal.set(calendar.month, (cal.get(calendar.month) + 1) - (cal.getactualmaximum(calendar.month)+1));                         cal.set(calendar.year, cal.get(calendar.year) + 1);                     }else{                         cal.set(calendar.month, cal.get(calendar.month) + 1);                     }                 }             }             am.set(alarmmanager.rtc_wakeup, cal.gettimeinmillis(), sender);         }         itemcursor.close();         mdbh.cerrar();     } } 

as can see, set several alarms depending on value table of database, , regarding of that, alarms different form each other (daily, weekly, etc.). , alarmreceiver class follows:

public class alarmreceiver extends broadcastreceiver {     private progressdialog pdialog1;     private static databasehelper mdbh;     context context0;      @override     public void onreceive(context context, intent intent) {         try {             context0 = context;             bundle bundle = intent.getextras();             int idu = bundle.getint("id_user");             asyncform af2 = new asyncform();             af2.execute(idu, mdbh);         } catch (exception e) {             toast.maketext(context, "error", toast.length_short).show();             e.printstacktrace();         }     }      class asyncform extends asynctask<object,string,string>{         int idu;         databasehelper db;         senddata sd;         protected void onpreexecute(){             pdialog1 = new progressdialog(contexto);             pdialog1.setmessage("sending...");             pdialog1.setindeterminate(false);             pdialog1.setcancelable(true);             pdialog1.show();         }         @override         protected string doinbackground(object... params) {             idu = integer.parseint(params[0].tostring());             db = (databasehelper) params[1];             sd = new senddata(idu,mdbh);             if(sd.send(idu) == true){                 return "ok";             }else{                 return "err";             }         }         protected void onpostexecute(string result){             if(result == "ok"){                 pdialog1.dismiss();                 toast.maketext(contexto, "success", toast.length_short).show();             }else{                 toast.maketext(contexto, "error", toast.length_short).show();             }         }     } } 

as can see, whole thing, try send data automatically server (the responses of forms stored in database). reason when time comes (lets 12:00 noon) nothing happens. there no error in logcat, thats why don't know i'm doing wrong!! please help, don't know if problem when working calendar or if isn't, in fact, way of setting alarms...

greetings!

i didin't read whole code, set more 1 alarms must provide different pendingintent, otherwise every alarm overwrites previous one. can setting different requestcode:

pendingintent sender = pendingintent.getbroadcast(context, somevalue, intent, 0); 

for example, write

arraylist<pendingintent> array;  private void setalarms(){       //your stuff       array = new arraylist<pendingintent>();       for(int i=0;i<something;i++){         pendingintent sender = pendingintent.getbroadcast(context, i, intent, 0);         array.add(sender)          //here can set alarm     }   } 

remember if want later cancel alarms, have store pendingintents! (in list example)

to cancel

void cancelallalarms(){      if(!array.isempty()){          for(int i=0; i<array.size(); i++){             alarmmanager.cancel(array.get(i));         }          array.clear();       } } 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -