Basic usage of Handler

Source: Internet
Author: User

Definition of Handler

Primarily accepts data sent by a child thread and updates the UI with this data in conjunction with the main thread

Explain

When the application starts, Android first opens a main thread (that is, the UI thread), and the main thread is the UI control in the admin interface for event distribution. If you need a time-consuming operation at this point, such as: networked read data, you cannot put these operations in the main thread, if you put in the main thread 5 seconds is not completed, you will receive an Android system error prompt "forced shutdown." This time we put the time-consuming operation on a sub-thread, because the child threads involve UI updates, and the Android main thread is not secure, that is, the update UI can only be updated in the main thread. The emergence of handler is to solve this complex problem. Since handler runs in the mainline approached (UI thread), at this time, the handler undertakes to accept the message object passed by the child thread, (contains data), put these messages into the main thread queue, with the main thread to update the UI.

Handler some features

Handler can distribute the Message object and the Runnable object into the main thread, and each handler instance is bound to the one in which it was created
(typically located in the main thread), it has two functions:

    • Schedule a message or runnable to execute somewhere in a main thread
    • Schedule an action to execute in a different thread

Some ways to distribute messages in handler

    • Post (Runnable)
    • Postattime (Runnable,long)
    • Postdelayed (Runnable Long)
    • Sendemptymessage (int)
    • SendMessage (Message)
    • Sendmessageattime (Message,long)
    • Sendmessagedelayed (Message,long)

The Post class method above allows you to arrange a runnable object into the main thread queue,
The SendMessage class method allows you to schedule a message object with data to queue and wait for updates.

Handler instances
  1. publicclassMyHandlerActivityextendsActivity{
  2. Button button;
  3. MyHandler myHandler;
  4. protectedvoid onCreate(Bundle savedInstanceState){
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.handlertest);
  7. button =(Button) findViewById(R.id.button);
  8. myHandler =newMyHandler();
  9. MyThread m =newMyThread();
  10. newThread(m).start();
  11. }
  12. //接受消息,处理消息 ,此Handler会与当前主线程一块运行
  13. classMyHandlerextendsHandler{
  14. publicMyHandler(){
  15. }
  16. publicMyHandler(Looper L){
  17. super(L);
  18. }
  19. // 子类必须重写此方法,接受数据
  20. @Override
  21. publicvoid handleMessage(Message msg){
  22. super.handleMessage(msg);
  23. // 此处可以更新UI
  24. Bundle b = msg.getData();
  25. String color = b.getString("color");
  26. MyHandlerActivity.this.button.append(color);
  27. }
  28. }
  29. classMyThreadimplementsRunnable{
  30. publicvoid run(){
  31. try{
  32. Thread.sleep(10000);
  33. }catch(InterruptedException e){
  34. e.printStackTrace();
  35. }
  36. Message msg =newMessage();
  37. Bundle b =newBundle();// 存放数据
  38. b.putString("color","我的");
  39. msg.setData(b);
  40. MyHandlerActivity.this.myHandler.sendMessage(msg);// 向Handler发送消息,更新UI
  41. }
  42. }
  43. }



From for notes (Wiz)



Basic usage of Handler

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.