Question

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;
}
Was it helpful?

Solution 2

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

OTHER TIPS

Since this keyword in your code is referencing DialogInterface.OnClickListener, then getSystemService is not defined for that type, so you must call getSystemService from a context variable referencing your current activity,for example, like this:

context.getSystemService(Activity.CONNECTIVITY_SERVICE)

I might be missing big time, but it seems like you're creating a DialogInterface.OnClickListener, but not assigning it to handle anything.

Change your code:

    localBuilder2.setPositiveButton(R.string.alert_dialog_ok, null);

    localBuilder2.setOnClickListener(new DialogInterface.OnClickListener() {
        ...
    });

To check network Availability

In your Activtiy

if(CheckNetwork.isInternetAvailable(ActvityName.this))// pass activity context
     {
             //do soemthing
     }  

If you need to check network availability in a non activity class you need to pass the activity context as a parameter to the class constructor and use the activity context.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);
    MyAnotherClass = new MyAnotherClass(MainActivtity.this); 

}

In your MyAnotherClass

    Class MyAnotherClass
      {
          Context context;
         public MyAnotherClass(Context activitycontext)
         {
             this.context= activitycontext;
             if(CheckNetwork.isInternetAvailable(context))
             {
                     //do something
             }
         }
      } 

CheckNetwork class

public class CheckNetwork {


private static final String TAG = CheckNetwork.class.getSimpleName();



public static boolean isInternetAvailable(Context context)
{
    NetworkInfo info = (NetworkInfo) ((ConnectivityManager)
    context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();

    if (info == null)
    {
         Log.d(TAG,"no internet connection");
         return false;
    }
    else
    {
        if(info.isConnected())
        {
            Log.d(TAG," internet connection available...");
            return true;
        }
        else
        {
            Log.d(TAG," internet connection");
            return true;
        }

    }
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top