To open a thread in front and show progress with the progress bar This article, we use the thread to achieve such a simple function, is to click the button, load progress bar. But there is no discovery, click once, click again will not be effective. We need to show the next picture every time we click. Eternal Profit Meeting Casino
There is a need to introduce the Android messaging mechanism, simply, Handler, Looper, and the use of the message queue. Here we are using a simple example to illustrate the use of Handler, which is to send a number 5 to the message queue each time the button is clicked. Or do it in a paintingactivity.
Paintingactivity:
01 |
private Button btn_next; |
02 |
private Handler pic_hdl; |
05 |
protected void onCreate(Bundle savedInstanceState) { |
06 |
// TODO Auto-generated method stub |
07 |
super .onCreate(savedInstanceState); |
08 |
setContentView(R.layout.painting); |
10 |
btn_next = (Button)findViewById(R.id.btn_loadnext); |
11 |
btn_next.setOnClickListener( new ButtonListener()); |
13 |
pic_hdl = new PicHandler(); |
16 |
class ButtonListener implements OnClickListener{ |
19 |
public void onClick(View v) { |
20 |
// TODO Auto-generated method stub |
21 |
if ( v.getId() == R.id.btn_loadnext){ |
22 |
Message msg = pic_hdl.obtainMessage(); |
24 |
pic_hdl.sendMessage(msg); |
30 |
class PicHandler extends { |
33 |
public void handleMessage(Message msg) { |
34 |
// TODO Auto-generated method stub |
36 |
System.out.println( "what: " + what); |
It was so simple that it was done. Demo Effect:
Briefly describe the process:
- Private Handler PIC_HDL; Define a Handler, while PIC_HDL = new Pichandler (); is to create a new Handler from our defined class Pichandler;
- Then look at the button OnClick event, obtainmessage to get a Msg object, and then assign a value to Msg.what, SendMessage sends the MSG object to the message queue;
- Returning to Pichandler, we can easily regain the value of what is in the message queue in Handlemessage. Then print it in Log.
And then we'll go deeper Handler this thing.
An example of a simple use of Android handler