7

My app has a widget that shows today's date and need to be updated on midnight. The widget is defined in the manifest as

<manifest>
    ...
    <application>
        ...
        <receiver
            android:name=".MyWidgetProviderClass"
            android:label="MyLabel" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/widget_icon_info" />
        </receiver>
    </application>
</manifest>

And the widget info file is

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialLayout="@layout/my_layout"
    android:minHeight="40dp"
    android:minWidth="294dp"
    android:updatePeriodMillis="3600000" >
</appwidget-provider>

This causes the widget to get updated anytime between midnight and 1pm but I want to to get updated closer to midnight so I added to the app an intent receiver

<receiver
     android:name=".MyWidgetUpdaterClass"
     android:enabled="true" >
     <intent-filter>
         <action android:name="MyAction" />
      </intent-filter>
</receiver>

and a static method

public static void setMidngintAlarms(Context context) {
    Intent intent = new Intent("MyAction");
    PendingIntent pendingIntent = 
        PendingIntent.getBroadcast(context, 0, intent, 0);              
    long interval = 24*60*60*1000;        
    long firstTime = SystemClock.elapsedRealtime() + 
        millisecondsToNextMidnight();
    AlarmManager am = (AlarmManager)context.getSystemService(
        Context.ALARM_SERVICE);
    am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                    firstTime, interval, pendingIntent);
}

(since alarm manager requests with the same intent overwriting each other there is not harm in calling setMidngintAlarms multiple times).

I tried calling setMidnightAlarms() from few places (from the app's main activity, from the AppWidgetProvider's onUpdate(), etc). Everything works well and the alarm manager intents are received and the widget is updated but only as long as the app runs. If I killed the app, the widgets stops updating on midnight.

Any idea how to cause the widgets to update on midnight? Do I need to add a service? If so how? Any example?

I am a little bit surprised how non trivial it is to get this basic functionality to work.

4

2 回答 2

0

我通过以下方式解决此问题:

  1. 启动一个服务来监听 Intent.ACTION_TIME_TICK 消息。如果 Time TICKED,则向小部件发送广播。(每分钟)。

  2. 在小部件中接收广播并通过以下方式判断天气是午夜:

代码:

   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.
        }
    }
于 2012-08-10T08:51:47.733 回答
0

将此添加到您的清单小部件接收器

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

日期更改将触发您的 onReceive() 以“android.intent.action.DATE_CHANGED”操作启动

于 2017-08-10T10:59:40.593 回答