Learn android from scratch (Message mechanism .)

Source: Internet
Author: User

Learn android from scratch (Message mechanism .)
The main function of ndroid. OS. Message is to encapsulate messages and specify the Message operation form.

No. Variable or Method Type Description
1 Public int what Variable Defines the operation of the Message.
2 Public Object obj Variable Used to define the information data passed by this Message
3 Public int arg1 Variable It is rarely used to transmit integer data.
4 Public int arg2 Variable It is rarely used to transmit integer data.
5 Public Handler getTarget () Normal Gets the Handler object that operates the message.

You can use Handler and Message to process time-consuming operations in the background and finally update them to the foreground UI.


Use Hander and Message to download network images
     
      
  
 


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. butto N; import android. widget. imageView; public class MainActivity extends Activity {public static final String URL = "http://www.baidu.com/img/baidu_sylogo1.gif"; public 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); dialog. setTitle ("loading images"); dialog. setMessage ("loading... please wait ...... "); Dialog. setCancelable (false); button. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubnew Thread (new MyThread ()). start (); dialog. show () ;}}); // handler updated by the front-end UI = new Handler () {@ Overridepublic void handleMessage (Message msg) {// TODO Auto-generated method stub // obtain Bitmap image Bitmap bitmap = (Bitmap) msg. obj; // set the imageView resource for the image. setImageBitmap (bitmap); // checks whether the image is obtained, and closes the dialog box if (msg. what = SUCCESS_CODE) {dialog. dismiss () ;}};}// background Thread class MyThread implements Runnable {@ Overridepublic void run () {Bitmap bitmap = null; // TODO Auto-generated method stubtry {URL url = new URL (URL); // create the Url object InputStream inputStream = url. openStream (); // obtain the input stream bitmap = BitmapFactory. decodeStream (inputStream); // converts the input stream to a Bitmap object} catch (MalformedURLException e) {// TODO Auto-generated catch blocke. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();} // creates a Message object and returns Message message = Message. obtain (); message. obj = bitmap; message. what = SUCCESS_CODE; handler. sendMessage (message );}}}







Next, let's start from the basics.

     
      
  
 

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 {private Button button; private TextView textView; private Handler handler; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); super. setContentView (R. layout. activity_main); button = (Button) this. findViewById (R. id. button1); textView = (TextView) this. findViewById (R.id.info); handler = new Handler () {@ Overridepublic void handleMessage (Message msg) {// TODO Auto-generated method stubint what = msg. what; int arg1 = msg. arg1; int arg2 = msg. arg2; Object obj = msg. obj; List
 
  
List = null; Bundle bundle = null; bundle = msg. getData (); // accept Bundle data list = bundle. getIntegerArrayList ("num"); // parse data for (Integer num: list) {System. out. print ("-- list:" + num);} System. out. println ("what:" + what + ", arg1:" + arg1 + ", arg2:" + arg2 + ". obj: "+ obj) ;}}; button. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubnew Thread (new MyThread ()). start () ;}});} class MyThread implements Runnable {@ Overridepublic void run () {// TODO Auto-generated method stub // Message message = new Message (); // message. arg1 = 1; // message. arg2 = 2; // message. obj = 3; // message. what = 4; // handler. sendMessage (message); // method 2 // Message message = Message. obtain (); // message. what = 1; // message. arg1 = 2; // message. arg2 = 3; // message. obj = 4; // handler. sendMessage (message); // method 3 // Message message = Message. obtain (handler); // message. what = 1; // message. arg1 = 2; // message. arg2 = 3; // message. obj = 4; // message. sendToTarget (); // Method 4 // Message message = Message. obtain (handler, 1); // message. arg1 = 2; // message. arg2 = 3; // message. obj = 4; // message. sendToTarget (); // Method 5 // Message message = Message. obtain (handler, 1, 4); // message. arg1 = 2; // message. arg2 = 3; // message. sendToTarget (); // Method 6 // Message message = Message. obtain (handler, 1, 2, 3); // message. obj = 4; // message. sendToTarget (); // Method 7 // Message message = Message. obtain (handler, 1, 2, 3, 4); // message. sendToTarget (); // Method 8 Message message = Message. obtain (handler, 1, 2, 3, 4); Bundle bundle = new Bundle (); List
  
   
List = new ArrayList
   
    
(); List. add (5); list. add (6); list. add (7); bundle. putIntegerArrayList ("num", (ArrayList
    
     
) List); message. setData (bundle); message. sendToTarget ();}}}
    
   
  
 

We only provide the demonstration of the last method.

Open logcat and display it in the System. out filter.








Use Send and psot to Send messages

<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vc3Ryb25nPgo8c3Ryb25nPjxicj4KPC9zdHJvbmc + CjxzdHJvbmc + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20140913/2014091308421632.png" alt = "\">
After you click "get information", it will be output in the Console 3 seconds later.





DEMO uses hander to implement the first screen of the application
The pos method is mainly used for delayed operations. Of course, this operation requires preparing an image as the background.
First screen xml
     
  
 

Main Interface xml
 
     
  
 


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 = 3000; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); requestWindowFeature (Window. FEATURE_NO_TITLE); // you can specify whether the application has a title. getWindow (). setFlags (WindowManager. layoutParams. FLAG_FULLSCREEN, // both of the preceding settings must be placed before setContentView. Otherwise, WindowManager may be abnormal. layoutParams. FLAG_FULLSCREEN); setContentView (R. layout. activity_main); new Handler (). postDelayed (new Runnable () {@ Overridepublic void run () {// TODO Auto-generated method stubIntent intent = new Intent (MainActivity. 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 Two extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);super.setContentView(R.layout.main);}}





Automatically enters the main interface 3 seconds later
Of course, you can use multiple methods to implement the first screen effect of an application. Here, you can use a thread to perform operations.

Next forecast: Service

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.