Jaison Brooks
Jaison Brooks

Reputation: 5826

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,

RESOLVED by the following:

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

Upvotes: 0

Views: 380

Answers (4)

Raghunandan
Raghunandan

Reputation: 133560

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

    }
}
}

Upvotes: 0

Jayyrus
Jayyrus

Reputation: 13051

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

Upvotes: 1

Daniel Conde Marin
Daniel Conde Marin

Reputation: 7742

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)

Upvotes: 2

Tomer B
Tomer B

Reputation: 5465

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() {
        ...
    });

Upvotes: 0

Related Questions