【问题标题】:Widget TextView not updating when using timer使用计时器时小部件 TextView 未更新
【发布时间】:2013-03-21 10:31:58
【问题描述】:

我有一个 TextView 来更新小部件中的时间和日期,并尝试使用 Timer 每秒更新一次,但它不起作用:

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    final int N = appWidgetIds.length;

    for (int i = 0; i < N; i++) {
        int appWidgetId = appWidgetIds[i];

        Intent clockIntent = new Intent(context, DeskClock.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, clockIntent, 0);

        final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.digitalclock);
        views.setOnClickPendingIntent(R.id.rl, pendingIntent);

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                java.util.Date noteTS = Calendar.getInstance().getTime();
                String time = "kk:mm";
                String date = "dd MMMMM yyyy";

                views.setTextViewText(R.id.tvTime, DateFormat.format(time, noteTS));
                views.setTextViewText(R.id.tvDate, DateFormat.format(date, noteTS));
            }
        }, 0, 1000);// Update text every second

        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
}

我在某个地方出错了,所以如果有人知道,请告诉我并给我正确的方法来做到这一点。提前致谢

【问题讨论】:

    标签: android timer android-widget widget textview


    【解决方案1】:

    尝试在 run() 中更新应用小部件

            @Override
            public void run() {
                java.util.Date noteTS = Calendar.getInstance().getTime();
                String time = "kk:mm";
                String date = "dd MMMMM yyyy";
    
                views.setTextViewText(R.id.tvTime, DateFormat.format(time, noteTS));
                views.setTextViewText(R.id.tvDate, DateFormat.format(date, noteTS));
                appWidgetManager.updateAppWidget(appWidgetId, views);
            }
        }, 0, 1000);// Update text every second
    

    像这样尝试这对我有用,我认为我在使用的另一个应用程序中遇到了同样的问题,而不是使用计时器和 timertask:

    Handler mHandler;
    Runnable continuousRunnable = new Runnable() {
            public void run() {
                java.util.Date noteTS = Calendar.getInstance().getTime();
                String time = "kk:mm";
                String date = "dd MMMMM yyyy";
    
                views.setTextViewText(R.id.tvTime, DateFormat.format(time, noteTS));
                views.setTextViewText(R.id.tvDate, DateFormat.format(date, noteTS));
                appWidgetManager.updateAppWidget(appWidgetId, views);
    
                mHandler.postDelayed(this, 1000);
            }
        };
      mHandler.post(continuousRunnable);
    

    【讨论】:

    • 我猜Handler mHandler; 超出了OnUpdate 方法.. 试过了,现在甚至没有更新一次
    • 我成功了!我只需要将timer.schedule() 更改为timer.scheduleAtFixedRate 我对计时器进行了一些研究,结果发现schedule 只运行一次,而scheduleAtFixedRate 重复。感谢@JRowan 的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-02
    • 1970-01-01
    • 2019-07-22
    • 1970-01-01
    • 2011-10-02
    • 2021-12-19
    • 1970-01-01
    相关资源
    最近更新 更多