在Android中Thread可是经常要用到的啊。
一、建立一个简单的Thread
初始界面如下:
![](https://i-blog.csdnimg.cn/blog_migrate/15087b7e9d958fae9620640fdfb94cd6.png)
点击Test按钮后,开启一个线程。这个线程,将“Hello World...”字符串修改成另外一个字符串。如下图所示。
![](https://i-blog.csdnimg.cn/blog_migrate/b28eabba64fa7b595dc7d8c3d1832856.png)
布局文件如下:
<?
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" >
< TextView
android: layout_width = "fill_parent"
android: layout_height = "wrap_content"
android: text = "@string/hello"
android: id = "@+id/txtShow" />
< Button android: id = "@+id/btnTest"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: text = "Test" />
</ LinearLayout >
< LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
android: layout_width = "fill_parent"
android: layout_height = "fill_parent"
android: orientation = "vertical" >
< TextView
android: layout_width = "fill_parent"
android: layout_height = "wrap_content"
android: text = "@string/hello"
android: id = "@+id/txtShow" />
< Button android: id = "@+id/btnTest"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: text = "Test" />
</ LinearLayout >
源代码如下:
public
class
TestThreadActivity
extends
Activity
{
private Handler mHandler;
private TextView mTextView;
private Button mButton;
@Override
public void onCreate( Bundle savedInstanceState) {
super . onCreate( savedInstanceState);
setContentView( R . layout . main);
mTextView = ( TextView) findViewById( R . id . txtShow);
mButton = ( Button) findViewById( R . id . btnTest);
mButton . setOnClickListener( new View . OnClickListener() {
public void onClick( View v) {
startThread();
}
});
}
private void startThread()
{
mHandler = new Handler();
private Handler mHandler;
private TextView mTextView;
private Button mButton;
@Override
public void onCreate( Bundle savedInstanceState) {
super . onCreate( savedInstanceState);
setContentView( R . layout . main);
mTextView = ( TextView) findViewById( R . id . txtShow);
mButton = ( Button) findViewById( R . id . btnTest);
mButton . setOnClickListener( new View . OnClickListener() {
public void onClick( View v) {
startThread();
}
});
}
private void startThread()
{
mHandler = new Handler();
//1秒刷新一次
mHandler . postDelayed ( changeText , 1000 );
}
private Runnable changeText = new Runnable () {
public void run ()
{
mTextView . setText ( "This is a thread example." );
}
};
}
mHandler . postDelayed ( changeText , 1000 );
}
private Runnable changeText = new Runnable () {
public void run ()
{
mTextView . setText ( "This is a thread example." );
}
};
}
这就是一个简单的线程例子了。
二、如何释放线程。
很简单。调用removeCallbacks就可以了。源代码如下:
@Override
public void onDestroy()
{
if( mHandler != null)
{
try
{
mHandler . removeCallbacks( changeText);
}
catch( Exception e)
{
//do nothing, don't crash
}
}
super . onDestroy();
}
public void onDestroy()
{
if( mHandler != null)
{
try
{
mHandler . removeCallbacks( changeText);
}
catch( Exception e)
{
//do nothing, don't crash
}
}
super . onDestroy();
}
三、Thread+Message+Handler实现同样效果
和上面同样的效果。点击按钮后,改变文字。Layout不变,源代码如下:
public
class
TestThreadActivity
extends
Activity
{
private Handler mHandler;
private TextView mTextView;
private Button mButton;
@Override
public void onCreate( Bundle savedInstanceState) {
super . onCreate( savedInstanceState);
setContentView( R . layout . main);
mTextView = ( TextView) findViewById( R . id . txtShow);
mButton = ( Button) findViewById( R . id . btnTest);
mButton . setOnClickListener( new View . OnClickListener() {
public void onClick( View v) {
TestMessageThread thread = new TestMessageThread();
thread . start();
mHandler = new Handler()
{
@Override
public void handleMessage( Message msg)
{
mTextView . setText(( msg . obj ). toString());
}
};
}
});
}
private class TestMessageThread extends Thread
{
@Override
public void run()
{
//do something
Message msg = new Message();
msg . obj = "This is a thread example.";
//send message
mHandler . sendMessage( msg);
}
}
}
private Handler mHandler;
private TextView mTextView;
private Button mButton;
@Override
public void onCreate( Bundle savedInstanceState) {
super . onCreate( savedInstanceState);
setContentView( R . layout . main);
mTextView = ( TextView) findViewById( R . id . txtShow);
mButton = ( Button) findViewById( R . id . btnTest);
mButton . setOnClickListener( new View . OnClickListener() {
public void onClick( View v) {
TestMessageThread thread = new TestMessageThread();
thread . start();
mHandler = new Handler()
{
@Override
public void handleMessage( Message msg)
{
mTextView . setText(( msg . obj ). toString());
}
};
}
});
}
private class TestMessageThread extends Thread
{
@Override
public void run()
{
//do something
Message msg = new Message();
msg . obj = "This is a thread example.";
//send message
mHandler . sendMessage( msg);
}
}
}
如何释放这里用到的线程呢?这就要用到线程的interrupt函数了。源代码如下: public class TestThreadActivity extends Activity {
private
Handler
mHandler
=
null;;
private TestMessageThread mThread = null;
private TextView mTextView;
private Button mButton;
@Override
public void onCreate( Bundle savedInstanceState) {
super . onCreate( savedInstanceState);
setContentView( R . layout . main);
mTextView = ( TextView) findViewById( R . id . txtShow);
mButton = ( Button) findViewById( R . id . btnTest);
mButton . setOnClickListener( new View . OnClickListener() {
public void onClick( View v) {
//startThread();
mThread = new TestMessageThread();
mThread . start();
mHandler = new Handler()
{
@Override
public void handleMessage( Message msg)
{
mTextView . setText(( msg . obj ). toString());
}
};
}
});
}
@Override
public void onDestroy()
{
super . onDestroy();
if( mThread != null)
{
try
{
mThread . interrupt();
}
catch( Exception e)
{
//do nothing, don't crash
}
}
}
private class TestMessageThread extends Thread
{
@Override
public void run()
{
//do something
Message msg = new Message();
msg . obj = "This is a thread example.";
//send message
mHandler . sendMessage( msg);
}
}
}
private TestMessageThread mThread = null;
private TextView mTextView;
private Button mButton;
@Override
public void onCreate( Bundle savedInstanceState) {
super . onCreate( savedInstanceState);
setContentView( R . layout . main);
mTextView = ( TextView) findViewById( R . id . txtShow);
mButton = ( Button) findViewById( R . id . btnTest);
mButton . setOnClickListener( new View . OnClickListener() {
public void onClick( View v) {
//startThread();
mThread = new TestMessageThread();
mThread . start();
mHandler = new Handler()
{
@Override
public void handleMessage( Message msg)
{
mTextView . setText(( msg . obj ). toString());
}
};
}
});
}
@Override
public void onDestroy()
{
super . onDestroy();
if( mThread != null)
{
try
{
mThread . interrupt();
}
catch( Exception e)
{
//do nothing, don't crash
}
}
}
private class TestMessageThread extends Thread
{
@Override
public void run()
{
//do something
Message msg = new Message();
msg . obj = "This is a thread example.";
//send message
mHandler . sendMessage( msg);
}
}
}