我使用的代碼類似於下面的內容,除了線程是通過按鈕啓動啓動的。當我按下按鈕時,url上的數據被抓取,它將我切換到另一個活動。我遇到的問題是,當我在其他活動,並擊中後退時,我得到一個例外說,線程已經開始。我已經嘗試殺死線程,當活動加載檢查isAlive時,但它似乎不會死。有沒有人碰巧知道如何讓這個活動返回到原來的狀態,創建的線程不運行?異常導致的後退按鈕按下(Android)上仍然運行的線程
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.util.ByteArrayBuffer;
public class Iconic extends Activity {
private String html = "";
private Handler mHandler;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mHandler = new Handler();
checkUpdate.start();
}
private Thread checkUpdate = new Thread() {
public void run() {
try {
URL updateURL = new URL("http://iconic.4feets.com/update");
URLConnection conn = updateURL.openConnection();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while((current = bis.read()) != -1){
baf.append((byte)current);
}
/* Convert the Bytes read to a String. */
html = new String(baf.toByteArray());
mHandler.post(showUpdate);
} catch (Exception e) {
}
}
};
private Runnable showUpdate = new Runnable(){
public void run(){
Intent newIntent = New Intent(Iconic.this, otherClass.class);
startActivity(newIntent);
}
};
}