How to update an app widget on midnight?

混江龙づ霸主 提交于 2019-12-04 05:38:54

I resolve this problem by:

  1. Start a service to listen to Intent.ACTION_TIME_TICK message. Then send a broadcast to widget if Time TICKED. (At every minute).

  2. Receive the broadcast in widget and judge weather it is the midnight by:

Code:

   if (MyWidget.BROADCAST_TIME_TICK.equals(action)) {
        //If it's midnight, then update.
        int hour = Calendar.getInstance().get(Calendar.HOUR);
        int minute = Calendar.getInstance().get(Calendar.MINUTE);
        Utils.log(TAG, "time is : " + hour+":" + minute);
        if (!(hour == 0 && minute == 0)) {  //Only update at midnight's time tick.
            return;
        } else {
            //Do the midnight stuff.
        }
    }

ad this to your manifest widget receiver

 <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
            <action android:name="android.intent.action.DATE_CHANGED" />...

the change in date will trigger your onReceive() to fire up with the "android.intent.action.DATE_CHANGED" action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!