The primary function of the ndroid.os.Message is to encapsulate the message and to specify the form of the message.
| No. |
Variable or method |
Type |
Describe |
| 1 |
public int |
Variable |
Used to define what this message belongs to |
| 2 |
Public Object obj |
Variable |
Used to define the information data passed by this message |
| 3 |
public int Arg1 |
Variable |
Used when passing some integral data, rarely used |
| 4 |
public int Arg2 |
Variable |
Used when passing some integral data, rarely used |
| 5 |
Public Handler Gettarget () |
Ordinary |
Gets the handler object that operates this message |
You can use Handler and message to process background time-consuming operations last updated to the foreground UI.
Download a Web image using hander and message
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". Mainactivity "> <imageview android:id=" @+id/imageview1 "android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" android:layout_alignparenttop= "true" android:layout_centerhorizontal= "t Rue "android:layout_margintop=" 40DP "android:src=" @drawable/ic_launcher "/> <button android: Id= "@+id/button1" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:la Yout_alignparentbottom= "true" Android:layout_Centerhorizontal= "true" android:text= "Get Picture"/></relativelayout>
Package Com.example.message1;import Java.io.ioexception;import Java.io.inputstream;import Java.net.malformedurlexception;import Java.net.url;import Android.app.activity;import Android.app.ProgressDialog; Import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.os.bundle;import Android.os.handler;import Android.os.message;import Android.view.view;import Android.widget.button;import Android.widget.imageview;public class Mainactivity extends Activity {public static final String URL = "http://www.baidu.c Om/img/baidu_sylogo1.gif ";p ublic static final int success_code = 1;private Button button;private ImageView ImageView; Private Handler Handler; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); button = (button) This.findviewbyid (R.id.button1); ImageView = (ImageView) This.findviewbyid (r.id.imageview1); final ProgressDialog dialog = new ProgressDialog ( Mainactivity.this);d Ialog.settitle ("Load Picture");d ialog.setmessage ("Loading please later ...");d ialog.setcancelable (false); Button.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method stubnew Thread (New MyThread () ). Start ();d ialog.show ();}); /Foreground UI Update Handler = new Handler () {@Overridepublic void Handlemessage (Message msg) {//TODO auto-generated method stub//Get Bi TMap picture Bitmap bitmap = (bitmap) msg.obj;//set the resource Imageview.setimagebitmap (bitmap) for the picture,//Determine if the picture is obtained, and close the dialog if (Msg.what = = Success_code) {Dialog.dismiss ();}}};} Background Thread class MyThread implements Runnable {@Overridepublic void run () {Bitmap Bitmap = null;//TODO auto-generated method s tubtry {URL url = new url (URL);//Create URL object InputStream inputstream = Url.openstream ();//Get input stream bitmap = Bitmapfactory.decodestream (InputStream);//Convert input stream to bitmap object} catch (Malformedurlexception e) {//TODO auto-generated Catch Blocke.printstacktrace ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} Create a Message object, return the message message message = Message.obtain (); message.obj = Bitmap;message.what = Success_code;handler.sendmessage (Message);}}
Next, let's start with the basics.
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". Mainactivity "> <textview android:id=" @+id/info "android:layout_width=" Wrap_content "Android : layout_height= "wrap_content" android:text= ""/> <button android:id= "@+id/button1" android:l Ayout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_alignparentbottom= "true" Android:layout_centerhorizontal= "true" android:layout_marginbottom= "150DP" android:text= "Get Data"/></ Relativelayout>
Package Com.example.message2;import Java.util.arraylist;import Java.util.list;import android.app.activity;import Android.os.bundle;import Android.os.handler;import Android.os.message;import Android.view.view;import Android.widget.button;import Android.widget.textview;public class Mainactivity extends Activity {<span style= " White-space:pre "></span>private button button;<span style=" White-space:pre "></span>private TextView textview;<span style= "White-space:pre" ></span>private Handler handler;<span style= " White-space:pre "></span> @Override <span style=" White-space:pre "></span>protected void OnCreate (Bundle savedinstancestate) {<span style= "White-space:pre" ></span>super.oncreate ( savedinstancestate); <span style= "White-space:pre" ></span>super.setcontentview (R.layout.activity_ Main); <span style= "white-space:pre" ></span>button = (Button) This.findviewbyid (r.id.button1); <span Style= "White-space:pre" ≫</span>textview = (TextView) This.findviewbyid (r.id.info); <span style= "White-space:pre" ></span >handler = new Handler () {<span style= "white-space:pre" ></span> @Override <span style= "White-space: Pre "></span>public void Handlemessage (Message msg) {<span style=" White-space:pre "></span>// TODO auto-generated method Stub<span style= "White-space:pre" ></span>int what = Msg.what;<span style= " White-space:pre "></span>int arg1 = Msg.arg1;<span style=" White-space:pre "></span>int arg2 = Msg.arg2;<span style= "White-space:pre" ></span>object obj = Msg.obj;<span style= "White-space:pre" > </span>List<Integer> List = Null;<span style= "White-space:pre" ></span>bundle Bundle = null; <span style= "White-space:pre" ></span>bundle = Msg.getdata ();//Receive bundle data <span style= "White-space: Pre "></span>list =bundle.getintegerarraylist (" num ");//Parse Data <span style=" white-Space:pre "></span>for (Integer num:list) {<span style=" White-space:pre "></span> System.out.print ("--list:" +num); <span style= "White-space:pre" ></span>}<span style= "White-space: Pre "></span>system.out.println (" What: "+ What +", arg1: "+ arg1 +", arg2: "<span style=" White-space:pre "> </span>+ arg2 + ". obj:" + obj); <span style= "White-space:pre" ></span>}<span style= "White-space: Pre "></span>};<span style=" White-space:pre "></span>button.setonclicklistener (new View.onclicklistener () {<span style= "white-space:pre" ></span> @Override <span style= "White-space:pre "></span>public void OnClick (View v) {<span style=" White-space:pre "></span>//TODO Auto-generated method Stub<span style= "White-space:pre" ></span>new Thread (New MyThread ()). Start ();< Span style= "White-space:pre" ></span>}<span style= "White-space:pre" ></SPAN>}); <span style= "White-sPace:pre "></span>}<span style=" White-space:pre "></span>class MyThread implements Runnable { <span style= "White-space:pre" ></span> @Override <span style= "White-space:pre" ></span> public void Run () {<span style= "white-space:pre" ></span>//TODO auto-generated method Stub<span style= " White-space:pre "></span>//message message=new message (); <span style=" White-space:pre "></span >//message.arg1=1;<span style= "White-space:pre" ></span>//message.arg2=2;<span style= " White-space:pre "></span>//message.obj=3;<span style=" White-space:pre "></span>// Message.what=4;<span style= "White-space:pre" ></span>//handler.sendmessage (message); <span style= " White-space:pre "></span>//The second method <span style=" White-space:pre "></span>//Message message= Message.obtain (); <span style= "White-space:pre" ></span>//message.what=1;<span style= "White-space: Pre "></span>//message.arg1=2;<span style= "White-space:pre" ></span>//message.arg2=3;<span style= " White-space:pre "></span>//message.obj=4;<span style=" White-space:pre "></span>// Handler.sendmessage (message); <span style= "White-space:pre" ></span>//the third method <span style= "White-space :p Re "></span>//Message Message=message.obtain (handler); <span style=" White-space:pre "></span >//message.what=1;<span style= "White-space:pre" ></span>//message.arg1=2;<span style= " White-space:pre "></span>//message.arg2=3;<span style=" White-space:pre "></span>// Message.obj=4;<span style= "White-space:pre" ></span>//message.sendtotarget (); <span style= " White-space:pre "></span>//Fourth method <span style=" White-space:pre "></span>//Message message= Message.obtain (handler, 1); <span style= "White-space:pre" ></span>//message.arg1=2;<span style= " White-space:pre "></span>Message.arg2=3;<span style= "White-space:pre" ></span>//message.obj=4;<span style= "White-space: Pre "></span>//message.sendtotarget () <span style=" White-space:pre "></span>//Fifth method <span Style= "White-space:pre" ></span>//Message Message=message.obtain (Handler, 1, 4); <span style= " White-space:pre "></span>//message.arg1=2;<span style=" White-space:pre "></span>// Message.arg2=3;<span style= "White-space:pre" ></span>//message.sendtotarget (); <span style= " White-space:pre "></span>//Sixth method <span style=" White-space:pre "></span>//Message message= Message.obtain (Handler, 1, 2, 3); <span style= "White-space:pre" ></span>//message.obj = 4;<span style= " White-space:pre "></span>//message.sendtotarget () <span style=" White-space:pre "></span>// Seventh method <span style= "White-space:pre" ></span>//Message Message=message.obtain (Handler, 1, 2, 3, 4); <span style="White-space:pre" ></span>//message.sendtotarget (); <span style= "White-space:pre" ></span>// Eighth method <span style= "White-space:pre" ></span>message Message = Message.obtain (Handler, 1, 2, 3, 4); <span Style= "White-space:pre" ></span>bundle bundle = new Bundle (); <span style= "White-space:pre" ></span >list<integer>list=new arraylist<integer> (); <span style= "White-space:pre" ></span> List.add (5); <span style= "White-space:pre" ></span>list.add (6) <span style= "White-space:pre" > </span>list.add (7); <span style= "White-space:pre" ></span>bundle.putintegerarraylist ("num", ( arraylist<integer>) list); <span style= "White-space:pre" ></span>message.setdata (bundle);< Span style= "White-space:pre" ></span>message.sendtotarget () <span style= "White-space:pre" ></ Span>}<span style= "White-space:pre" ></SPAN>}
We only give a demonstration of the last method
Open Logcat Display in System.out filter
sending messages using send and Psot
after clicking Get information, it will output in the console after 3 seconds
DEMO uses hander to implement the first screen of the application
Mainly using the Pos method to delay the operation of course, this operation requires the implementation of preparing a picture, as a background.
First screen XML
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin " android:paddingright=" @dimen/activity_horizontal_margin " android:paddingtop=" @dimen /activity_vertical_margin " tools:context=". Mainactivity "> <imageview android:id=" @+id/imageview1 " android:layout_width=" Fill_parent " android:layout_height= "fill_parent" android:layout_alignparentbottom= "true" android:layout_ Alignparentleft= "true" android:layout_alignparentright= "true" android:layout_alignparenttop= "true" android:src= "@drawable/logo"/></relativelayout><strong></strong>
Main interface XML
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" > <textview android:id= "@+id/textview1" android:layout_width= "Wrap_ Content " android:layout_height=" wrap_content " android:text=" main interface "/></linearlayout>
First screen Java
Package Com.example.message4;import Android.app.activity;import Android.content.intent;import android.os.Bundle; Import Android.os.handler;import Android.view.window;import Android.view.windowmanager;public class MainActivity Extends Activity {public static final int sight_time = n; @Overrideprotected void OnCreate (Bundle savedinstancestate) {s Uper.oncreate (savedinstancestate); Requestwindowfeature (window.feature_no_title);//Settings App no caption This.getwindow (). SetFlags (windowmanager.layoutparams.flag_fullscreen,//above two settings must be placed before Setcontentview, Otherwise there will be abnormal WindowManager.LayoutParams.FLAG_FULLSCREEN); Setcontentview (R.layout.activity_main); new Handler (). Postdelayed (New Runnable () {@Overridepublic void Run () {//TODO auto-generated method Stubintent Intent = new Intent (MainA Ctivity.this, Two.class); startactivity (intent); Finish ();}}, Sight_time);}}
Second screen Java file
Package Com.example.message4;import Android.app.activity;import Android.os.bundle;public Class A extends Activity {@ overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate ( Savedinstancestate); Super.setcontentview (R.layout.main);}}
Automatically enters the main interface after 3 seconds
Of course, the implementation of the first screen effect can be implemented using a variety of methods, where the knowledge of one, the reader can also directly use the thread to operate.
Forecast for next section: Service services
Learn the android<message message mechanism from scratch. 42 .>