Android 开发中的 Handler ,Thread ,Message ,Runnable 的综合使用方法

边做边学的方法。

多线程更新UI的方法

layout  的布局文件

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    >  
    <TextView  
        android:id="@+id/text"    
        android:layout_width="fill_parent"   
        android:layout_height="wrap_content"   
        android:text="@string/hello"  
        />  
         <TextView  
        android:id="@+id/text2"    
        android:layout_width="fill_parent"   
        android:layout_height="wrap_content"   
        android:text="@string/hello"  
        />  
    <Button  
        android:id="@+id/startservice"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="startThread1"  
    />  
    <Button  
        android:id="@+id/stopservice"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="stopThread1"  
    />  
    <Button  
        android:id="@+id/bindservice"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="startThread2"  
    />  
    <Button  
        android:id="@+id/unbindservice"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="stopThread2"  
    />  
     <Button  
        android:id="@+id/over"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="over"  
    />  
</LinearLayout>


======================2个textView 一个用来显示时间,一个计数==========================


代码如下:

package com.dhanzhang;


import java.text.SimpleDateFormat;
import java.util.Date;


import android.app.Activity;
 
import android.os.Bundle;
import android.os.Handler;
 
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class TestHandler extends Activity implements OnClickListener {


private TextView mTextView;
private TextView mTextView2;
private Button startServiceButton;
private Button stopServiceButton;
private Button bindServiceButton;
private Button unbindServiceButton;
private Button over;


protected static final int GUIUPDATEIDENTIFIER = 0x101;
int i = 0;


Thread myRefreshThread = null;
Handler handler2 = new Handler();


// 1.定义一个Handler(一般更新View)
Handler myHandler = new Handler() {
// 2.重写消息处理函数
public void handleMessage(Message msg) {
switch (msg.what) {
// 判断发送的消息
case TestHandler.GUIUPDATEIDENTIFIER:
// 更新View
Date d = new Date(System.currentTimeMillis());
SimpleDateFormat sf = new SimpleDateFormat(
"yyyy-MM-dd hh:mm:ss.SSS");
mTextView.setText(String.format("Runnable:%s ", sf.format(d)));
break;
}
super.handleMessage(msg);
}
};
Runnable runnable = new Runnable() {
@Override
public void run() {
mTextView2.setText(String.valueOf(i++));
handler2.postDelayed(runnable, 1000);
}
};




class myThread implements Runnable {
public void run() {
while (!Thread.currentThread().isInterrupted()) {
// 3.发送消息
Message message = new Message();
// 发送消息与处理函数里一致
message.what = TestHandler.GUIUPDATEIDENTIFIER;
// 内部类调用外部类的变量
TestHandler.this.myHandler.sendMessage(message);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}


public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
setupViews();
}


public void setupViews() {


mTextView = (TextView) findViewById(R.id.text);
mTextView2 = (TextView) findViewById(R.id.text2);
mTextView.setText("Begin To");
mTextView2.setText("Y");
startServiceButton = (Button) findViewById(R.id.startservice);
stopServiceButton = (Button) findViewById(R.id.stopservice);
bindServiceButton = (Button) findViewById(R.id.bindservice);
unbindServiceButton = (Button) findViewById(R.id.unbindservice);


over = (Button) findViewById(R.id.over);


startServiceButton.setOnClickListener(this);
stopServiceButton.setOnClickListener(this);
bindServiceButton.setOnClickListener(this);
unbindServiceButton.setOnClickListener(this);
over.setOnClickListener(new View.OnClickListener() {


@Override
public void onClick(View v) {
setResult(Activity.RESULT_OK);
finish();
}
});
}


@Override
public void onDestroy() {
super.onDestroy();
if (myRefreshThread != null) {
myRefreshThread.interrupt();
myRefreshThread.stop();
}
 
handler2.removeCallbacks(runnable) ;
}


public void onClick(View v) {


if (v == startServiceButton) {
myRefreshThread = new Thread(new myThread());
myRefreshThread.start();
} else if (v == stopServiceButton) {
myRefreshThread.interrupt();
} else if (v == bindServiceButton) {
handler2.post(runnable); 
} else if( v== unbindServiceButton)  {
handler2.removeCallbacks(runnable) ;
}
}


}


====================PS:===============

onDestroy()的重写可能有问题,不过在虚拟机里看不出来啥问题,不知道实际情况会如何。

Android 版本是2.2 

Runnable 接口并非真正的开启了线程(具体的请参见: http://www.cnblogs.com/ghj1976/archive/2011/05/06/2038516.html 这篇文章)


posted on 2011-08-19 12:15  老代哥哥  阅读(744)  评论(0编辑  收藏  举报

导航