为什么我的 Android 时间小部件不会自行更新?我为主屏幕创建了一个时间和日期小部件,但问题是我的时间小部件只有在屏幕方向发生更改时才会更新,例如从横向更改为纵向。它应该在实时模式下自动更新。这是我的代码:
public class PointlessWidget extends AppWidgetProvider {
Time mCalendar;
float mMinutes;
float mHour;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onUpdate(context, appWidgetManager, appWidgetIds);
mCalendar = new Time();
// Line below sets time that will be displayed - modify if needed.
mCalendar.setToNow();
int hour = mCalendar.hour;
int minute = mCalendar.minute;
int second = mCalendar.second;
mMinutes = minute + second / 60.0f;
mHour = hour + minute / 60.0f;
final int N = appWidgetIds.length;
for (int i = 0; i < N; i++) {
int awID = appWidgetIds[i];
RemoteViews v = new RemoteViews(context.getPackageName(),
R.layout.widget);
v.setTextViewText(R.id.textHour, " " + hour);
v.setTextViewText(R.id.textMinute, " " + minute);
appWidgetManager.updateAppWidget(awID, v);
}
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onDeleted(context, appWidgetIds);
Toast.makeText(context, "Widget Deleted!", Toast.LENGTH_SHORT).show();
}
}
这是我的清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidwidgets"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name=".PointlessWidget" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_stuff" />
</receiver>
<service android:name="TimeSettableAnalogClockService"/>
<activity android:name=".WidgetsConfig" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
</application>
</manifest>
这是我的 widget_stuff xml。我已经在转换中设置了 updatePeriodMillis="1000" 或相当于 1 秒。
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget"
android:minHeight="30dp"
android:minWidth="274dp"
android:updatePeriodMillis="1000" >
</appwidget-provider>