ProgrammerGuide.Net | Step by Step Programmer Guide

Check Mobile Network Availability By Code | Android Programming | ProgrammerGuide.Net | Step by Step Guide

Below code snippet checks for the availability of network signal on Android device

public static boolean isNetworkAvailable(Context ctx) {
 ConnectivityManager connMgr = (ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
 if(connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected() ||
  connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected()){
   return true;
 }  
 return false;
 }

This function will return true if either WIFI or MOBILE network available, otherwise returns false.

ConnectivityManager

ConnectivityManager is a class used to monitor network connections (Wi-Fi, GPRS, UMTS, etc.). It exposes functionalities that allows applications to query the state of the available networks.

How to instantiate ConnectivityManager class?

ConnectivityManager class can be instantiated by calling Context.getSystemService(Context.CONNECTIVITY_SERVICE).

Please click here for related products on Amazon!

Jayashee

Add comment

Want to Check Celebrities News?