Android 如何在特定时间后终止异步任务

Android 如何在特定时间后终止异步任务,android,android-asynctask,Android,Android Asynctask,如何在特定时间后终止异步任务 这是我的密码 public class EnterTextAsyc extends AsyncTask<Integer, Integer, Integer> { /* displays the progress dialog untill background task is completed */ @Override protected void onPreExecute() { progressDialog

如何在特定时间后终止异步任务

这是我的密码

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;
    }
公共类EnterTextAsyc扩展异步任务{
/*显示“进度”对话框,直到后台任务完成*/
@凌驾
受保护的void onPreExecute(){
progressDialog(上下文“请稍候!!!…”);
super.onPreExecute();
}
/*EnterTextAsyc在后台执行的任务*/
@SuppressLint(“NewApi”)@覆盖
受保护的整数doInBackground(整数…参数){
试一试{
套接字=新套接字(服务器IP、服务器端口);
out=新的打印流(socket.getOutputStream(),true);
in=新的BufferedReader(新的InputStreamReader(
getInputStream());
out.write((“BE”).getBytes();
ServerData=in.readLine();
if(ServerData==null | | ServerData.equalsIgnoreCase(“”){
ServerData=“IsNull”;
}
}捕获(NetworkOnMainThreade异常){
ServerData=“IsNull”;
}捕获(IOEX异常){
ServerData=“IsNull”;
}
返回null;
}
如果服务器没有响应,如何设置超时消息(ServerData=in.readLine();)

即使将mayInterrupt传递为true,连接仍在执行,您也可以创建用于取消联机连接的新方法,然后为您创建
super.cancel(true);
。有关覆盖的详细信息和一些方法-
onCanceled()
onCanceled(布尔结果)


PS在执行过程中取消
Socket
连接可能会很困难,请执行线程。可以尝试
HttpClient
HTTPConnection

您可以使用计时器或线程,运行时可以倒计时,在x ms后可以终止异步任务。此处的超时处理应在Socket中设置直接。此解决方案解决了我的问题,但我无法取消套接字连接,它的运行背景。请帮助我?就像我在上一段中建议的那样:使用HttpClient或HttpConnection或其他“可取消”方法。简单httpClient和GET方法的示例:我不想使用另一个连接,没有办法取消套接字连接???。我正在使用socket.close();但此代码不适用于mi。
socket
连接只能由服务器端取消,因为我知道…有
socket.close();
方法,但仅标记
isClosed()的连接。
。检查并执行线程,可能会对您有所帮助
Handler h;
private static final int DELAY=3000; //milis

@Override
protected void onPreExecute() {
    h = new Handler();
    h.postDelayed(new Runnable(){
        public void run() {
            if(h!=null)
                h.removeCallbacksAndMessages(null);
            cancelAfterTimeout(true);
        }
    }, DELAY);
    progressDialog(context, "Please Wait !!!!!...");
    super.onPreExecute();
}

private void cancelAfterTimeout(boolean interrupt){
    //cancel/force interrupt/dissable/remove connection here
    //which you are initializing in doInBackground
    cancel(interrupt);
}

@Override
protected void onPostExecute(Boolean result) {
    if(h!=null)
        h.removeCallbacksAndMessages(null);
}