App Widget 更新 TextView

发布于 2024-10-06 04:06:50 字数 1095 浏览 5 评论 0原文

我一直在遵循制作 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥有 2024-10-13 04:06:50

首先,您不能使用Timer。一旦 onUpdate() 返回,您的 AppWidgetProviderTimer 将被垃圾收集。

其次,请不要每秒更新一个应用程序小部件。 android:updatePeriodMillis 最短时间为 30 分钟是有原因的——将更新推送到应用程序小部件是一项昂贵的操作。每秒更新一次不仅会迫使您的“更新应用程序小部件”代码有效地驻留在内存中并始终运行,而且还会显着消耗用户的电池。

要定期更新应用小部件,请使用 android:updatePeriodMillis 或使用 AlarmManager

First, you cannot use Timer. Your AppWidgetProvider, and the Timer, will be garbage-collected once onUpdate() 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 use AlarmManager.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文