android - Alarm Manager background services not working properly -
i working on project , making reminder app. made yesterday, problem facing. if destroy app, notification doesn't come anymore. if minimize app, works correctly. if set alarm 4-5 hours longer current time, not works/not shows notification
this main activity
public class setquiz extends appcompatactivity implements datepickerdialog.ondatesetlistener, timepickerdialog.ontimesetlistener{ toolbar toolbar; edittext title, details; button save, time; textview s_date, s_time; quizdatabasehelper mydb; string q_title, q_details, h, m, y, mo, d; int year, month, day, hour, minute; int yearfinal, monthfinal, dayfinal, hourfinal, minutefinal; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_set_quiz); mydb = new quizdatabasehelper(this); title = (edittext) findviewbyid(r.id.quiz_title); details = (edittext) findviewbyid(r.id.quiz_details); save = (button) findviewbyid(r.id.quiz_save); time = (button) findviewbyid(r.id.add_time); s_date = (textview) findviewbyid(r.id.show_date); s_time = (textview) findviewbyid(r.id.show_time); calendar c = calendar.getinstance(); string yy = string.valueof(c.get(calendar.year)); string mm = string.valueof(c.get(calendar.month)+1); string dd = string.valueof(c.get(calendar.day_of_month)); int hh = c.get(calendar.hour); int mmm = c.get(calendar.minute); string timeset = ""; if (hh > 12) { hh -= 12; timeset = "pm"; } else if (hh == 0) { hh += 12; timeset = "am"; } else if (hh == 12){ timeset = "pm"; }else{ timeset = "am"; } string min = ""; if (mmm < 10) min = "0" + mmm; else min = string.valueof(mmm); string hhh = string.valueof(hh); string mmmm = string.valueof(min); s_date.settext(yy+"-"+mm+"-"+dd); s_time.settext(hhh+"."+mmmm+" "+timeset); toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); getsupportactionbar().setdisplayhomeasupenabled(true); time.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { calendar c = calendar.getinstance(); year = c.get(calendar.year); month = c.get(calendar.month); day = c.get(calendar.day_of_month); datepickerdialog datepickerdialog = new datepickerdialog(setquiz.this,r.style.datepickerdialogtheme, setquiz.this, year,month, day); datepickerdialog.show(); } }); save.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { q_title = title.gettext().tostring(); q_details = details.gettext().tostring(); if(q_title.length() == 0 || q_details.length() == 0){ toast.maketext(getapplicationcontext(), "please fill empty field!", toast.length_short).show(); } else{ int alarm_id = mydb.insertdata(q_title, q_details, h, m, y, mo, d); if(integer.valueof(m) <=9){ m = "0"+m; } // code alarm manager calendar calendar2 = calendar.getinstance(); calendar2.set(calendar.hour_of_day, integer.parseint(h)); calendar2.set(calendar.minute, integer.parseint(m)); calendar2.set(calendar.year, integer.parseint(y)); calendar2.set(calendar.month, integer.parseint(mo)-1); calendar2.set(calendar.day_of_month, integer.parseint(d)-1); intent intent = new intent(getapplicationcontext(), notificationreceiver.class); intent.putextra("id", alarm_id); pendingintent pendingintent = pendingintent.getbroadcast(getapplicationcontext(), alarm_id, intent, pendingintent.flag_update_current); alarmmanager alarmmanager = (alarmmanager) getsystemservice(alarm_service); alarmmanager.setrepeating(alarmmanager.rtc_wakeup, calendar2.gettimeinmillis(), alarmmanager.interval_fifteen_minutes, pendingintent); // code alarm manager finish intent intent2 = new intent(setquiz.this, quizreminder.class); startactivity(intent2); finish(); } } }); } @override public void ondateset(datepicker view, int year, int month, int dayofmonth) { yearfinal = year; monthfinal = month + 1; dayfinal = dayofmonth; y = string.valueof(yearfinal); mo = string.valueof(monthfinal); d = string.valueof(dayfinal); s_date.settext(d+"-"+mo+"-"+y); calendar c = calendar.getinstance(); hour = c.get(calendar.hour_of_day); minute = c.get(calendar.minute); timepickerdialog timepickerdialog = new timepickerdialog( setquiz.this, r.style.timepickerdialogtheme, setquiz.this, hour, minute , false); timepickerdialog.show(); } @override public void ontimeset(timepicker view, int hourofday, int minute) { hourfinal = hourofday; minutefinal = minute; h = string.valueof(hourfinal); m = string.valueof(minutefinal); string timeset = ""; if (hourfinal > 12) { hourfinal -= 12; timeset = "pm"; } else if (hourfinal == 0) { hourfinal += 12; timeset = "am"; } else if (hourfinal == 12){ timeset = "pm"; }else{ timeset = "am"; } string min = ""; if (minutefinal < 10) min = "0" + minute ; else min = string.valueof(minutefinal); string hh = string.valueof(hourfinal); string mm = string.valueof(min); string final_time = hh + ":"+mm+" "+timeset; s_time.settext(final_time); } }
this receiver class
public class notificationreceiver extends broadcastreceiver{ quizdatabasehelper mydb; string title, details; @override public void onreceive(context context, intent intent) { int id= intent.getintextra("id", 1); mydb = new quizdatabasehelper(context); cursor data = mydb.getquiztitle(string.valueof(id)); while(data.movetonext()) { title = data.getstring(1); details = data.getstring(2); } notificationmanager notificationmanager = (notificationmanager) context.getsystemservice(context.notification_service); intent repeating_intent = new intent(context, quizreminder.class); repeating_intent.setflags(intent.flag_activity_clear_top); pendingintent pendingintent = pendingintent.getactivity(context, id , repeating_intent, pendingintent.flag_update_current); android.support.v4.app.notificationcompat.builder builder = new notificationcompat.builder(context) .setcontentintent(pendingintent) .setsmallicon(r.drawable.notepad) .setcontenttitle(" ' "+title+" ' "+" quiz tomorrow") .setdefaults(android.support.v4.app.notificationcompat.default_sound| notification.default_vibrate) .setcontenttext("syllabus: "+details) .setautocancel(true); notificationmanager.notify(id, builder.build()); } }
Comments
Post a Comment