2012年3月18日 星期日

android -- Handler


按下按鈕後,會在新啟動的執行緒中向主執行緒發Handler訊息,主執行緒收到訊息後會根據訊息的內容修改TextView的顯示內容。



public class HandlerActivity extends Activity {
    /** Called when the activity is first created. */

public static final int UPDATE_DATA = 0;
public static final int UPDAT_COMPLETE = 1;
TextView tv;
Button btnStart;
//Handler物件
Handler myHandler = new Handler () {

@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
//super.handleMessage(msg);
switch (msg.what) {
case UPDATE_DATA:
tv.setText("Handler物件 " + msg.arg1 + "%...");
break;
case UPDATE_COMPLETE:
tv.setText("已完成來自執行緒的更新資料!");
break;
}
}

};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
     
        tv = (TextView)findViewById(R.id.tv);
        btnStart = (Button)findViewById(R.id.btnStart);
     
        btnStart.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//啟動一個新執行緒
new Thread () {

@Override
public void run() {
// TODO Auto-generated method stub
//super.run();
for (int i = 0; i < 100; i++) {
try {
Thread.sleep(150);
} catch (Exception e) {
e.printStackTrace();
}
Message m = myHandler.obtainMessage();
m.what = UPDATE_DATA; //控制Handler的switch
m.arg1 = i + 1;
myHandler.sendMessage(m);//發出Message物件
}
myHandler.sendEmptyMessage(UPDATE_COMPLETE);//發更新完畢
}// end of run

}.start();
}
});
    }
}

******************************************************
res/layout/main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
    android:id="@+id/btnStart"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="點按開始更新來自執行緒的資料"
    />
<TextView
android:id="@+id/tv"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="等待更新來自執行緒的資料..."
   />

</LinearLayout>


**********************************************************

Optional: 以物件代替數字:

public class HandlerDemo extends Activity {
    /** Called when the activity is first created. */

public static final int UPDATE_DATA = 0;
public static final int UPDATE_COMPLETE = 1;
TextView tv;
Button btnStart;
//Handler物件
Handler myHandler = new Handler () {

@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
//super.handleMessage(msg);
switch (msg.what) {
case UPDATE_DATA:

tv.setText("Handler物件 : " + msg.obj);
break;
case UPDATE_COMPLETE:
tv.setText("已完成來自執行緒的更新資料!");
break;
}
}

};

String [] showWords= {"Blue Mountain", "Cappuccino", "Machiato", "Expresso", "Latte"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
     
        tv = (TextView)findViewById(R.id.tv);
        btnStart = (Button)findViewById(R.id.btnStart);
     
        btnStart.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//啟動一個新執行緒
new Thread () {

@Override
public void run() {
// TODO Auto-generated method stub
//super.run();
for (int i = 0; i < showWords.length; i++) {
try {
Thread.sleep(1500);
} catch (Exception e) {
e.printStackTrace();
}
Message m = myHandler.obtainMessage();
m.what = UPDATE_DATA;

m.obj = showWords[i].toString();
myHandler.sendMessage(m);//發出Message物件

//緩衝一下,以便看到最後一個字
try {
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
}
myHandler.sendEmptyMessage(UPDATE_COMPLETE);//發更新完畢
}// end of run

}.start();
}
});
    }
}


沒有留言:

張貼留言