android - SMS scheduler not accepting multiple alarms -
i'm developing sms scheduler app. here user can set time, number , msg. code works fine if there 1 message need schedule. if want have multiple schedules, wont possible new 1 replaces old.
the technique i'm using creating array of pending intents different request codes suggested other posts read, new schedule replaces old one.
below code:
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_zax_sms_scheduler); button dateset=(button) findviewbyid(r.id.datesetbtn); button timeset=(button) findviewbyid(r.id.timesetbtn); dateset.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { datepickerdialog.ondatesetlistener d=new datepickerdialog.ondatesetlistener() { @override public void ondateset(datepicker view, int year, int monthodyear, int dayofmonth) { // todo auto-generated method stub mmonth=monthodyear; myear=year; mday=dayofmonth; toast.maketext(getbasecontext(), "date set :"+mday+"/"+(mmonth+1)+"/"+myear, toast.length_short).show(); } }; // todo auto-generated method stub new datepickerdialog(zaxsmsscheduler.this,d,calendar.year,calendar.month,calendar.day_of_month).show(); } }); timeset.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { // todo auto-generated method stub timepickerdialog.ontimesetlistener t=new timepickerdialog.ontimesetlistener() { @override public void ontimeset(timepicker view2, int hour, int min) { // todo auto-generated method stub mhour=hour; mmin=min; if(mhour>=12) mhour=mhour-12; log.d("myapp", "hh:"+mhour+"\nmm:"+mmin); toast.maketext(getbasecontext(), "time set is:"+mhour+":"+mmin+":00", toast.length_short).show(); } }; calendar cal=calendar.getinstance(); new timepickerdialog(zaxsmsscheduler.this,t,cal.get(calendar.hour_of_day),cal.get(calendar.minute),true).show(); } }); button saveandclearbtn=(button) findviewbyid(r.id.savebtn); saveandclearbtn.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { // todo auto-generated method stub calendar mycal=calendar.getinstance(); long timetotrigger; /*mycal.set(calendar.day_of_month, mday); mycal.set(calendar.month,mmonth); mycal.set(calendar.year, myear);*/ mycal.set(calendar.hour, mhour); mycal.set(calendar.minute, mmin); mycal.set(calendar.second, 0); long settime=mycal.gettimeinmillis(); if(settime>system.currenttimemillis()) { timetotrigger=settime-system.currenttimemillis(); edittext edt1=(edittext) findviewbyid(r.id.edittext1); edittext edt2=(edittext) findviewbyid(r.id.edittext2); msg=edt2.gettext().tostring(); telno=edt1.gettext().tostring(); sqlitedatabase db=openorcreatedatabase("mydb", mode_private, null); db.execsql("create table if not exists mysmsscheduler(slno varchar,number varchar,msg varchar);"); string s="insert mysmsscheduler values ('"+count+"','"+telno+"','"+msg+"');"; db.execsql(s); log.d("myrec", "insertion of data successfull"); db.close(); edt1.settext(""); edt2.settext(""); intent intent = new intent(); intent.setclass(zaxsmsscheduler.this, mybroadcastreceiver.class); bundle b = new bundle(); b.putstring("index", integer.tostring(count)); intent.putextras(b); pendingintent pendingintent = pendingintent.getbroadcast(zaxsmsscheduler.this, (int)math.random(), intent, 0); intentarray.add(pendingintent); alarmmanager alarmmanager = (alarmmanager) getsystemservice(alarm_service); alarmmanager.set(alarmmanager.rtc_wakeup, system.currenttimemillis() + timetotrigger, intentarray.get(count)); count++; toast.maketext(getbasecontext(), "sms scheduled after:"+(timetotrigger/1000)+" sec.", toast.length_short).show(); log.d("myapp", "set time:"+(settime/1000)+"sec. \n"+"cur time:"+system.currenttimemillis()/1000); log.d("myapp", "time trigger:"+(timetotrigger/1000)+"sec."); } else { toast.maketext(getbasecontext(), "please enter valid time", toast.length_short).show(); calendar calendar =calendar.getinstance(); int h=calendar.get(calendar.hour); int m=calendar.get(calendar.minute); log.d("myapp", "cur hh:"+h+"\ncur mm:"+m); } } }); }
the code broadcast receiver is:
@override public void onreceive(context arg0, intent intent) { // todo auto-generated method stub int mycount; string cnt=intent.getstringextra("index"); if(cnt==null) log.d("myapp","data not received"); log.d("myapp", "count:"+cnt); mycount=integer.parseint(cnt); log.d("myapp","broadcast receiver called..."); sqlitedatabase db=arg0.openorcreatedatabase("mydb",context.mode_private, null); cursor c=db.rawquery("select number, msg mysmsscheduler slno=="+mycount, null); log.d("myapp", "cursor reference obtained..."); if(c!=null) { c.movetofirst(); } else log.d("myapp", "cursor null"); /* c.movetofirst();*/ string num=c.getstring(c.getcolumnindex("number")); string mymsg=c.getstring(c.getcolumnindex("msg")); log.d("myapp", "number:"+num+"\nmsg:"+mymsg); smsmanager sm = smsmanager.getdefault(); sm.sendtextmessage(num, null, mymsg, null, null); log.d("myapp", "message sent"); toast.maketext(arg0, "msg sent successfully", toast.length_long).show(); string table="mysmsscheduler"; string whereclause = "slno = ?"; string[] whereargs = new string[] { integer.tostring(zaxsmsscheduler.count) }; db.delete(table, whereclause, whereargs); db.close(); log.d("myapp", "entry deleted.."); //sm.sendtextmessage(zaxsmsscheduler.telno, null, zaxsmsscheduler.msg, null, null); }
i have set required permissions in manifest , have registered broadcast receiver. kindly request provide valuable suggestions solve same.
thanks in advance.
use different notification id multiple alarms
@override public void onreceive(context c, intent i) { int icon = r.drawable.ic_launcher; long when = system.currenttimemillis(); bundle bundle = i.getextras(); string title = bundle.getstring("title"); string text = bundle.getstring("text"); string billno = bundle.getstring("billno"); int id = bundle.getint("id"); toast.maketext(c, text, toast.length_long).show(); final int notif_id = id; // final int notif_id = 1; notificationmanager notofmanager = (notificationmanager) c .getsystemservice(context.notification_service); intent notificationintent = new intent(c, ayyappagoldactivity.class); //notificationintent.putextra("billno", billno); pendingintent contentintent = pendingintent.getactivity(c, 0, notificationintent, 0); notification notification = new notification(icon, title, when); notification.setlatesteventinfo(c, title, text, contentintent); //notification.flags = notification.flag_insistent; notification.defaults |= notification.default_sound; notification.flags |= notification.flag_auto_cancel; notofmanager.notify(notif_id, notification); } }
code setalarm;
int notif_id = (int) cal.gettimeinmillis(); notif_id++; intent intent = new intent(getapplicationcontext(), alarmactivity.class); intent.putextra("title", title); intent.putextra("text", text); intent.putextra("billno", billno); intent.putextra("id", notif_id); pendingintent pendingintent = pendingintent.getbroadcast( getapplicationcontext(), notif_id, intent, pendingintent.flag_update_current); @suppresswarnings("static-access") alarmmanager alarmmanager = (alarmmanager) getapplicationcontext() .getsystemservice(getapplicationcontext().alarm_service); alarmmanager.cancel(pendingintent); // cancel existing alarms alarmmanager.set(alarmmanager.rtc_wakeup, cal.gettimeinmillis(), pendingintent);
Comments
Post a Comment