0

In my android app I have to gather a lot of information regarding the cell tower like Network name, Network code etc etc but before I start capturing all those things I want to make sure that a cell tower connections exists i.e. GSM or CDMA, but I want to ensure that there is a connection. Otherwise I would just like to return. For example, as we do it in case of Wi Fi. With the help of WifiManager's isWifiEnabled() method we ensure first hand whether we have Wifi running or not.

Similar to that, for Cell Tower.

Rajen Raiyarela
  • 5,526
  • 4
  • 21
  • 41
Ankit
  • 4,426
  • 7
  • 45
  • 62
  • I m putting as a comment because i do not know this is a solution or not for the thing you are looking for? As described in this post http://www.anddev.org/poor_mans_gps_-_celltowerid_-_location_area_code_-lookup-t257.html you can register a broadcast receiver for phonestate and using that you can get the current CellId & LAC, so based on that values you can say device connected to cell network or not. – Rajen Raiyarela Jul 25 '14 at 05:58

2 Answers2

1

As described in this post, you can find as below

boolean hasNetwork = android.telephony.TelephonyManager.getNetworkType() != android.telephony.TelephonyManager.NETWORK_TYPE_UNKNOWN; 
// True if the phone is connected to some type of network i.e. has signal

Another way from same link, but not marked as solution, is to check for network operator field as below

public static Boolean isMobileAvailable(Context appcontext) {       
    TelephonyManager tel = (TelephonyManager) appcontext.getSystemService(Context.TELEPHONY_SERVICE);       
    return ((tel.getNetworkOperator() != null && tel.getNetworkOperator().equals("")) ? false : true);      
}
Community
  • 1
  • 1
Rajen Raiyarela
  • 5,526
  • 4
  • 21
  • 41
0

Try this:-

public boolean isNetworkAvailable() {
   Context context = getApplicationContext();
   ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
   if (connectivity == null) {
      boitealerte(this.getString(R.string.alert),"getSystemService rend null");
   } 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;
}

It will return true if the network is available.

Amit Anand
  • 1,225
  • 1
  • 16
  • 40