Android widget refresh not working -
my widget should refresh textviews every day @ 0:00. in widget_provider.xml set android:updateperiodmillis="1000" read minimum update period 30 minutes , have use alarmmanager this. want alarm triggers refresh every day @ 0:00. updateservice.class handles refreshing (setting texts textviews based on date. class not called until around half hour after midnight)
in public void onupdate(context context, appwidgetmanager appwidgetmanager, int[] appwidgetids)
method using code:
intent intentn = new intent(context, updateservice.class); pendingintent pendingintent = pendingintent.getbroadcast(context, 0, intentn, pendingintent.flag_update_current); calendar calendarn = calendar.getinstance(); calendarn.settimeinmillis(system.currenttimemillis()); calendarn.add(calendar.hour_of_day, 20); calendarn.add(calendar.minute, 44); calendarn.add(calendar.second, 5); alarmmanager alarmmanager = (alarmmanager)context.getsystemservice(context.alarm_service); alarmmanager.setrepeating(alarmmanager.rtc_wakeup, calendarn.gettimeinmillis(), 7000, pendingintent);
(i trying actual time now)
is right place code? why doesn't work? using toast messages in updateservice.java, never toast in onstart(intent intent, int startid)
method apart first time, when app in installed.
thanks in advance
update
with of probablykevin, rewrite code. tried code out in activity , works:
intent myintent = new intent(mainactivity.this, updateservice.class); pendingintent pendingintent = pendingintent.getservice(mainactivity.this, 0, myintent, 0); alarmmanager alarmmanager = (alarmmanager)getsystemservice(alarm_service); alarmmanager.cancel(pendingintent); calendar calendar = calendar.getinstance(); calendar.settimeinmillis(system.currenttimemillis()); calendar.add(calendar.second, 10); alarmmanager.setrepeating(alarmmanager.rtc_wakeup, calendar.gettimeinmillis(), 8000, pendingintent); toast.maketext(mainactivity.this, "start alarm", toast.length_long).show();
however if modify appwidgetprovider class (and put there ofc) not work. never toast msg:
intent myintent = new intent(context, updateservice.class); pendingintent pendingintent = pendingintent.getservice(context, 0, myintent, 0); alarmmanager alarmmanager = (alarmmanager)context.getsystemservice(context.alarm_service); alarmmanager.cancel(pendingintent); calendar calendar2 = calendar.getinstance(); calendar2.settimeinmillis(system.currenttimemillis()); calendar2.add(calendar.second, 10); alarmmanager.setrepeating(alarmmanager.rtc_wakeup, calendar2.gettimeinmillis(), 8000, pendingintent); toast.maketext(context, "start alarm", toast.length_long).show();
first, repeating time should 24 * 60 * 60 * 1000, in milliseconds. second start time found using time left until next 0:00:
final long millis_in_day = 24 * 60 * 60 * 1000; long timeuntilmidnight = millis_in_day - (system.currenttimemillis() % millis_in_day);
then set alarm adding time left current time:
alarmmanager.setrepeating(...system.currenttimemillis() + timeuntilmidnight, millis_in_day...);
make sure don't set multiple of same alarm. call
alarmmanager.cancel(pendingintent);
before setting again sure.
Comments
Post a Comment