0

how to kill Async task after a specific time.

Here is My code

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;
    }

How to set a timeout message if there is no response from Server(ServerData = in.readLine();).

Anuj Zunjarrao
  • 426
  • 2
  • 14

1 Answers1

0
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);
}

even if you pass mayInterrupt as true the connection is still executing, you might create new method for cancelling online connections and then super.cancel(true);. more info and some methods for override here - onCanceled() and onCanceled(Boolean result) for you

PS canceling Socket connection while executing might be difficult, thread about this here . maybe try HttpClient or HTTPConnection?

Community
  • 1
  • 1
snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • this solution sol my problem but i am not able to cancel Socket connection, its run background. please help me? – Anuj Zunjarrao Dec 09 '14 at 11:01
  • like I suggested you in last paragraph: use HttpClient or HttpConnecion or other "cancelable" method. example for simple httpClient and GET method here: http://stackoverflow.com/questions/5400258/cancel-an-httpclient-request – snachmsm Dec 09 '14 at 12:59
  • i does not want use another connection, there is no way to cancel socket connection??. i am use socket.close(); but this code not working for mi. – Anuj Zunjarrao Dec 10 '14 at 09:44
  • `Socket` connection may be cancelled only by server-side as I know... there is `Socket.close();` method, but is only flagging connection for `isClosed()`. check [this](http://stackoverflow.com/questions/4891044/in-android-how-to-stop-a-thread-that-is-wating-for-a-new-socket) and [this](http://stackoverflow.com/questions/3701073/how-can-a-socket-be-both-connected-and-closed/3701249) thread, maybe it helps you – snachmsm Dec 10 '14 at 12:34