Android中很重要的一个机制就是线程+消息,当然线程并不是android独有的,下面,简单的说说使用线程的时候应该注意的地方
我们采用最简单的方法来建立一个android的线程+消息的例子
1.Thread + Handler
[java]
复制代码 代码如下:
package com.example.test_thread;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView mTextView = null;
// static TextView mTextView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView)findViewById(R.id.textview);
Thread th = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0;i<1000;i++)
{
try {
Thread.sleep(500);
System.out.println("Thread running :"+i+"!");
Message msg = new Message();
msg.what = i;
mHandler.sendMessage(msg);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
th.start();
}
public Handler mHandler = new Handler(){
// public static Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
mTextView.setText(String.valueOf(msg.what));
}
};
}
package com.example.test_thread;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView mTextView = null;
// static TextView mTextView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView)findViewById(R.id.textview);
Thread th = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0;i<1000;i++)
{
try {
Thread.sleep(500);
System.out.println("Thread running :"+i+"!");
Message msg = new Message();
msg.what = i;
mHandler.sendMessage(msg);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
th.start();
}
public Handler mHandler = new Handler(){
// public static Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
mTextView.setText(String.valueOf(msg.what));
}
};
}
当我们用以上方式建立线程时,进入应用之后,线程开始运行,Handler接收消息改变UI中的TextView,此时一切正常
当按下退出时,程序退出,但是程序进程还在stack中,因此主线程之子线程,也就是我们定义的th(th_1)不会退出,此时,在log信息中可以看到,system.out还在print数字
当再次进入程序的时候,可以看到,log中打印的信息double,但是UI会按照新线程(th_2)的次序改变
此时th_1仍在运行,th_1使用的 handler_1也在运行,只不过上一个Activity的状态已经是finish,因此不会改变UI this ->mFinished= true
其实只要th_1中有关于上一个Activity的引用,那么Activity就不会销毁,java的机制就是这样,这是我们推荐的线程机制,下面着重说一下可能遇到的问题
2.同样是刚刚的例子,我们将Handler定义成static
[java]
public static Handler mHandler = new Handler(){
public static Handler mHandler = new Handler(){此时,在退出应用再重新进入时,由于Handler并不会有新的实例,因此,th_1与th_2同时发消息给一个static Handler 或者说是指向了同一块内存区域,这时就会出现TextView上的数字来回跳的现象
3.这样也可以
使用static定义Handler也不是不可以,只要在Activity的onCreate()中重新实例一个Handler,这样,JVM分配另一块内存给新的Handler,这样运行就正常了
[java]
复制代码 代码如下:
package com.example.test_thread;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
public class MainActivity extends Activity {
public Handler mHandler = null;
TextView mTextView = null;
// static TextView mTextView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView)findViewById(R.id.textview);
mHandler = new TestHandler();
Thread th = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0;i<1000;i++)
{
try {
Thread.sleep(500);
System.out.println("Thread running :"+i+"!");
Message msg = new Message();
msg.what = i;
mHandler.sendMessage(msg);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
th.start();
}
class TestHandler extends Handler
{
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
System.out.println("Handler running :"+msg.what+"!");
mTextView.setText(String.valueOf(msg.what));
}
}
}
package com.example.test_thread;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
public class MainActivity extends Activity {
public Handler mHandler = null;
TextView mTextView = null;
// static TextView mTextView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView)findViewById(R.id.textview);
mHandler = new TestHandler();
Thread th = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0;i<1000;i++)
{
try {
Thread.sleep(500);
System.out.println("Thread running :"+i+"!");
Message msg = new Message();
msg.what = i;
mHandler.sendMessage(msg);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
th.start();
}
class TestHandler extends Handler
{
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
System.out.println("Handler running :"+msg.what+"!");
mTextView.setText(String.valueOf(msg.what));
}
}
}
当然,总的来说Java还是不推荐使用static变量的,这本身也不符合面向对象的变成思想,所以,建议除了一些final值,尽量还是多使用消息机制来解决问题,维护也轻松些