Search code examples
androidandroid-asynctaskandroid-activityandroid-dialog

AsyncTask with Dialog to internet unavailable then end entire app


I'm developing an app that makes requests to a web service more often. What I want to do is, when internet is not available or disconnected then instead of proceeding to request; app should show a message indicating unavailability of internet and kill itself. I've this code working fine with me -

public GetXMLFromServer(Context context, GetXMLCallback gc,
          ArrayList<NameValuePair> params, String message) {
    this.context = context;
    this.gc = gc;
    this.postParameters = params;
    progressDialog = new ProgressDialog(this.context);
    this.message = message;
}

protected void onPreExecute() {
    progressDialog.setTitle("Please Wait");
    progressDialog.setMessage(message);
    progressDialog.show();
}

@Override
protected void onPostExecute(String result) {
    progressDialog.dismiss();
    gc.onSuccess(result);   
}

@Override
protected String doInBackground(String... params) {
    String response = null;     
    try{
        ConnectivityManager cm = (ConnectivityManager) this.context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {         
        } else if (netInfo == null) {
            AlertDialog alertDialog = new AlertDialog.Builder(this.context).create();
            alertDialog.setTitle("Connection Problem");
            alertDialog.setMessage("You are not connected to Internet");
            alertDialog.setButton("Exit",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {                                
                            isAvailable=false;
                            return;
                        }
                    });
            alertDialog.show();
        }

        /*if(isAvailable==false){
            then finish the calling activity or entire app.
        }*/
    }catch(Exception e){

    }
    try {
        response = CustomHttpClient.executeHttpPost(this.urlToFetch,
                this.postParameters);           
    } catch (Exception e) {
        e.printStackTrace();
    }
    return response;
}
  }`

Now my problem is, I can't figure out how to end entire app or calling activity before making a request and crashing the app because of there is unavailability of internet. (Commented part of code is where I want to stop app and end it)


Solution

  • The first question I have is, why don't you check the network connectivity before you start the AsyncTask()?

    Given that, here's what you need to do if you still want to include it in the AsyncTask(). What you want to do is cancel the AsyncTask. There's two keys to doing this.

    1. Call the cancel() function.
    2. Check in appropriate locations (doInBackground(...) within the AsyncTask isCancelled(). If you are canceled, then end the function as soon as possible.

    As far as letting the user know, I suggest you pop up a Dialog, and in the onClick() method in the dialog, finish() the activity.

    It might be easier to do this if you put your dialog in an onCreateDialog(), as is shown in the documentation. Then the parent activity will be the application, not an AsyncTask, and thus the finish() statement will be correct.