標籤:android style blog http io color ar os 使用
Android系統規定,一些耗時的操作不能放在UI線程中去執行,這樣會報一個ANR錯誤。所以為了避免該問題,我們需要開啟一個新的線程去執行一些耗時操作;開啟新的線程,將耗時的操作在新線程裡面去執行, 但是子線程中不能更新UI介面,所以我們使用android的Handler機制可以解決這個問題。
詳細解釋如下:
當應用程式啟動時,Android首先會開啟一個主線程 (也就是UI線程) , 主線程為管理介面中的UI控制項,進行事件分發, 比如說, 你要是點擊一個 Button ,Android會分發事件到Button上,來響應你的操作。 如果此時需要一個耗時的操作,例如: 連網讀取資料, 或者讀取本地較大的一個檔案的時候,你不能把這些操作放在主線程中,如果你放在主線程中的話,介面會出現假死現象, 如果5秒鐘還沒有完成的話,,會收到Android系統的一個錯誤提示 "強制關閉". 這個時候我們需要把這些耗時的操作,放在一個子線程中,因為子線程涉及到UI更新,Android主線程是線程不安全的,也就是說,更新UI只能在主線程中更新,子線程中操作是危險的. 這個時候,Handler就出現了.,來解決這個複雜的問題 , 由於Handler運行在主線程中(UI線程中), 它與子線程可以通過Message對象來傳遞資料, 這個時候,Handler就承擔著接受子線程傳過來的(子線程用sedMessage()方法傳弟)Message對象,(裡麵包含資料) , 把這些訊息放入主線程隊列中,配合主線程進行更新UI。
1。圖示說明:
用一個執行個體來說明:
2.XML代碼:
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context=".MainActivity" >10 11 <TextView12 android:id="@+id/msg"13 android:layout_width="wrap_content"14 android:layout_height="wrap_content" />15 16 <Button17 android:id="@+id/btn"18 android:layout_width="wrap_content"19 android:layout_height="wrap_content"20 android:text="更新UI介面" />21 22 </RelativeLayout>
3.java代碼:
1 package com.example.handler; 2 3 import android.os.Bundle; 4 import android.os.Handler; 5 import android.os.Message; 6 import android.app.Activity; 7 import android.view.Menu; 8 import android.view.View; 9 import android.view.View.OnClickListener;10 import android.widget.Button;11 import android.widget.TextView;12 13 public class MainActivity extends Activity {14 private Mythread mythread;15 private TextView mTextView;16 private Button mButton;17 public class myhandler extends Handler{18 @Override19 public void handleMessage(Message msg) {20 21 // TODO Auto-generated method stub22 super.handleMessage(msg);23 int count = msg.arg1;24 String name =(String) msg.obj;25 mTextView.setText(name);26 }27 }28 29 private final Handler handler = new Handler() {30 public void handleMessage(Message msg) {31 super.handleMessage(msg);32 String name = (String) msg.obj;33 int m = msg.arg1;34 mTextView.append(name + m);35 };36 };37 38 @Override39 protected void onCreate(Bundle savedInstanceState) {40 super.onCreate(savedInstanceState);41 setContentView(R.layout.activity_main);42 mTextView = (TextView) this.findViewById(R.id.msg);43 mButton = (Button) this.findViewById(R.id.btn);44 mButton.setOnClickListener(new OnClickListener() {45 @Override46 public void onClick(View arg0) {47 mythread = new Mythread();48 new Thread(mythread).start();49 }50 });51 }52 53 private class Mythread implements Runnable {54 @Override55 public void run() {56 int count = 0;57 while (count >= 20) {58 59 try {60 Thread.sleep(500);61 } catch (InterruptedException e) {62 // TODO Auto-generated catch block63 e.printStackTrace();64 }65 Message message = Message.obtain();//這裡雖然可以new一個訊息執行個體,但是最好別new.66 message.arg1 = count;67 message.obj = "tom";68 count++;69 handler.sendMessage(message);70 }71 }72 }73 74 @Override75 public boolean onCreateOptionsMenu(Menu menu) {76 // Inflate the menu; this adds items to the action bar if it is present.77 getMenuInflater().inflate(R.menu.main, menu);78 return true;79 }80 }
Android學習之----Handler訊息