Skip to content

Instantly share code, notes, and snippets.

@hitenpratap
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hitenpratap/d0d26314fce9b59e1370 to your computer and use it in GitHub Desktop.
Save hitenpratap/d0d26314fce9b59e1370 to your computer and use it in GitHub Desktop.
Validate Intenet Connection in Android
package com.hprog99;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class ValidateInternetConnection {
private Context ctx;
public ValidateInternetConnection(Context context){
this.ctx = context;
}
public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
Toast.makeText(context, "Internet is connected",
Toast.LENGTH_SHORT).show();
return true;
}
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment