Search code examples
javaandroidandroid-webviewandroid-alertdialogconnectivity

Checking Network Connectivity before dismissing Alert Dialog


So I have a webview based app. My question is that when OnReceivedError is called when there is internet loss, I'd like to

When the user clicks "OK" on the dialog for it to check for connectivity,

  • If connectivity is available then dismiss alert and call web.reload();
  • If there isn't web connectivity (wifi or mobile) then call finish();

RESOLVED by the following:

  • Changed Alert Dialog Postive button to the following

    .setPositiveButton(R.string.alert_dialog_ok,new DialogInterface.OnClickListener() {         
     public void onClick(DialogInterface dialog,int id) {
    
  • The issue was with the following

    DialogInterface paramDialogInterface,int paramInt)
    

Context context;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  this.context = this;
  ...
  @Override
public void onReceivedError(WebView view, int errorCode,
            String description, String failingUrl) {
        AlertDialog.Builder localBuilder2 = new AlertDialog.Builder(
                Webview_Main.this);
        localBuilder2.setTitle(R.string.webview_error_received_title);
        localBuilder2.setMessage(R.string.webview_error_received);
        localBuilder2.setIcon(R.drawable.ic_launcher);
        localBuilder2.setPositiveButton(R.string.alert_dialog_ok,new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                 if(isNetworkAvailible(context))
                 web.reload();
                 else
                 finish();
         }
    };
        localBuilder2.show();
    };

isNetworkAvailible Class

   public boolean isNetworkAvailible(Context ctx) {

    NetworkInfo info = (NetworkInfo) ((ConnectivityManager) ctx
            .getSystemService(Context.CONNECTIVITY_SERVICE))
            .getActiveNetworkInfo();

    if (info == null || !info.isConnected()) {
        return false;
    }
    if (info.isRoaming()) {
        // here is the roaming option you can change it if you want to
        // disable internet while roaming, just return false
        return false;
    }
    return true;
}

Solution

  • Correcting my answer:

        Context context;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
                this.context = this; 
        }
    
    
    @Override
    public void onReceivedError(WebView view, int errorCode,
                String description, String failingUrl) {
            AlertDialog.Builder localBuilder2 = new AlertDialog.Builder(
                    Webview_Main.this);
            localBuilder2.setTitle(R.string.webview_error_received_title);
            localBuilder2.setMessage(R.string.webview_error_received);
            localBuilder2.setIcon(R.drawable.ic_launcher);
            localBuilder2.setPositiveButton(R.string.alert_dialog_ok,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface paramDialogInterface,int paramInt) {
                        if(haveInternet(context))
                            web.reload();
                        else
                            finish();
                    }
                });
            localBuilder2.show();
        };
    
    public boolean haveInternet(Context ctx) {
    
        NetworkInfo info = (NetworkInfo) ((ConnectivityManager) ctx
                .getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
    
        if (info == null || !info.isConnected()) {
            return false;
        }
        if (info.isRoaming()) {
            // here is the roaming option you can change it if you want to
            // disable internet while roaming, just return false
            return false;
        }
        return true;
    }