The Android development tutorial uses Looper to process Message Queuing _android

Source: Internet
Author: User

Copy Code code as follows:

Package Com.yanjun;

Import android.app.Activity;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.os.HandlerThread;
Import Android.os.Looper;
Import Android.os.Message;

public class Handleractivity extends activity {

@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Implements the ability to use Looper to process message queues by Handlerthread objects
Handlerthread handlerthread = new Handlerthread ("Handlerthread");
Make thread run must call Start ()
Handlerthread.start ();
MyHandler MyHandler = new MyHandler (Handlerthread.getlooper ());
Message message = Myhandler.obtainmessage ();
Send Message to Myhandler.obtainmessage () target, that is, who called it sent to
Bundle Bundle = new Bundle ();
Adding data through bundle
Bundle.putint ("Age", 10);
Bundle.putstring ("name", "John");
Add the bundle information in
Message.setdata (bundle);
Message.sendtotarget ();
}

Class MyHandler extends Handler {

Public MyHandler (Looper looper) {
Super (Looper);
TODO auto-generated Constructor stub
}

@Override
public void Handlemessage (msg) {
Bundle Bundle = Msg.getdata ();
int age = Bundle.getint (' age ');
String name = bundle.getstring ("name");
System.out.println ("ages" + Age + "name" + name);
}

}

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.