【发布时间】:2012-08-02 11:05:11
【问题描述】:
我有一个在 onStart() 方法中创建线程的活动。 Thread 用于通过 TCP 读取网络数据,并且在 while 循环中具有阻塞网络读取方法,该方法检查每个增量时的布尔变量。
我的问题是,当使用返回键销毁 Activity 时,我将布尔循环控制变量的值设置为 false,但线程无法完成,因为它卡在阻塞网络方法上。
public class MyActivity extends Activity implements Runnable
{
Thread thread;
boolean loopControl;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
protected void onStart()
{
super.onStart();
loopControl=true;
thread = new Thread(this);
thread.start();
}
public void run()
{
while(loopControl)
{
directories = (Vector<String>) TCPFunctions.inputStream.readObject();
}
}
protected void onDestroy()
{
super.onDestroy();
loopcontrol = false;
}
}
我要如何完成这样的线程,因为它会在再次启动此 Activity 时产生问题。
实际上,每次活动启动时,它都会从服务器读取一些数据
【问题讨论】:
标签: android multithreading tcp