Hy!
My Thread.interrupt doesn't work.
Code (us is global):
//Call
us = new UpdateState(params, hup);
us.start();
//Interupt
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId()== R.id.stopthread)
{
Log.e("Kill", "Kill");
us.interrupt();
}
return super.onOptionsItemSelected(item);
}
Class:
package android.skiptvad;
import java.util.List;
import org.apache.http.NameValuePair;
import android.os.Handler;
import android.os.Message;
import android.text.NoCopySpan.Concrete;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import android.util.Log;
public class UpdateState extends Thread {
public List<NameValuePair> params;
public Handler handler;
public Handler ins;
public UpdateState(List<NameValuePair> params, final Handler handler) {
this.handler = handl开发者_开发问答er;
this.params = params;
this.ins = new Handler (){
@Override
public void handleMessage(Message msg) {
if (msg.obj.toString()!= null)
{
JSONParse json = null;
try
{
Message msg2 =new Message();
Log.e("Channel_State_Update",msg.obj.toString());
json = new JSONParse(msg.obj.toString());
String state = json.getChannelState();
Log.e("Channel_State_Send",state);
msg2.obj = state;
handler.sendMessage(msg2);
}
catch (final Exception e)
{
e.printStackTrace();
}
}
}
};
}
@Override
public void run() {
while (true)
{
if (!this.isInterrupted())
{
HttpConnection con = new HttpConnection(params, "http://surfkid.redio.de/getChannelState", this.ins);
con.start();
try {
Log.e("Sleep", "Begin");
UpdateState.this.sleep(5000);
Log.e("Sleep", "End");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
Log.e("Sleep", "Error");
e.printStackTrace();
}
}
//super.run();
}
}
}
Please help
Log:
02-15 18:50:30.317: ERROR/Sleep(10696): End
02-15 18:50:30.347: ERROR/Sleep(10696): Begin
02-15 18:50:30.677: ERROR/Channel_State_Update(10696): {"responseData":{"channelState":"0"},"responseDetails":null,"responseStatus":200}
02-15 18:50:30.677: ERROR/Channel_State_Send(10696): 0
02-15 18:50:30.677: ERROR/UPDATE(10696): 0
02-15 18:50:35.347: ERROR/Sleep(10696): End
02-15 18:50:35.357: ERROR/Sleep(10696): Begin
02-15 18:50:35.897: ERROR/Channel_State_Update(10696): {"responseData":{"channelState":"0"},"responseDetails":null,"responseStatus":200}
02-15 18:50:35.897: ERROR/Channel_State_Send(10696): 0
02-15 18:50:35.897: ERROR/UPDATE(10696): 0
02-15 18:50:36.868: ERROR/Kill(10696): Kill
02-15 18:50:36.878: ERROR/Sleep(10696): Error
02-15 18:50:36.908: ERROR/Sleep(10696): Begin
02-15 18:50:37.427: ERROR/Channel_State_Update(10696): {"responseData":{"channelState":"0"},"responseDetails":null,"responseStatus":200}
02-15 18:50:37.427: ERROR/Channel_State_Send(10696): 0
02-15 18:50:37.427: ERROR/UPDATE(10696): 0
02-15 18:50:41.909: ERROR/Sleep(10696): End
02-15 18:50:41.927: ERROR/Sleep(10696): Begin
You need to break a loop when thread is interrupted:
@Override
public void run() {
while (!this.isInterrupted()) { // Exit when thread's interrupt flag is set
HttpConnection con = new HttpConnection(params, "http://surfkid.redio.de/getChannelState", this.ins);
con.start();
try {
Log.e("Sleep", "Begin");
UpdateState.this.sleep(5000);
Log.e("Sleep", "End");
} catch (InterruptedException e) {
Log.e("Sleep", "Error");
e.printStackTrace();
// Restore interrupt flag after catching InterruptedException
// to make loop condition false
Thread.currentThread().interrupt();
}
}
}
What do you expect to happen? You have the interrupt exception in a massive while loop :) You need to write break; inside the exception part for it to exit the loop. The if !this.isInterrupted()
line only makes the loop extremely tight by eliminating the code inside, but the loop is still there on the outside.
精彩评论