【发布时间】:2014-12-08 22:11:03
【问题描述】:
如何在特定时间后终止异步任务。
这是我的代码
public class EnterTextAsyc extends AsyncTask<Integer, Integer, Integer> {
/* displays the progress dialog untill background task is completed */
@Override
protected void onPreExecute() {
progressDialog(context, "Please Wait !!!!!...");
super.onPreExecute();
}
/* Task of EnterTextAsyc performing in the background */
@SuppressLint("NewApi") @Override
protected Integer doInBackground(Integer... params) {
try {
socket = new Socket(SERVER_IP, SERVERPORT);
out = new PrintStream(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
out.write(("BE").getBytes());
ServerData = in.readLine();
if (ServerData == null || ServerData.equalsIgnoreCase("")) {
ServerData = "IsNull";
}
} catch (NetworkOnMainThreadException e) {
ServerData = "IsNull";
} catch (IOException ex) {
ServerData = "IsNull";
}
return null;
}
如果服务器没有响应,如何设置超时消息(ServerData = in.readLine();)。
【问题讨论】:
-
你可以使用定时器或线程,可以运行来倒计时你想要的时间,x毫秒后你可以杀死异步任务
-
这里对超时的处理应该直接在socket中设置。