我一直在学习制作 Android App Widgets 的教程,但遇到了一些麻烦。该教程引导我创建此代码:
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int [] appWidgetIds) {
Timer timer = new Timer();
timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1, 1000);
}
private class MyTime extends TimerTask {
RemoteViews remoteViews;
AppWidgetManager appWidgetManager;
ComponentName thisWidget;
DateFormat format = SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM, Locale.getDefault());
public MyTime(Context context, AppWidgetManager appWidgetManager) {
this.appWidgetManager = appWidgetManager;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
thisWidget = new ComponentName(context, FlipWidget.class);
}
@Override
public void run() {
remoteViews.setTextViewText(R.id.widget_text, "Time: "+format.format(new Date()));
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}
}
但似乎永远不会调用 run 方法,因为 TextView 永远不会改变。感谢您的帮助,我很想了解 App Widgets。
缺口