Android handler several common ways of passing values

Source: Internet
Author: User
Tags diff
 package com.handlerthread;
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 HandlerThread2 extends activity {@Override protected void onCreate (Bundle savedinstancestate) {SUPER.O
		Ncreate (savedinstancestate);
		
		Setcontentview (R.layout.main);
		SYSTEM.OUT.PRINTLN ("Activity thread ID:" +thread.currentthread (). GetId ());
		Handlerthread handlerthread = new Handlerthread ("Handlerthread");
		Handlerthread.start ();
		MyHandler handler = new MyHandler (Handlerthread.getlooper ());
		
		Message msg = Handler.obtainmessage ();
		MSG.ARG1 = 123;//Pass integer data//msg.obj = "ASD"; Pass object type///Use Bundle object to pass value Bundle B = new Bundle ();
		B.putint ("ID", 12);
		B.putstring ("name", "ThinkPad");
		
		Msg.setdata (b);
	Msg.sendtotarget ();
		Class MyHandler extends Handler {public MyHandler () {super (); } public MyHandler (Looper loopeR) {super (Looper);
			@Override public void Handlemessage (msg) {super.handlemessage);
			int args = MSG.ARG1;
			
			string s = (string) msg.obj;
			Gets the value of the Bundle object Bundle b = Msg.getdata ();
			int id = b.getint ("id");
			String name = b.getstring ("name");
			
			SYSTEM.OUT.PRINTLN ("ID is" +id+ ", the name is" +name);
		System.out.println ("Handler thread ID:" +thread.currentthread (). GetId ()); }
		
	}

}

Run results

So, what exactly is bundle? Let's take a look at the official explanation.

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.