從零開始學android<Message訊息機制.四十二.>

來源:互聯網
上載者:User

標籤:風飛雪未揚   從零開始學android   android後台資料通訊   android應用第一屏   hander message   

ndroid.os.Message的主要功能是進行訊息的封裝,並且同時可以指定訊息的操作形式。
No. 變數或方法 類型 描述
1 public int what 變數 用於定義此Message屬於何種操作
2 public Object obj 變數 用於定義此Message傳遞的資訊資料
3 public int arg1 變數 傳遞一些整型資料時使用,一般很少使用
4 public int arg2 變數 傳遞一些整型資料時使用,一般很少使用
5 public Handler getTarget() 普通 取得操作此訊息的Handler對象

可以使用 Handler 和Message處理後台耗時操作最後更新到前台UI。


使用Hander與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: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="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_centerHorizontal="true"        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:layout_alignParentBottom="true"        android:layout_centerHorizontal="true"        android:text="擷取圖片" /></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.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("載入圖片");dialog.setMessage("正在載入請稍後……");dialog.setCancelable(false);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubnew Thread(new MyThread()).start();dialog.show();}});//前台UI的更新handler = new Handler() {@Overridepublic void handleMessage(Message msg) {// TODO Auto-generated method stub//取得Bitmap圖片Bitmap bitmap = (Bitmap) msg.obj;//為圖片設定資源imageView.setImageBitmap(bitmap);//判斷圖片是否獲得,並關閉對話方塊if (msg.what == SUCCESS_CODE) {dialog.dismiss();}}};}//後台線程class MyThread implements Runnable {@Overridepublic void run() {Bitmap bitmap = null;// TODO Auto-generated method stubtry {URL url = new URL(URL);//建立Url對象InputStream inputStream = url.openStream();//獲得輸入資料流bitmap = BitmapFactory.decodeStream(inputStream);//將輸入資料流轉換為Bitmap對象} catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}//建立Message對象,返回資訊Message message = Message.obtain();message.obj = bitmap;message.what = SUCCESS_CODE;handler.sendMessage(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: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" >    <TextView        android:id="@+id/info"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="" />    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_centerHorizontal="true"        android:layout_marginBottom="150dp"        android:text="擷取資料" /></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();//接受Bundle資料<span style="white-space:pre"></span>list =bundle.getIntegerArrayList("num");//解析資料<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>// 第二種方法<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>// 第三種方法<span style="white-space:pre"></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>// 第四種方法<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>// 第五種方法<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>// 第六種方法<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>// 第七種方法<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>// 第八種方法<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>}}

我們僅給出最後一隻方法的示範

開啟logcat 在System.out的過濾器中顯示








使用Send和psot發送訊息



點擊擷取資訊後,將在3秒後在控制台輸出





DEMO  使用hander 實現應用程式的第一屏
主要使用pos方法進行延時操作當然,此操作需要實現準備一張圖片,作為背景。
第一屏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>

主介面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:orientation="vertical" >    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="主介面" /></LinearLayout>


第一屏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);//設定應用沒有標題this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,//以上兩個設定都必須放在setContentView之前,不然會出現異常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(MainActivity.this, Two.class);startActivity(intent);finish();}}, SIGHT_TIME);}}


第二屏JAVA檔案
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);}}





3秒後自動進入主介面
當然,實現應用的第一屏效果可以使用多種方法實現,在這裡知識其中的一種,讀者也可以直接使用線程進行操作。

下節預報:Service 服務

從零開始學android<Message訊息機制.四十二.>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.