最終更新:2016-09-23 (金) 14:41:31 (2772d)  

android.os.Handler
Top / android.os.Handler

A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue?

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

メモ

  • 他のスレッドからメインスレッドに処理を依頼する窓口になる仕組み
  • ハンドラーに処理を依頼すると、メインスレッドは適切なタイミングでその処理を実行します。
  • When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.

用途

  • (2) to enqueue an action to be performed on a different thread than your own.

private handler handler;

onCreate(){
  handler = new Handler();
  new Thread(){
    @Override
    public void run(){
      //メッセージキューに追加
      handler.post(new Runnable(){
        @Override
        public void run(){
        //ここに処理
        }
      })
    }
  }
}

コンストラクタ

  • Handler() - Default constructor associates this handler with the Looper for the current thread.
  • Handler(Handler.Callback callback)
  • Handler(Looper looper)
  • Handler(Looper looper, Handler.Callback callback)

サブクラス

  • android.content.AsyncQueryHandler?
  • android.content.AsyncQueryHandler.WorkerHandler?
  • android.webkit.HttpAuthHandler?
  • android.webkit.SslErrorHandler?

メソッド

  • Handler.handleMessage?(Message msg) - Subclasses must implement this to receive messages.
  • Handler.sendMessage(Message msg) - Pushes a message onto the end of the message queue after all pending messages before the current time.
  • Handler.post(Runnable r) - Causes the Runnable r to be added to the message queue.
  • Handler.postDelayed(Runnable r, long delayMillis) - Causes the Runnable r to be added to the message queue, to be run after the specified amount of time elapses.

別スレッド

ソースコード

関連