How can I determine the current internet connection type available to an Android device? e.g. have it return WiFi, 3G, none.
4 Answers
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.

- 10,917
- 4
- 51
- 70
-
Thanks for your concern. i an trying to use this code like in this way but its giving force to close if(connected ){ startActivity(intent7); } – user667340 Aug 23 '11 at 15:20
-
This means that in the code you are executing when connected there is an error. But not in the code that your are executing when not connected. – Shlublu Aug 23 '11 at 15:21
-
please guide how can i put in case statement that if its connected to Internet it should open the other intent otherwise it just display in toast. – user667340 Aug 23 '11 at 15:27
-
A `if()` is sufficient for that: `if(connected) { /* intent */ } else { /* toast */ }` – Shlublu Aug 23 '11 at 15:38
-
yeah i have tried in this way but its giving me force to close even i am connected to internet – user667340 Aug 23 '11 at 15:41
-
This comes from another problem not related to this internet connection topic. You should create another question for this. – Shlublu Aug 23 '11 at 15:42
-
@Shlublu let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/2779/discussion-between-user667340-and-shlublu) – user667340 Aug 23 '11 at 15:44
-
No sorry, I don't have the time. – Shlublu Aug 23 '11 at 15:44
-
Well, I am open to criticisms but why this "-1", today? – Shlublu Nov 14 '12 at 11:21
-
Your code looks very promising, but I can't get past the "ctx" cannot be resolved. – L. D. James Oct 01 '14 at 16:18
-
@L.D.James `ctx` is just the `Context`. So `this` should this code stand in an `Activity` for example. (Ah, maybe this is why this answer got downvoted, long ago) – Shlublu Oct 01 '14 at 16:24
-
Thanks for the input and attention to your example. That removed the error. I have it in a method of the MainActivity file `public boolean connectiontype()`. When called the Android crashes. I added "Internet" to the manifest. Is there another permission that should be added? I see you made a "continue this discussion in chat" link, which I tried to follow, but it isn't available. If you open up one at your convenience, I'll follow it to assist in debugging the answer... or just followup with what I find right here. – L. D. James Oct 01 '14 at 16:57
-
1I posted three comments/questions while studying the code and testing it. It works very well and is very precise and clear. Initially it crashed but the manifest permissions, `ACCESS_NETWORK_STATE`, `ACCESS_WIFI_STATE` resolved the crashes. – L. D. James Oct 01 '14 at 17:26
-
I was going to answer you previous comment but you did it for me :) Thanks ! – Shlublu Oct 02 '14 at 07:26
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

- 2,693
- 2
- 21
- 33
see this post, I answered the same question how to save request and resend in android app

- 1
- 1

- 1,336
- 10
- 11
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

- 16,782
- 9
- 50
- 50
-
No, that's why I said "You can also..." in case someone needs to know the type of mobile network being used. At least some of the other answers only say whether the connection is WiFi or mobile. – Intrications Aug 23 '12 at 19:32