How to detect the Internet connection in application. Network from wi-fi and network of data plan as well as in the same blog we will see the GPS status

We can use the ConnectivityManager to check that you’re actually connected to the Internet, and if so, what type of connection is in place.

The following snippet shows how to use the ConnectivityManager to query the active network and determine if it has Internet connectivity.

 

public class ConnectionDetector {

/*** Function to check either mobile or wifi network is available or not. ***/

public static boolean networkStatus(Context context) {

          return (ConnectionDetector.isWifiAvailable(context) || ConnectionDetector.isMobileNetworkAvailable(context));

}

public static boolean isMobileNetworkAvailable(Context ctx) {

ConnectivityManager connecManager = (ConnectivityManager) ctx

.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo myNetworkInfo = connecManager

.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

            if (myNetworkInfo.isConnected()) {

                       return true;

} else {

                       return false;

}

}

public static boolean isWifiAvailable(Context ctx) {

ConnectivityManager myConnManager = (ConnectivityManager) ctx

.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo myNetworkInfo = myConnManager

.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

             if (myNetworkInfo.isConnected())

                        return true;

            else

                        return false;

}

}

It’s also possible to determine the type of Internet connection currently available.

Device connectivity can be provided by mobile data, WiMAX, Wi-Fi, and ethernet connections. By querying the type of the active network, as shown below, you can alter your refresh rate based on the bandwidth available.

The ConnectivityManager broadcasts the CONNECTIVITY_ACTION("android.net.conn.CONNECTIVITY_CHANGE") action whenever the connectivity details have changed.

 

The following code snippet will use to show the no network connection dialog.

public static void displayNoNetworkDialog(final Activity activity) {

AlertDialog.Builder builder = new AlertDialog.Builder(activity);

builder.setMessage(“No connection”)

.setCancelable(false)

.setPositiveButton(“Exit”,

  new DialogInterface.OnClickListener() {

  public void onClick(DialogInterface dialog, int id) {

dialog.cancel();

activity.finish();

}

})

.setNegativeButton(“Retry”,

  new DialogInterface.OnClickListener() {

  public void onClick(DialogInterface dialog, int id) {

Intent intent = activity.getIntent();

activity.finish();

activity.startActivity(intent);

dialog.cancel();

}

});

AlertDialog alertDialog = builder.create();

alertDialog.show();

}

 

 

To get the GPS Status we can use the following code snippet:

      public static boolean isGpsEnabled(Context ctx) {

LocationManager locationManager = (LocationManager) ctx

.getSystemService(Context.LOCATION_SERVICE);

         if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))

                     return true;

         else

                    return false;

}

Don’t forget to add the following code into your manifest file.

<!– Internet Permissions –>

<uses-permission android:name=”android.permission.INTERNET” />

<!– Network State Permissions –>

<uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE” />

<!– GPS Permission  –>

<uses-permission android:name=“android.permission.ACCESS_COARSE_LOCATION” />

 

You can use it in your launching activity by writing following code:

if (!ConnectionDetector.networkStatus(getApplicationContext())) {
Manager.displayNoNetworkDialog(ListTabHomeNear.this);
}else{

// Do your internet related task here.

}

// Where displayNoNetworkDialog function is defined in Manager class.

 

If your device has no internet connection then the following  alert dialog will display.

blog1

 

Hope this will help.