Handler allows us to send and process Message and Runnable Objects associated with a thread's MessageQueue.
Why Handler?:
There are two main uses for a Handler:
a)- To Schedule Messages and Runnables to be executed as some point in the future.
b)- To enqueue an action to be performed on a different Thread than our own.
Message?:
Defines a message containing a description and orbitrary data object that can be sent to a Handler.this object contaions two extra fields.
Looper :
It's designed to make an ordinary thread into a Looper Thread.We need this this type of thread because we often need a thread which continuously cycle.if there is a new task execute them and continue to wait for the next task. Looper internally maintains a message queue(Linked Queue).
NOTE: A Thread can only have a Looper object.
Class used to run a message loop for a thread. Threads by default do not have a Message loop associated with them; to create one, call prepare() in the thread that is to run loop, and then loop() to have it process messages until the loop is stopped.
Each Handler instance is associated with a single thread and that thread's message queue.
The Handler is the middleman between a new thread and the message queue.
Note:- When we Create a new Handler,it's bound to the Thread/ MessageQueue 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 queue.
Handler Ball Moving Example:
HandlerUIExample.java :
BounceBallView.java :
Why Handler?:
There are two main uses for a Handler:
a)- To Schedule Messages and Runnables to be executed as some point in the future.
b)- To enqueue an action to be performed on a different Thread than our own.
Message?:
Defines a message containing a description and orbitrary data object that can be sent to a Handler.this object contaions two extra fields.
Looper :
It's designed to make an ordinary thread into a Looper Thread.We need this this type of thread because we often need a thread which continuously cycle.if there is a new task execute them and continue to wait for the next task. Looper internally maintains a message queue(Linked Queue).
NOTE: A Thread can only have a Looper object.
Class used to run a message loop for a thread. Threads by default do not have a Message loop associated with them; to create one, call prepare() in the thread that is to run loop, and then loop() to have it process messages until the loop is stopped.
Class LooperThread extends Thread{ public Handler mHandler; public void run(){ Looper.prepare(); mHandler=new Hanlder(){ public void handleMessage(Message msg){ //process incomming messages here } }; Looper.loop(); } }
Each Handler instance is associated with a single thread and that thread's message queue.
The Handler is the middleman between a new thread and the message queue.
Note:- When we Create a new Handler,it's bound to the Thread/ MessageQueue 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 queue.
Handler Ball Moving Example:
HandlerUIExample.java :
package com.imrankhanandroid.HandlerExample; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.Window; public class HandlerUIExample extends Activity { /** Called when the activity is first created. */ protected static final int UiUpadte=0x1001; Thread myThread=null; BounceBallView UiBounceBallview=null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); this.UiBounceBallview=new BounceBallView(this); setContentView(this.UiBounceBallview); new Thread(new myThread()).start(); } Handler Testhandler=new Handler(){ public void handleMessage(Message msg){ switch(msg.what) { case HandlerUIExample.UiUpadte: UiBounceBallview.invalidate(); break; } super.handleMessage(msg); } }; class myThread implements Runnable{ /*Worker Thread for Updating UI */ @Override public void run() { // TODO Auto-generated method stub while(!Thread.currentThread().isInterrupted()){ Message mesg=new Message(); mesg.what=HandlerUIExample.UiUpadte; HandlerUIExample.this.Testhandler. sendMessage(mesg); try{ Thread.sleep(100); } catch (InterruptedException e) { // TODO: handle exception Thread.currentThread().interrupt(); } } } } }
BounceBallView.java :
Download Source Code: HandlerExample
package com.imrankhanandroid.HandlerExample; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.view.View; public class BounceBallView extends View { float x=20; float y=260; float xy=230; float yx=140; float yz=60; float yt=0; public BounceBallView(Context context) { super(context); // TODO Auto-generated constructor stub } @Override protected void onDraw(Canvas canvas) { Paint mPaint =new Paint(); mPaint.setAntiAlias(true); mPaint.setColor(Color.RED); canvas.drawCircle(x, yz, 30, mPaint); mPaint.setColor(Color.GREEN); canvas.drawCircle(x, yx, 30, mPaint); mPaint.setColor(Color.YELLOW); canvas.drawCircle(x, xy, 30, mPaint); if(x==y & x>20 ) { x=y-20; y-=20; yt=yz; yz=yx; yx=yt; } else { yt=yz; yz=yx; yx=yt; x+=10; y=260; } } }
0 comments:
Post a Comment