0

ハンドラーでルーパーを使用する必要がある場合に誰か教えてもらえますか? 複数のスレッドとハンドラーがあるコードベースがあります。しかしLooper.prepare()Looper.loop()それらすべてに対して呼び出されるわけではありません。

handleMessage メソッドでメッセージを継続的に処理するためにルーパーが必要なのだろうか? ルーパーがなくても、メッセージがハンドラーに送信されたときに handleMessage() は呼び出されませんか? ここで Looper が提供するその他の目的は何ですか?

ありがとう、シャミー

4

2 に答える 2

3

スレッドのメッセージ ループを実行するために使用されるクラス。デフォルトでは、スレッドにはメッセージ ループが関連付けられていません。これを作成するには、ループを実行するスレッドで prepare() を呼び出し、次に loop() を呼び出して、ループが停止するまでメッセージを処理させます。

メッセージ ループとの対話のほとんどは、Handler クラスを介して行われます。

以下に、スレッドの実行メソッドがあります

@Override
    public void run() {
        try {
            // preparing a looper on current thread         
            // the current thread is being detected implicitly
            Looper.prepare();

            Log.i(TAG, "DownloadThread entering the loop");

            // now, the handler will automatically bind to the
            // Looper that is attached to the current thread
            // You don't need to specify the Looper explicitly
            handler = new Handler();

            // After the following line the thread will start
            // running the message loop and will not normally
            // exit the loop unless a problem happens or you
            // quit() the looper (see below)
            Looper.loop();

            Log.i(TAG, "DownloadThread exiting gracefully");
        } catch (Throwable t) {
            Log.e(TAG, "DownloadThread halted due to an error", t);
        } 
    }
于 2012-06-05T09:39:32.593 に答える
0

Android Looper は、Android ユーザー インターフェイス内の Java クラスであり、Handler クラスと共に、ボタンのクリック、画面の再描画、向きの切り替えなどの UI イベントを処理します。また、コンテンツを HTTP サービスにアップロードしたり、画像のサイズを変更したり、リモート リクエストを実行したりするためにも使用できます。

http://developer.android.com/reference/android/os/Looper.html

于 2012-06-05T09:44:47.977 に答える