13 thoughts on “Android – Proper way to cancel AsyncTask

  1. androidexperts

    u can check network connection by

    private boolean checkInternetConnection() {

    ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    if (conMgr.getActiveNetworkInfo() != null
    && conMgr.getActiveNetworkInfo().isAvailable()
    && conMgr.getActiveNetworkInfo().isConnected()) {

    return true;

    } else {
    Toast.makeText(Login.this, “Internet Connection Not Present”, Toast.LENGTH_LONG).show();
    Log.v(TAG, “Internet Connection Not Present”);
    return false;

    1. Vikas Post author

      This work well on device. But do some other testing. Try to disable network adapter of pc, then check it in emulator. This method will not work as it is still connected to i.e. pc’s localhost!

  2. androidexperts

    hi, I m not getting why u want to disable LAN connection,any way as per my knowledge, check ur logcat ,if you are getting exception of “java.net.UnknownHostException: http://www.urweb.com“, and you can handle this by try{ urLoginProcess….. }catch (IOException e) {e.printtrace();}
    and one more tips if u use my “checkInternetConnection()” in thread then it be faster as like

    t = new Thread() {
    public void run() {

    and one more thing ur link “here” is not working plz check it, i ll try if i can give you better solution

      1. androidexperts

        OK, Its working, but sry I can’t help you more on regarding current topic, bcz as per my above suggestion i ve tried with above my code and it working perfect(not FC) even if LAN is disable… any way all D best

  3. Pingback: Cancel asynctask download on backbutton press : Android Community - For Application Development

  4. Pingback: Json can not be convert to int : Android Community - For Application Development

  5. Pingback: When I developing an Android application ,how can I cancel a running AsyncTask? : Android Community - For Application Development

  6. sonu

    hi..i am new to android development, i m using mysql as backend.servlet(webservice) to communicate with database.i tried your login code.u have mentioned some code like as parameter to WebServiceURL,””/cfc/iphonewebservice.cfc?returnformat=json&method=validateUserLogin&username=”.
    what is it actually??webservice page URL after running on browser??

  7. Frank

    ” if (isCancelled()) {
    return (null); // don’t forget to terminate this method
    in this statement the publishProgress(CANCELLED) is never called when the task is cancelled. But it does call the @Override onCancelled() method for the async task.
    Can you explain if this is true.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s