Tuesday, 15 July 2014

android - AlarmManager setRepeating not working with daily interval -



android - AlarmManager setRepeating not working with daily interval -

i encountered issue: trying setup daily notification

i used alarmmanager setup alarm @ 6:pm everyday

intent myintent = new intent(this, alarmreceiver.class); pendingintent = pendingintent.getbroadcast(this, 0, myintent, pendingintent.flag_update_current); alarmmanager alarmmanager = (alarmmanager) getsystemservice(alarm_service); calendar calendar = calendar.getinstance(); if (calendar.get(calendar.hour_of_day) >= 18) { calendar.add(calendar.date, 1); } calendar.set(calendar.minute, 0); calendar.set(calendar.second, 0); calendar.set(calendar.hour_of_day, 18); alarmmanager.cancel(pendingintent); alarmmanager.setrepeating(alarmmanager.rtc_wakeup, calendar.gettimeinmillis(), 24*60*60*1000, pendingintent);

but dont know why, first alarm fired @ exact 18:00, next alarm fire @ random time, time 19:30, time 20:06, dont understand i'm wrong here, tried both interval_day or 24*60*60*1000, nil worked, worked fine interval 1 minute, 5 minutes, 10 minutes, 1h

alarmreceiver:

public class alarmreceiver extends broadcastreceiver { @override public void onreceive(context context, intent intent) { intent service1 = new intent(context, myalarmservice.class); context.startservice(service1); system.out.println("alarm"); }

}

myalarmservice:

public class myalarmservice extends service { private notificationmanager mmanager; @override public ibinder onbind(intent arg0) { // todo auto-generated method stub homecoming null; } @override public void oncreate() { // todo auto-generated method stub super.oncreate(); } @suppresswarnings("static-access") @override public void onstart(intent intent, int startid) { super.onstart(intent, startid); mmanager = (notificationmanager) this.getapplicationcontext().getsystemservice(this.getapplicationcontext().notification_service); intent intent1 = new intent(this.getapplicationcontext(),mainactivity.class); notification notification = new notification(r.drawable.ic_launcher,"test notification", system.currenttimemillis()); intent1.addflags(intent.flag_activity_single_top| intent.flag_activity_clear_top); pendingintent pendingnotificationintent = pendingintent.getactivity( this.getapplicationcontext(),0, intent1,pendingintent.flag_update_current); notification.flags |= notification.flag_auto_cancel; notification.setlatesteventinfo(this.getapplicationcontext(), "test", "test notification", pendingnotificationintent); mmanager.notify(0, notification); } @override public void ondestroy() { // todo auto-generated method stub super.ondestroy(); } }

you have 2 problems:

setrepeating() inexact on android 4.4+ android:targetsdkversion of 19 or higher.

you not using wakefulbroadcastreceiver, wakefulintentservice, or own wakelock, , device can fall asleep between time alarm triggered time service completes work.

in case, can solve #2 easier moving code onstart() (which, btw, has been deprecated ~5 years) onreceive() , getting rid of service entirely. need service here if doing disk i/o or network i/o; raising notification should cheap.

android notifications alarmmanager

No comments:

Post a Comment