Handler and callback mechanisms in Android

Source: Internet
Author: User

Handler and callback mechanism in Android--Iteye technology website

The handler is primarily used to communicate between threads in a mechanism. If you need to accept messages from other threads in the activity or service, you need to implement the Handler.callback interface in the activity or service that needs to receive the message to implement this interface:

Java code ?
    1. public? booleanhandlemessage (? Message?inmessage?)? ;??
public boolean handlemessage (Message inmessage);

?

Method. An instance of the class that implements the callback is then passed in where the handler is created:

Java code ?
    1. Looper?? Looper?=?incontext.getmainlooper ();??
    2. Mthread?=?looper.getthread ();??
    3. Mhandler?=? new? Handler (Looper?,? mcallback?);??
Looperlooper = Incontext.getmainlooper (); mthread = Looper.getthread (); mhandler = new Handler (Looper, mcallback);

?

Or:

Java code ?
    1. Mhandler?=? new? Handler (? mcallback?)??
Mhandler = new Handler (mcallback)

?

You can then send a message to the Acitity or service using the following code in the thread:

Java code ?
    1. //intoken? is an object that can pass in other parameters ??
    2. Mhandler.obtainmessage (inwhat?,? inA?,? inB?,? intoken?)??
    3. Mhandler.sendmessage (? inmessage?)??
Intoken is an object that can pass in other parameters Mhandler.obtainmessage (Inwhat, InA, InB, Intoken) mhandler.sendmessage (inmessage)

?

? or

Java code ?
    1. Mhandler.sendemptymessage (? inwhat?)??
Mhandler.sendemptymessage (Inwhat)

?

When the above code is taken, the handlemessage using the callback instance will be called when the handler is created.

?

You can use the following code to put a thread instance into handler for it to execute:

Java code ?
    1. Mhandler.post (? inrunnable?);???

?

You can delete this thread using the following code:

Java code ?
    1. Mhandler.removecallbacks (? inrunnable?);???

? There are many similar ways to send messages or add threads on handler. Added some other features.

Handler and callback mechanisms in Android

Related Article

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.