App Widget 更新 TextView
我一直在遵循制作 Android 应用程序小部件的教程,但遇到了一些麻烦。本教程引导我创建了这段代码:
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 永远不会改变。感谢您的帮助,我很想了解应用程序小部件。
缺口
I have been following a tutorial to make Android App Widgets, and I was having a little trouble. The tutorial led me to create this code:
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);
}
}
but it seems that the run method is never being called, as the TextView never changes. Thanks for your help, I'd love to get a handle on App Widgets.
Nick
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您不能使用
Timer
。一旦onUpdate()
返回,您的AppWidgetProvider
和Timer
将被垃圾收集。其次,请不要每秒更新一个应用程序小部件。
android:updatePeriodMillis
最短时间为 30 分钟是有原因的——将更新推送到应用程序小部件是一项昂贵的操作。每秒更新一次不仅会迫使您的“更新应用程序小部件”代码有效地驻留在内存中并始终运行,而且还会显着消耗用户的电池。要定期更新应用小部件,请使用
android:updatePeriodMillis
或使用AlarmManager
。First, you cannot use
Timer
. YourAppWidgetProvider
, and theTimer
, will be garbage-collected onceonUpdate()
returns.Second, please do not update an app widget every second. There is a reason why
android:updatePeriodMillis
has a 30-minute minimum -- pushing updates to app widgets is an expensive operation. Updating one every second would not only force your "update an app widget" code to effectively be resident in memory and running all of the time, but you will also whack the user's battery pretty significantly.For periodic updates of an app widget, either use
android:updatePeriodMillis
or useAlarmManager
.