2

私は何かについて少し混乱しています。基本的に、私はスレッドを生成しています。さらに、そのスレッドでメッセージ ループを実行したいと考えています。私は基本的に次のことを行っています: これは、Android ルーパー クラス API ドキュメントから直接引用したものです。ただし、私のアプリケーションは常に Looper.loop() でスタックし、そこから戻ることはありません。これに対する私の現在の回避策は、メイン スレッド (または UI スレッド) でハンドラーを作成し、代わりにそのスレッドにメッセージを送信することです。ただし、きれいにするため、およびアプリケーションの流れを意味のあるものにするために、作成中のスレッドにメッセージを送信することをお勧めします。

class LooperThread extends Thread {
      public Handler mHandler;

      public void run() {
          Looper.prepare();

          mHandler = new Handler() {
              public void handleMessage(Message msg) {
                  // process incoming messages here
              }
          };

          Looper.loop();
          // The rest of the code below is a control loop
      }
}

Looper.loop() が戻らない原因について何か考えはありますか?

4

2 に答える 2

7

Looper.loop無限ループを作成し、呼び出したときにのみ停止しますquit

http://developer.android.com/reference/android/os/Looper.html#loop()

于 2013-07-18T15:33:51.493 に答える
-2

これはうまくいくかもしれません

class LooperThread extends Thread {
      public Handler mHandler;

      public void run() {
          Looper.prepare();
          while(true){
             mHandler = new Handler() {
                public void handleMessage(Message msg) {
                  // process incoming messages here
                }
             };
          }
          // The rest of the code below is a control loop
      }
}
于 2015-02-11T07:52:12.533 に答える