这就是我的做法
创建活动的成员变量:
private BackgroundWorkerThread m_bgThread = null;
protected Handler m_bgHandler = null;
请参阅下面我的 BackgroundWorkerThread 类,然后在 onCreate() 中初始化主活动中的线程和处理程序:
// Notice we pass the Handler Constructor this because we are implementing the interface
// in our activity
m_bgHandler = new Handler(this)
m_bgThread = new BackgroudnWorkerThread();
m_bgThread.start();
确保您有您的活动implements Handler.Callback 接口,它将将此方法添加到您的活动:
@Override public boolean handleMessage(Message msg)
{
switch(msg.what){
case 0:
// Will update the UI on the Main Thread
updateUI(msg.obj);
break;
case 1:
// rejoin the Thread with the main Thread
try {
m_bgThread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
return true;
}
在每个 case 语句中,您都可以将消息传递给后台线程。
例如在您的活动中调用 sendFirstMessage():
public void sendFirstMessage(){
// Passes the 0 to the background Handler handleMessage
m_bgThread.workerHandler.obtainMessage(0).sendToTarget();
}
那么在你的 Activity 的处理程序中,handleMessage() 覆盖
case 0:
updateUI(msg.obj);
break;
然后
public void updateUI(Object obj){
text.setText(obj.toString());
// Call to tell Background Thread to shut down looper
m_bgThread.workerHandler.obtainMessage(1).sendToTarget();
// Call to tell the Main Thread no more updates needed
m_bgHandler.obtainMessage(1).sendToTarget();
}
然后创建你的后台线程类作为你的活动的子类:
private class BackgroundWorkerThread extends Thread implements Handler.Callback{
private Looper workerLooper;
protected Handler workerHandler;
@Override public void run()
{
// Set up the Handler and Looper
Looper.prepare();
workerLooper = Looper.myLooper();
workerHandler = new Handler(workerLooper, this);
Looper.loop();
}
@Override public boolean handleMessage(Message msg)
{
switch(msg.what){
case 0:
// now this function is being called to run on the background Thread
beginListenForData();
break;
case 1:
workerLooper.quit();
break;
}
return true;
}
}
// This method is now being execute in the background
public void beginListenForData()
{
int bytesAvailable = 3;
while(!Thread.currentThread().isInterrupted())
{
try{
bytesAvailable = mmInStream.available();
if(bytesAvailable > 0)
{
byte[] packetBytes = new byte[bytesAvailable];
mmInStream.read(packetBytes);
bytesAvailable = mmInStream.available();
String s = new String(packetBytes);
// Instead of trying to set the text view here, send a message back to
// the Activity to update the UI on the Main thread
// text.setText(s);
// Passes 0, and the String Object to the Activity Handler
m_bgHandler.obtainMessage(0, s).sendToTarget();
}
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这有点难以理解,但这就是它的完成方式,手动创建一个线程,使用循环器在后台检查消息,然后将消息从后台线程传递给活动以更新用户界面。
祝你好运!有任何问题欢迎随时提问。
或者你可以采取从http://android-developers.blogspot.com/2009/05/painless-threading.html获取的最简单的意识形态路线
public void beginListenForData()
{
String s = null;
new Thread(new Runnable(){
public void run(){
int bytesAvailable = 3;
while(!Thread.currentThread().isInterrupted())
{
try{
bytesAvailable = mmInStream.available();
if(bytesAvailable > 0)
{
byte[] packetBytes = new byte[bytesAvailable];
mmInStream.read(packetBytes);
bytesAvailable = mmInStream.available();
s = new String(packetBytes);
// Takes Care of updating the UI
text.post(new Runnable(){
public void run(){
text.setText(s);
}
});
}
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}