如何更新午夜一个应用程序窗口小部件?部件、应用程序、午夜、窗口

2023-09-06 13:54:59 作者:卫生纸都比你干净

我的应用程序有一个小窗口,显示当天的日期和需要更新的午夜。窗口小部件在清单中定义为

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>

和小部件的信息文件是

<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>

这会导致部件得到随时随地午夜和下午1点之间更新,但我希望得到更新更接近午夜,所以我加入到应用程序的意图接收器

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>

和一个静态方法

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);
}

(因为具有相同目的的报警管理要求覆盖彼此没有伤害,呼吁setMidngintAlarms多次)。

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

我打过电话setMidnightAlarms()从几个地方(从应用程序的主要活动,从AppWidgetProvider的的OnUpdate(),等)。一切运作良好,并在接收到报警管理器意图和小窗口被更新,但仅只要应用程序运行。如果我杀了应用程序,窗口小部件停止更新午夜。

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.

推荐答案

我通过解决此问题:

开始听Intent.ACTION_TIME_TICK消息的服务。 然后发送一个广播窗口小部件,如果时间勾选。 (在每分钟)。

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

在接收部件和判断天气广播是午夜方式:

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.
        }
    }