1

TextViewウィジェットの時刻と日付を更新する を取得し、を使用しTimerて 1 秒ごとに更新しようとしましたが、機能していません。

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);
    }
}

私はどこかで間違っているので、誰かが知っているなら、私に知らせて、これを行う正しい方法を教えてください. 前もって感謝します

4

1 に答える 1

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

このように試してみてください。これは私にとってはうまくいきます。タイマーとタイマータスクを使用する代わりに、使用していた別のアプリで同じ問題が発生したと思います。

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);
于 2013-03-31T00:33:48.927 に答える