android - widget AlarmManager repeating too fast -
i have widget shows data usage, have created preferenceactivity user can choose various update frequencies.. chosen freq value sharedpreferences , start alarm weird result. here appwidgetprovider:
private static pendingintent pendingintent = null; ... public void onreceive(context context, intent intent) { log.d("tag", "receive"); super.onreceive(context, intent); if (clock_widget_update.equals(intent.getaction())) { componentname thisappwidget = new componentname(context.getpackagename(), getclass().getname()); appwidgetmanager appwidgetmanager = appwidgetmanager.getinstance(context); int ids[] = appwidgetmanager.getappwidgetids(thisappwidget); (int appwidgetid : ids) { updateappwidget(context, appwidgetmanager, appwidgetid); } } } @override public void onupdate(context context, appwidgetmanager appwidgetmanager, int[] appwidgetids) { log.d("tag", "update"); super.onupdate(context, appwidgetmanager, appwidgetids); (int = 0; < appwidgetids.length; i++) { int appwidgetid = appwidgetids[i]; updateappwidget(context, appwidgetmanager, appwidgetid); } } public static void updateappwidget(context context, appwidgetmanager appwidgetmanager, int appwidgetid) { // starting timer alarmmanager alarmmanager = (alarmmanager) context.getsystemservice(context.alarm_service); alarmmanager.cancel(pendingintent); intent intent = new intent(clock_widget_update); pendingintent = pendingintent.getbroadcast(context, 0, intent, pendingintent.flag_update_current); sharedpreferences prefs = context.getsharedpreferences(integer.tostring(appwidgetid), context.mode_private); int updatefrequencymin = integer.parseint(prefs.getstring(key_update_freq, default_update_freq)); log.d("tag", "update freq: " + updatefrequencymin); long updatefrequencymillis = updatefrequencymin * 60 * 1000; // alarmmanager.cancel(pendingintent); alarmmanager.setinexactrepeating(alarmmanager.rtc, system.currenttimemillis(), updatefrequencymillis, pendingintent); .... appwidgetmanager.updateappwidget(appwidgetid, view); } public void ondisabled(context context) { alarmmanager m = (alarmmanager) context.getsystemservice(context.alarm_service); m.cancel(pendingintent); }
if set update frequency 5 minutes inexactrepeating, log:
08-18 15:30:17.902: d/tag(23624): receive 08-18 15:30:18.122: d/tag(23624): receive 08-18 15:30:18.122: d/tag(23624): update 08-18 15:30:18.122: d/tag(23624): update freq: 60 08-18 15:30:22.582: d/tag(23624): receive 08-18 15:30:22.602: d/tag(23624): update freq: 60 08-18 15:30:33.272: d/tag(23624): receive 08-18 15:30:33.282: d/tag(23624): update freq: 60 08-18 15:30:39.692: d/tag(23624): receive 08-18 15:30:39.692: d/tag(23624): update freq: 60 08-18 15:31:33.292: d/tag(23624): receive <--- have chosen freq 08-18 15:31:33.292: d/tag(23624): update freq: 5 08-18 15:31:39.962: d/tag(23624): receive 08-18 15:31:39.962: d/tag(23624): update freq: 5
i don't know why onreceive , onupdate called multiple times when on configuration activity.
with setrepeat method worse can see timestamps:
08-18 15:38:59.992: d/tag(24011): receive 08-18 15:39:00.242: d/tag(24011): receive 08-18 15:39:00.242: d/tag(24011): update 08-18 15:39:00.252: d/tag(24011): update freq: 60 08-18 15:39:00.442: d/tag(24011): receive 08-18 15:39:00.442: d/tag(24011): update freq: 60 08-18 15:39:00.562: d/tag(24011): receive 08-18 15:39:00.562: d/tag(24011): update freq: 60 08-18 15:39:00.642: d/tag(24011): receive 08-18 15:39:00.642: d/tag(24011): update freq: 60 08-18 15:39:00.652: d/tag(24011): receive 08-18 15:39:00.652: d/tag(24011): update freq: 60 08-18 15:39:00.662: d/tag(24011): receive 08-18 15:39:00.662: d/tag(24011): update freq: 60
and finally, preference activity:
public class datastatwidgetconfigure extends preferenceactivity { private int widgetid; @suppresswarnings("deprecation") @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); bundle extras = getintent().getextras(); if (extras != null) { widgetid = extras.getint(appwidgetmanager.extra_appwidget_id, appwidgetmanager.invalid_appwidget_id); log.d("meeegy", integer.tostring(widgetid)); getpreferencemanager().setsharedpreferencesname(integer.tostring(widgetid)); addpreferencesfromresource(r.xml.prefs); } } @override public void onbackpressed() { intent resultvalue = new intent(); resultvalue.putextra(appwidgetmanager.extra_appwidget_id, widgetid); setresult(result_ok, resultvalue); finish(); } }
from see in code, never cancel previous alarms when update widget. keep adding more , more callback alarmmanager. try removing previous call , add new one
Comments
Post a Comment