How to determine Android internet connection?

2019-01-15 02:16发布

How can I determine the current internet connection type available to an Android device? e.g. have it return WiFi, 3G, none.

4条回答
Ridiculous、
2楼-- · 2019-01-15 02:24

You can also use the TelephonyManager to find out what the mobile network type is:

TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
int networkType = telephonyManager.getNetworkType();

Possible network types are listed at http://developer.android.com/reference/android/telephony/TelephonyManager.html#getNetworkType%28%29

查看更多
对你真心纯属浪费
3楼-- · 2019-01-15 02:26

You can use this to determine whether you are connected:

final ConnectivityManager connectManager = (ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE); // ctx stands for the Context
final NetworkInfo mobile = connectManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
final NetworkInfo wifi   = connectManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI  );

// Return true if connected, either in 3G or wi-fi
return ((mobile != null && mobile.getState() == NetworkInfo.State.CONNECTED) || 
        (wifi   != null && wifi.getState()   == NetworkInfo.State.CONNECTED)   );
}

This code requires the permissions ACCESS_NETWORK_STATE and ACCESS_WIFI_STATE.

EDIT: public NetworkInfo getNetworkInfo (int networkType) has been deprecated in API 21. For API levels higher than 20 you can do as follows:

final Network[] networks = connectManager.getAllNetworks();
boolean connected = false;

for (int ctr = 0; !connected && ctr < networks.length; ++ctr) {
    final NetworkInfo network = connectManager.getNetworkInfo(networks[ctr]);
    final int netType = network.getType();

    connected = (network.getState() == NetworkInfo.State.CONNECTED) &&
            (
                netType == ConnectivityManager.TYPE_MOBILE     ||
                netType == ConnectivityManager.TYPE_WIFI     /*||
                netType == ConnectivityManager.TYPE_WIMAX      ||
                netType == ConnectivityManager.TYPE_ETHERNET */  );
}

return connected;

I limited this code to Wi-Fi and 3G as per the OP's question but it's easy to extend it to newly added connection types such as Ethernet or Wi-Max.

查看更多
Viruses.
4楼-- · 2019-01-15 02:38

see this post, I answered the same question how to save request and resend in android app

查看更多
Bombasti
5楼-- · 2019-01-15 02:48

Don't know why your app crashes, maybe this helps. Maybe try this method: (needs the application context)

 public static boolean isInternetConnectionActive(Context context) {
  NetworkInfo networkInfo = ((ConnectivityManager) context
    .getSystemService(Context.CONNECTIVITY_SERVICE))
    .getActiveNetworkInfo();

  if(networkInfo == null || !networkInfo.isConnected()) {
   return false;
  }
  return true;
 }

then try to call

if(isInternetConnectionActive(getApplicationContext)) {
 /* start Intent */
}
else {
  /* show Toast */
}

hope this helps

查看更多
登录 后发表回答