0

how to check whether network is connected or not in android mobile phone..(network means not internet connection or data connection its simple mobile network like AIRTEL, CELLONE,VODAFONE etc..)

I tried with ConnectivityManager.TYPE_MOBILE and ConnectivityManager.TYPE_WIFI but they are used for checking data connectivity... and not network connectivity

Sachidananda naik
  • 391
  • 1
  • 3
  • 11

5 Answers5

0

You can use TelephonyManager's getSimOperator()

0
public static boolean isNetworkAvailable(Activity activity) {
    ConnectivityManager connectivity = (ConnectivityManager) activity
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity == null)
    {
        return false;
    } else {
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null) {
            for (int i = 0; i < info.length; i++) {
                if (info[i].getState() == NetworkInfo.State.CONNECTED) 
                {
                    return true;
                }
            }
        }
    }
    return false;
}

you should add this permission to manifest file

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
  • He wrote "network means not internet connection or data connection " . He's not asking about internet connection. Just simple connection to the network, meaning he can make calls and receive them – android developer Oct 17 '19 at 08:47
0

According to the Android Docs

getSimOperator()

Returns the MCC+MNC (mobile country code + mobile network code) of the provider of the SIM.

e.g. to Get operator ID( MNC+MCC) from SIM Card

 public String getOperator()
       {
            TelephonyManager manager = (TelephonyManager) mGap.getSystemService(Context.TELEPHONY_SERVICE);
            String operatorName = manager.getSimOperator();
            return operatorName; 
       }
android developer
  • 114,585
  • 152
  • 739
  • 1,270
Jitender Dev
  • 6,907
  • 2
  • 24
  • 35
0

try this :

public class ConnectionDetector {
    private static Context _context;

    public ConnectionDetector(Context context){
        this._context = context;
    }

    public static boolean isConnectingToInternet(){ boolean haveConnectedWifi = false;
        boolean haveConnectedMobile = false;

        ConnectivityManager cm = (ConnectivityManager)_context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] netInfo = cm.getAllNetworkInfo();
        for (NetworkInfo ni : netInfo) {
            if (ni.getTypeName().equalsIgnoreCase("WIFI"))
                if (ni.isConnected())
                    haveConnectedWifi = true;
            if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
                if (ni.isConnected())
                    haveConnectedMobile = true;
        }
        return haveConnectedWifi || haveConnectedMobile;}

}

add these permissions in manifest file

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
Bapusaheb Shinde
  • 839
  • 2
  • 13
  • 16
  • But he wrote "network means not internet connection or data connection " . He's not asking about internet connection. Just simple connection to the network, meaning he can make calls and receive them – android developer Oct 17 '19 at 08:43
0

In order to check the status, there are 2 ways:

  1. ServiceState constants : STATE_EMERGENCY_ONLY, STATE_IN_SERVICE, STATE_OUT_OF_SERVICE, STATE_POWER_OFF .

Those you get by calling telephonyManager.serviceState.state. If you wish to listen to changes you use PhoneStateListener by calling telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE) . To unregister you call telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE)

This is a good solution because it seems that even if you don't have a SIM card, you can still sometimes (in some countries) call emergency phone numbers, such as the police.

enum class PhoneServiceState {
    STATE_EMERGENCY_ONLY, STATE_IN_SERVICE, STATE_OUT_OF_SERVICE, STATE_POWER_OFF, UNKNOWN;

    companion object {
        fun getFromValue(value: Int): PhoneServiceState = when (value) {
            ServiceState.STATE_EMERGENCY_ONLY -> STATE_EMERGENCY_ONLY
            ServiceState.STATE_IN_SERVICE -> STATE_IN_SERVICE
            ServiceState.STATE_OUT_OF_SERVICE -> STATE_OUT_OF_SERVICE
            ServiceState.STATE_POWER_OFF -> STATE_POWER_OFF
            else -> UNKNOWN
        }
    }
}

Usage:

PhoneServiceState.getFromValue(telephonyManager!!.serviceState.state)

If it's STATE_OUT_OF_SERVICE or STATE_POWER_OFF, it means you are completely disconnected. If you have STATE_EMERGENCY_ONLY, it means you can only call emergency phone calls.

  1. You can use subscriptionManager.activeSubscriptionInfoList . If you get a non-empty list, I think it means you are connected:

    fun hasCellConnection(context: Context): Boolean {
        val subscriptionManager = context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE) as SubscriptionManager
        val activeSubscriptionInfoList = subscriptionManager.activeSubscriptionInfoList
        return !activeSubscriptionInfoList.isNullOrEmpty()
    }
    
android developer
  • 114,585
  • 152
  • 739
  • 1,270