5

I want to use a AsyncTask to check an InetAddress, such as in the code below. The variable tempInet is a global variable that indicates whether the website was contactable or not.

I begin the AsyncTask with the code... new InetAsyncTask().execute("www.facebook.com");

My problem is that I want the AsyncTask to cancel itself after (say) 10 seconds.

Some other questions suggest using the get(10, TimeUnit.SECONDS) method. I would like to do this but not sure where/how to put the get method. With execute? In the doInBackground method?

Also, does the get() method block the main thread? If it does, what is the point of it?

Any help appreciated.

 class InetAsyncTask extends AsyncTask<String, String, Boolean> {



    @Override
    protected Boolean doInBackground(String... params) {

        try {                 
                InetAddress.getByName("www.facebook.com");
                return true;          

                } catch (UnknownHostException e) {  
                  return false;
                }

    } //end doInBackground function



    protected void onPostExecute(Boolean... result) {

           tempInet = result[0];

    }


   } //end class

Related Questions

Android - Setting a Timeout for an AsyncTask?

stop async task after 60 second

Android Developers AsyncTask Documentation

http://developer.android.com/reference/android/os/AsyncTask.html

Community
  • 1
  • 1
Mel
  • 6,214
  • 10
  • 54
  • 71

2 Answers2

1

You should make a handler which cancels the Asynctask (http://developer.android.com/reference/android/os/AsyncTask.html#cancel(boolean))

Send a delayed message to this Handler like:

Handler.sendMessageDelayed(msg, delayMillis)

private android.os.Handler mHandler = new android.os.Handler() {

    @Override
    public void handleMessage(Message msg) {
        (Your AsyncTask object).cancel(true);
    }
}
Ion Aalbers
  • 7,830
  • 3
  • 37
  • 50
0

To answer your question. The code to use with get() is the following:

Boolean isHost = new AsyncTask<String, Void, Boolean>() {
            @Override
            protected Boolean doInBackground(String... params) {
                try {                 
                    InetAddress.getByName(params[0]);
                    return true;          
                } catch (UnknownHostException e) {  
                    return false;
                }
            }
        }.execute("www.facebook.com").get(PING_TIME, TimeUnit.MILLISECONDS);

The get method blocks the caller thread as long as the result needs to be returned. You should therefore only use it in test situations. If you want to get an asynchronous answer start it as follows (this is one of my examples, you'll have to change it for your purpose):

private AsyncTask<Void, Void, Boolean> mAsyncTask;

private android.os.Handler timeHandler = new android.os.Handler() {
@Override
public void handleMessage(Message msg) {
        super.handleMessage(msg);
        // react here or...
        mAsyncTask.cancel(true);
    }
};

public void pingServer() {

    timeHandler.sendEmptyMessageDelayed(1, PING_TIME);        
    mAsyncTask = new AsyncTask<Void, Void, Boolean>() {
            @Override
            protected Boolean doInBackground(Void... params) {
                // stop the time handler
                timeHandler.removeMessages(1);
                return restService.pingServer();
            }

            @Override
            protected void onPostExecute(Boolean isOnline) {
                super.onPostExecute(isOnline);
                // use result after execution (e.g.: send to callback)
            }


            @Override
            protected void onCancelled(Boolean aBoolean) {
                super.onCancelled(aBoolean);
                // ...react here
            }
        }.execute();
}
luckyhandler
  • 10,651
  • 3
  • 47
  • 64