我在这里构建一个简单的时钟小部件很有趣。我想知道最好的做法是什么?大多数时候它工作正常,但有些人说我的时钟小部件落后了。实际时间是上午 10 点,然后我的小部件显示可能是上午 9 点 48 分
我的清单上有这个
<receiver android:name="my.package.name.MyClock" android:label="@string/widget_name"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/my_clock" /> </receiver> <service android:name="MyClock$UpdateService" android:label="UpdateService" > <intent-filter> <action android:name="my.package.name.UPDATE" /> </intent-filter> </service>
这是我的主要java类
公共类 MyClock 扩展 AppWidgetProvider {
@Override public void onDisabled(Context context) { super.onDisabled(context); context.stopService(new Intent(context, UpdateService.class)); } @Override public void onEnabled(Context context) { super.onEnabled(context); context.startService(new Intent(UpdateService.ACTION_UPDATE)); } @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { super.onUpdate(context, appWidgetManager, appWidgetIds); context.startService(new Intent(UpdateService.ACTION_UPDATE)); } public static final class UpdateService extends Service { static final String ACTION_UPDATE = "my.package.name.UPDATE"; private final static IntentFilter sIntentFilter; private String mMinuteFormat; private String mHourFormat; private Calendar mCalendar; static { sIntentFilter = new IntentFilter(); sIntentFilter.addAction(Intent.ACTION_TIME_TICK); sIntentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED); sIntentFilter.addAction(Intent.ACTION_TIME_CHANGED); } @Override public void onCreate() { super.onCreate(); reinit(); registerReceiver(mTimeChangedReceiver, sIntentFilter); } @Override public void onDestroy() { super.onDestroy(); unregisterReceiver(mTimeChangedReceiver); } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); reinit(); update(); } @Override public IBinder onBind(Intent intent) { return null; } private void update() { mCalendar.setTimeInMillis(System.currentTimeMillis()); final CharSequence minute = DateFormat.format(mMinuteFormat, mCalendar); final CharSequence hour = DateFormat.format(mHourFormat, mCalendar); RemoteViews views = null; views = new RemoteViews(getPackageName(), R.layout.main); views.setTextViewText(R.id.HOUR, hour); views.setTextViewText(R.id.MINUTE, minute); //Refresh the widget ComponentName widget = new ComponentName(this, MyClock.class); AppWidgetManager manager = AppWidgetManager.getInstance(this); manager.updateAppWidget(widget, views); } private void reinit() { mHourFormat = "hh"; mMinuteFormat = "mm"; } private final BroadcastReceiver mTimeChangedReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); if (action.equals(Intent.ACTION_TIME_CHANGED) || action.equals(Intent.ACTION_TIMEZONE_CHANGED)) { reinit(); } update(); } }; } }
我错过了什么?为什么小部件落后了?你能帮我发现这里的问题吗?我在做正确的方法吗?使用 Service 而不是 AlarmManager 每分钟更新时钟小部件?
问候