1

ハンドラー内で TextView を設定するにはどうすればよいですか?

public class DigitalClock extends AppWidgetProvider {

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

    RemoteViews views = new RemoteViews(context.getPackageName(),
            R.layout.digitalclock);

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

        views.setOnClickPendingIntent(R.id.rl, pendingIntent);

        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
}

private static Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        // update your textview here.


    }
};

class TickThread extends Thread {
    private boolean mRun;

    @Override
    public void run() {
        mRun = true;

        while (mRun) {
            try {
                sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        mHandler.sendEmptyMessage(0);
    }
}
}

ここでTextViewを更新することになっています:

    private static Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        // update your textview here.
    ...

どうすればいいですか?OnUpdateメソッドでは使用しますviews.setTextViewText(R.id...が、HandlerRemoteViews には存在しません。私が知っていることはすべて試しましたが、これまでのところ何もありません

4

1 に答える 1

1

新しいものを作成してください :) RemoteViews がリモート エンティティにアタッチされたばかりで、それが実現されたときに行われる一連の変更をほとんどキューに入れます。

だからあなたがするとき

appWidgetManager.updateAppWidget(appWidgetId, views);

これは、RemoteViews が実際に何かを行うときです。

本当の問題は、使用されているデザインが少し乱雑であることだと思います. これが開始される場所がわからないスレッドがありますが、ハンドラーを呼び出します。これは問題ありませんが、おそらく構造化されたデータを送信して、ハンドラーが何をすべきかを知る必要があります。RemoteViews インスタンス自体は Parcelable です。つまり、Intent や Message インスタンスなどのペイロードの一部として送信できます。この設計の本当の問題は、実際に変更を実行するために AppWidgetManager インスタンスなしで updateAppWidget を呼び出すことができないことです。

ウィジェットの存続期間中 AppWidgetManager をキャッシュするか、更新頻度を更新してより多くの遅延キュー ワーカーに移動することができます。システムから受け取る次の更新イベントの場所、または両方の混合。

private SparseArray<RemoteView> mViews;

public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {

       ....
       for (int appWidgetId : appWidgetIds) {
          RemoteViews v = mViews.get(appWidgetId);
          if (v != null) {
              appWidgetManager.updateWidget(appWidgetId, v);
          } else {
              enqueue(appWidgetManager, appWidgetId, new RemoteViews(new RemoteViews(context.getPackageName(),
            R.layout.digitalclock)));
             /* Enqueue would pretty much associate these pieces of info together
                and update their contents on your terms. What you want to do is up
                to you. Everytime this update is called though, it will attempt to update
                the widget with the info you cached inside the remote view. 
              */
          }
        }
}
于 2013-02-23T17:16:30.293 に答える