Get Network type
I've been trying to retrive the current network type, but no success
when i say network type: i refer to know this info:
if the type is: NETWORK_TYPE_IDEN
or NETWORK_TYPE_UMTS
.. and so on..
i tried to use:
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
or
NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo
(ConnectivityManager.TYPE_MOBILE);
but no success..
i am doing this coz i wanna know if the current network is IDEN, or if the current network is connected through wifi..
android
add a comment |
I've been trying to retrive the current network type, but no success
when i say network type: i refer to know this info:
if the type is: NETWORK_TYPE_IDEN
or NETWORK_TYPE_UMTS
.. and so on..
i tried to use:
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
or
NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo
(ConnectivityManager.TYPE_MOBILE);
but no success..
i am doing this coz i wanna know if the current network is IDEN, or if the current network is connected through wifi..
android
Can you select answer from following? So people reach to this thread can know the accepted answer.
– Mr. Sajid Shaikh
Nov 25 '15 at 8:46
add a comment |
I've been trying to retrive the current network type, but no success
when i say network type: i refer to know this info:
if the type is: NETWORK_TYPE_IDEN
or NETWORK_TYPE_UMTS
.. and so on..
i tried to use:
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
or
NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo
(ConnectivityManager.TYPE_MOBILE);
but no success..
i am doing this coz i wanna know if the current network is IDEN, or if the current network is connected through wifi..
android
I've been trying to retrive the current network type, but no success
when i say network type: i refer to know this info:
if the type is: NETWORK_TYPE_IDEN
or NETWORK_TYPE_UMTS
.. and so on..
i tried to use:
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
or
NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo
(ConnectivityManager.TYPE_MOBILE);
but no success..
i am doing this coz i wanna know if the current network is IDEN, or if the current network is connected through wifi..
android
android
edited Feb 18 '11 at 9:38
phihag
196k47362407
196k47362407
asked May 27 '10 at 8:05
MoshikMoshik
2791319
2791319
Can you select answer from following? So people reach to this thread can know the accepted answer.
– Mr. Sajid Shaikh
Nov 25 '15 at 8:46
add a comment |
Can you select answer from following? So people reach to this thread can know the accepted answer.
– Mr. Sajid Shaikh
Nov 25 '15 at 8:46
Can you select answer from following? So people reach to this thread can know the accepted answer.
– Mr. Sajid Shaikh
Nov 25 '15 at 8:46
Can you select answer from following? So people reach to this thread can know the accepted answer.
– Mr. Sajid Shaikh
Nov 25 '15 at 8:46
add a comment |
11 Answers
11
active
oldest
votes
I hate magic numbers :
/**
* You need to add:
*
* <pre>
* <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
* </pre>
*
* in your AndroidManifest.xml.
*/
private String networkType() {
TelephonyManager teleMan = (TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE);
int networkType = teleMan.getNetworkType();
switch (networkType) {
case TelephonyManager.NETWORK_TYPE_1xRTT: return "1xRTT";
case TelephonyManager.NETWORK_TYPE_CDMA: return "CDMA";
case TelephonyManager.NETWORK_TYPE_EDGE: return "EDGE";
case TelephonyManager.NETWORK_TYPE_EHRPD: return "eHRPD";
case TelephonyManager.NETWORK_TYPE_EVDO_0: return "EVDO rev. 0";
case TelephonyManager.NETWORK_TYPE_EVDO_A: return "EVDO rev. A";
case TelephonyManager.NETWORK_TYPE_EVDO_B: return "EVDO rev. B";
case TelephonyManager.NETWORK_TYPE_GPRS: return "GPRS";
case TelephonyManager.NETWORK_TYPE_HSDPA: return "HSDPA";
case TelephonyManager.NETWORK_TYPE_HSPA: return "HSPA";
case TelephonyManager.NETWORK_TYPE_HSPAP: return "HSPA+";
case TelephonyManager.NETWORK_TYPE_HSUPA: return "HSUPA";
case TelephonyManager.NETWORK_TYPE_IDEN: return "iDen";
case TelephonyManager.NETWORK_TYPE_LTE: return "LTE";
case TelephonyManager.NETWORK_TYPE_UMTS: return "UMTS";
case TelephonyManager.NETWORK_TYPE_UNKNOWN: return "Unknown";
}
throw new RuntimeException("New type of network");
}
RuntimeException is a crash; I preferdefault: return "Unknown new type";
. As of today, the code contains the full list of NETWORK_TYPE_xxx constants.
– 18446744073709551615
Sep 6 '14 at 7:18
@18446744073709551615: Well yes - one should have a custom checked exception probably - what I meant to say with RE is that it should not happen - the library should take care of it withNETWORK_TYPE_UNKNOWN
(by the name of it).
– Mr_and_Mrs_D
Sep 6 '14 at 9:48
OFF-TOPIC that's an interesting philosophical question: what value should getNetworkType() return when a new network type (say, NETWORK_TYPE_XYZ) appears and the phone registers in such a network? If it returns NETWORK_TYPE_XYZ, the old applications may crash (exactly like thethrow new RuntimeException()
above does). If they check the application manifest and return NETWORK_TYPE_UNKNOWN for applications built against the older versions of SDK, the developers will probably go crazy trying to find out why the old application does not see the new kind of network.
– 18446744073709551615
Sep 6 '14 at 10:43
@18446744073709551615:throw NewNetTypeException("Network from Mars: " + networkType)
-NewNetTypeException
being checked (private String networkType() throws NewNetTypeException { /*...*/}
- let the caller decide - perfectly on topic good Java style programming ;) Just did not want to add also an exception - it's Q&A here not Java seminars - but that is what I'd do.
– Mr_and_Mrs_D
Sep 6 '14 at 11:22
Upvoted for "I hate magic numbers"!
– Luis Mendo
Apr 26 '18 at 16:46
add a comment |
This works for me to check the network type...
TelephonyManager teleMan =
(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
int networkType = teleMan.getNetworkType();
switch (networkType)
{
case 7:
textV1.setText("1xRTT");
break;
case 4:
textV1.setText("CDMA");
break;
case 2:
textV1.setText("EDGE");
break;
case 14:
textV1.setText("eHRPD");
break;
case 5:
textV1.setText("EVDO rev. 0");
break;
case 6:
textV1.setText("EVDO rev. A");
break;
case 12:
textV1.setText("EVDO rev. B");
break;
case 1:
textV1.setText("GPRS");
break;
case 8:
textV1.setText("HSDPA");
break;
case 10:
textV1.setText("HSPA");
break;
case 15:
textV1.setText("HSPA+");
break;
case 9:
textV1.setText("HSUPA");
break;
case 11:
textV1.setText("iDen");
break;
case 13:
textV1.setText("LTE");
break;
case 3:
textV1.setText("UMTS");
break;
case 0:
textV1.setText("Unknown");
break;
}
add a comment |
To get the network type (I think your talking about wifi or mobile) you can use this code snippet:
ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
//mobile
State mobile = conMan.getNetworkInfo(0).getState();
//wifi
State wifi = conMan.getNetworkInfo(1).getState();
and then use it like that:
if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING) {
//mobile
} else if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING) {
//wifi
}
To get the type of the mobile network I would try TelephonyManager#getNetworkType or NetworkInfo#getSubtypeName
Yes this is checking weather i`am on wifi or not.. and it does work! but is there also a way of checking my network type?(for example i am onto IDEN/GSM..) ?
– Moshik
May 27 '10 at 8:25
i editet my ansewr
– RoflcoptrException
May 27 '10 at 8:31
It didnt work after i tried it with Wifi... it's still eneter into the first condisiton.. seems like NetworkInfo.State.CONNECTED always return true.. any idea?
– Moshik
May 31 '10 at 6:45
add a comment |
I am using this function:
public String get_network()
{Activity act=(Activity)context;
String network_type="UNKNOWN";//maybe usb reverse tethering
NetworkInfo active_network=((ConnectivityManager)act.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (active_network!=null && active_network.isConnectedOrConnecting())
{if (active_network.getType()==ConnectivityManager.TYPE_WIFI)
{network_type="WIFI";
}
else if (active_network.getType()==ConnectivityManager.TYPE_MOBILE)
{network_type=((ConnectivityManager)act.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo().getSubtypeName();
}
}
return network_type;
}
add a comment |
Best answer
public static String getNetworkType(Context context) {
TelephonyManager teleMan = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
int networkType = teleMan.getNetworkType();
switch ( networkType ) {
case TelephonyManager.NETWORK_TYPE_1xRTT:
return "1xRTT";
case TelephonyManager.NETWORK_TYPE_CDMA:
return "CDMA";
case TelephonyManager.NETWORK_TYPE_EDGE:
return "EDGE";
case TelephonyManager.NETWORK_TYPE_EHRPD:
return "eHRPD";
case TelephonyManager.NETWORK_TYPE_EVDO_0:
return "EVDO rev. 0";
case TelephonyManager.NETWORK_TYPE_EVDO_A:
return "EVDO rev. A";
case TelephonyManager.NETWORK_TYPE_EVDO_B:
return "EVDO rev. B";
case TelephonyManager.NETWORK_TYPE_GPRS:
return "GPRS";
case TelephonyManager.NETWORK_TYPE_HSDPA:
return "HSDPA";
case TelephonyManager.NETWORK_TYPE_HSPA:
return "HSPA";
case TelephonyManager.NETWORK_TYPE_HSPAP:
return "HSPA+";
case TelephonyManager.NETWORK_TYPE_HSUPA:
return "HSUPA";
case TelephonyManager.NETWORK_TYPE_IDEN:
return "iDen";
case TelephonyManager.NETWORK_TYPE_LTE:
return "LTE";
case TelephonyManager.NETWORK_TYPE_UMTS:
return "UMTS";
case TelephonyManager.NETWORK_TYPE_UNKNOWN:
return "Unknown";
}
return "New type of network";
}
add a comment |
/** Network type is unknown */
public static final int NETWORK_TYPE_UNKNOWN = 0;
/** Current network is GPRS */
public static final int NETWORK_TYPE_GPRS = 1;
/** Current network is EDGE */
public static final int NETWORK_TYPE_EDGE = 2;
/** Current network is UMTS */
public static final int NETWORK_TYPE_UMTS = 3;
/** Current network is CDMA: Either IS95A or IS95B*/
public static final int NETWORK_TYPE_CDMA = 4;
/** Current network is EVDO revision 0*/
public static final int NETWORK_TYPE_EVDO_0 = 5;
/** Current network is EVDO revision A*/
public static final int NETWORK_TYPE_EVDO_A = 6;
/** Current network is 1xRTT*/
public static final int NETWORK_TYPE_1xRTT = 7;
/** Current network is HSDPA */
public static final int NETWORK_TYPE_HSDPA = 8;
/** Current network is HSUPA */
public static final int NETWORK_TYPE_HSUPA = 9;
/** Current network is HSPA */
public static final int NETWORK_TYPE_HSPA = 10;
/** Current network is iDen */
public static final int NETWORK_TYPE_IDEN = 11;
/** Current network is EVDO revision B*/
public static final int NETWORK_TYPE_EVDO_B = 12;
/** Current network is LTE */
public static final int NETWORK_TYPE_LTE = 13;
/** Current network is eHRPD */
public static final int NETWORK_TYPE_EHRPD = 14;
/** Current network is HSPA+ */
public static final int NETWORK_TYPE_HSPAP = 15;
/** Current network is GSM {@hide} */
public static final int NETWORK_TYPE_GSM = 16;
/** Current network is TD_SCDMA {@hide} */
public static final int NETWORK_TYPE_TD_SCDMA = 17;
/** Current network is IWLAN {@hide} */
public static final int NETWORK_TYPE_IWLAN = 18;
List of networkType Provided.
add a comment |
Also, add below required permission in your AndroidManifest.xml.
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Otherwise, you will face app crash while getting ConnectivityManager handle due to security exception.
add a comment |
In my experience... it's best to use Android training guides for these types of efforts. It's easy to get null pointer exceptions when you use these classes, and it's especially bad when you try to detect these connections when the app first opens and then the app crashes.
You can use the ConnectivityManager to check that you're actually connected to the Internet, and if so, what type of connection is in place:
http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html
You can use the ConnectivityManager to determine the active wireless radio:
http://developer.android.com/training/efficient-downloads/connectivity_patterns.html
add a comment |
Better use would be:
NetworkInfo activeNetworkInfo = ((ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (activeNetworkInfo == null) return false;
switch (activeNetworkInfo.getType()) {
case ConnectivityManager.TYPE_WIFI:
return true;
}
// public static final int TYPE_BLUETOOTH = 7;
// public static final int TYPE_DUMMY = 8;
// public static final int TYPE_ETHERNET = 9;
// public static final int TYPE_MOBILE = 0;
// public static final int TYPE_MOBILE_DUN = 4;
// /** @deprecated */
// @Deprecated
// public static final int TYPE_MOBILE_HIPRI = 5;
// /** @deprecated */
// @Deprecated
// public static final int TYPE_MOBILE_MMS = 2;
// /** @deprecated */
// @Deprecated
// public static final int TYPE_MOBILE_SUPL = 3;
// public static final int TYPE_VPN = 17;
// public static final int TYPE_WIFI = 1;
// public static final int TYPE_WIMAX = 6;
Which other types you want to define and handle is up to you...
For those wanting a string identifier, better use:
activeNetworkInfo.getTypeName()
activeNetworkInfo.getSubtypeName()
add a comment |
//**Return type of connection to network
public static int getNetworkType(Context context) {
if (!isNetworkConnected(context))
return -1;
ConnectivityManager conMan = (ConnectivityManager) context.
getSystemService(Context.CONNECTIVITY_SERVICE);
//mobile
State mobile = conMan.getNetworkInfo(0).getState();
//wifi
State wifi = conMan.getNetworkInfo(1).getState();
int result = 0;
if (mobile == State.CONNECTED || mobile == State.CONNECTING) {
result |= NETWORK_MOBILE;
}
if (wifi == State.CONNECTED || wifi == State.CONNECTING) {
result |= NETWORK_WIFI;
}
return result;
}
add a comment |
If you want to know which type of network it is, you can use this :
private String getNetworkClass() {
// network type
TelephonyManager mTelephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
int networkType = mTelephonyManager != null ? mTelephonyManager.getNetworkType() : 0;
switch (networkType) {
case TelephonyManager.NETWORK_TYPE_UNKNOWN:
return "Unknown network";
case TelephonyManager.NETWORK_TYPE_GSM:
return " GSM";
case TelephonyManager.NETWORK_TYPE_CDMA:
case TelephonyManager.NETWORK_TYPE_1xRTT:
case TelephonyManager.NETWORK_TYPE_IDEN:
return " 2G";
case TelephonyManager.NETWORK_TYPE_GPRS:
return " GPRS (2.5G)";
case TelephonyManager.NETWORK_TYPE_EDGE:
return " EDGE (2.75G)";
case TelephonyManager.NETWORK_TYPE_UMTS:
case TelephonyManager.NETWORK_TYPE_EVDO_0:
case TelephonyManager.NETWORK_TYPE_EVDO_A:
case TelephonyManager.NETWORK_TYPE_EVDO_B:
return " 3G";
case TelephonyManager.NETWORK_TYPE_HSPA:
case TelephonyManager.NETWORK_TYPE_HSDPA:
case TelephonyManager.NETWORK_TYPE_HSUPA:
return " H (3G+)";
case TelephonyManager.NETWORK_TYPE_EHRPD:
case TelephonyManager.NETWORK_TYPE_HSPAP:
case TelephonyManager.NETWORK_TYPE_TD_SCDMA:
return " H+ (3G++)";
case TelephonyManager.NETWORK_TYPE_LTE:
case TelephonyManager.NETWORK_TYPE_IWLAN:
return " 4G";
default:
return " 4G+";
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2919414%2fget-network-type%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
11 Answers
11
active
oldest
votes
11 Answers
11
active
oldest
votes
active
oldest
votes
active
oldest
votes
I hate magic numbers :
/**
* You need to add:
*
* <pre>
* <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
* </pre>
*
* in your AndroidManifest.xml.
*/
private String networkType() {
TelephonyManager teleMan = (TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE);
int networkType = teleMan.getNetworkType();
switch (networkType) {
case TelephonyManager.NETWORK_TYPE_1xRTT: return "1xRTT";
case TelephonyManager.NETWORK_TYPE_CDMA: return "CDMA";
case TelephonyManager.NETWORK_TYPE_EDGE: return "EDGE";
case TelephonyManager.NETWORK_TYPE_EHRPD: return "eHRPD";
case TelephonyManager.NETWORK_TYPE_EVDO_0: return "EVDO rev. 0";
case TelephonyManager.NETWORK_TYPE_EVDO_A: return "EVDO rev. A";
case TelephonyManager.NETWORK_TYPE_EVDO_B: return "EVDO rev. B";
case TelephonyManager.NETWORK_TYPE_GPRS: return "GPRS";
case TelephonyManager.NETWORK_TYPE_HSDPA: return "HSDPA";
case TelephonyManager.NETWORK_TYPE_HSPA: return "HSPA";
case TelephonyManager.NETWORK_TYPE_HSPAP: return "HSPA+";
case TelephonyManager.NETWORK_TYPE_HSUPA: return "HSUPA";
case TelephonyManager.NETWORK_TYPE_IDEN: return "iDen";
case TelephonyManager.NETWORK_TYPE_LTE: return "LTE";
case TelephonyManager.NETWORK_TYPE_UMTS: return "UMTS";
case TelephonyManager.NETWORK_TYPE_UNKNOWN: return "Unknown";
}
throw new RuntimeException("New type of network");
}
RuntimeException is a crash; I preferdefault: return "Unknown new type";
. As of today, the code contains the full list of NETWORK_TYPE_xxx constants.
– 18446744073709551615
Sep 6 '14 at 7:18
@18446744073709551615: Well yes - one should have a custom checked exception probably - what I meant to say with RE is that it should not happen - the library should take care of it withNETWORK_TYPE_UNKNOWN
(by the name of it).
– Mr_and_Mrs_D
Sep 6 '14 at 9:48
OFF-TOPIC that's an interesting philosophical question: what value should getNetworkType() return when a new network type (say, NETWORK_TYPE_XYZ) appears and the phone registers in such a network? If it returns NETWORK_TYPE_XYZ, the old applications may crash (exactly like thethrow new RuntimeException()
above does). If they check the application manifest and return NETWORK_TYPE_UNKNOWN for applications built against the older versions of SDK, the developers will probably go crazy trying to find out why the old application does not see the new kind of network.
– 18446744073709551615
Sep 6 '14 at 10:43
@18446744073709551615:throw NewNetTypeException("Network from Mars: " + networkType)
-NewNetTypeException
being checked (private String networkType() throws NewNetTypeException { /*...*/}
- let the caller decide - perfectly on topic good Java style programming ;) Just did not want to add also an exception - it's Q&A here not Java seminars - but that is what I'd do.
– Mr_and_Mrs_D
Sep 6 '14 at 11:22
Upvoted for "I hate magic numbers"!
– Luis Mendo
Apr 26 '18 at 16:46
add a comment |
I hate magic numbers :
/**
* You need to add:
*
* <pre>
* <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
* </pre>
*
* in your AndroidManifest.xml.
*/
private String networkType() {
TelephonyManager teleMan = (TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE);
int networkType = teleMan.getNetworkType();
switch (networkType) {
case TelephonyManager.NETWORK_TYPE_1xRTT: return "1xRTT";
case TelephonyManager.NETWORK_TYPE_CDMA: return "CDMA";
case TelephonyManager.NETWORK_TYPE_EDGE: return "EDGE";
case TelephonyManager.NETWORK_TYPE_EHRPD: return "eHRPD";
case TelephonyManager.NETWORK_TYPE_EVDO_0: return "EVDO rev. 0";
case TelephonyManager.NETWORK_TYPE_EVDO_A: return "EVDO rev. A";
case TelephonyManager.NETWORK_TYPE_EVDO_B: return "EVDO rev. B";
case TelephonyManager.NETWORK_TYPE_GPRS: return "GPRS";
case TelephonyManager.NETWORK_TYPE_HSDPA: return "HSDPA";
case TelephonyManager.NETWORK_TYPE_HSPA: return "HSPA";
case TelephonyManager.NETWORK_TYPE_HSPAP: return "HSPA+";
case TelephonyManager.NETWORK_TYPE_HSUPA: return "HSUPA";
case TelephonyManager.NETWORK_TYPE_IDEN: return "iDen";
case TelephonyManager.NETWORK_TYPE_LTE: return "LTE";
case TelephonyManager.NETWORK_TYPE_UMTS: return "UMTS";
case TelephonyManager.NETWORK_TYPE_UNKNOWN: return "Unknown";
}
throw new RuntimeException("New type of network");
}
RuntimeException is a crash; I preferdefault: return "Unknown new type";
. As of today, the code contains the full list of NETWORK_TYPE_xxx constants.
– 18446744073709551615
Sep 6 '14 at 7:18
@18446744073709551615: Well yes - one should have a custom checked exception probably - what I meant to say with RE is that it should not happen - the library should take care of it withNETWORK_TYPE_UNKNOWN
(by the name of it).
– Mr_and_Mrs_D
Sep 6 '14 at 9:48
OFF-TOPIC that's an interesting philosophical question: what value should getNetworkType() return when a new network type (say, NETWORK_TYPE_XYZ) appears and the phone registers in such a network? If it returns NETWORK_TYPE_XYZ, the old applications may crash (exactly like thethrow new RuntimeException()
above does). If they check the application manifest and return NETWORK_TYPE_UNKNOWN for applications built against the older versions of SDK, the developers will probably go crazy trying to find out why the old application does not see the new kind of network.
– 18446744073709551615
Sep 6 '14 at 10:43
@18446744073709551615:throw NewNetTypeException("Network from Mars: " + networkType)
-NewNetTypeException
being checked (private String networkType() throws NewNetTypeException { /*...*/}
- let the caller decide - perfectly on topic good Java style programming ;) Just did not want to add also an exception - it's Q&A here not Java seminars - but that is what I'd do.
– Mr_and_Mrs_D
Sep 6 '14 at 11:22
Upvoted for "I hate magic numbers"!
– Luis Mendo
Apr 26 '18 at 16:46
add a comment |
I hate magic numbers :
/**
* You need to add:
*
* <pre>
* <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
* </pre>
*
* in your AndroidManifest.xml.
*/
private String networkType() {
TelephonyManager teleMan = (TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE);
int networkType = teleMan.getNetworkType();
switch (networkType) {
case TelephonyManager.NETWORK_TYPE_1xRTT: return "1xRTT";
case TelephonyManager.NETWORK_TYPE_CDMA: return "CDMA";
case TelephonyManager.NETWORK_TYPE_EDGE: return "EDGE";
case TelephonyManager.NETWORK_TYPE_EHRPD: return "eHRPD";
case TelephonyManager.NETWORK_TYPE_EVDO_0: return "EVDO rev. 0";
case TelephonyManager.NETWORK_TYPE_EVDO_A: return "EVDO rev. A";
case TelephonyManager.NETWORK_TYPE_EVDO_B: return "EVDO rev. B";
case TelephonyManager.NETWORK_TYPE_GPRS: return "GPRS";
case TelephonyManager.NETWORK_TYPE_HSDPA: return "HSDPA";
case TelephonyManager.NETWORK_TYPE_HSPA: return "HSPA";
case TelephonyManager.NETWORK_TYPE_HSPAP: return "HSPA+";
case TelephonyManager.NETWORK_TYPE_HSUPA: return "HSUPA";
case TelephonyManager.NETWORK_TYPE_IDEN: return "iDen";
case TelephonyManager.NETWORK_TYPE_LTE: return "LTE";
case TelephonyManager.NETWORK_TYPE_UMTS: return "UMTS";
case TelephonyManager.NETWORK_TYPE_UNKNOWN: return "Unknown";
}
throw new RuntimeException("New type of network");
}
I hate magic numbers :
/**
* You need to add:
*
* <pre>
* <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
* </pre>
*
* in your AndroidManifest.xml.
*/
private String networkType() {
TelephonyManager teleMan = (TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE);
int networkType = teleMan.getNetworkType();
switch (networkType) {
case TelephonyManager.NETWORK_TYPE_1xRTT: return "1xRTT";
case TelephonyManager.NETWORK_TYPE_CDMA: return "CDMA";
case TelephonyManager.NETWORK_TYPE_EDGE: return "EDGE";
case TelephonyManager.NETWORK_TYPE_EHRPD: return "eHRPD";
case TelephonyManager.NETWORK_TYPE_EVDO_0: return "EVDO rev. 0";
case TelephonyManager.NETWORK_TYPE_EVDO_A: return "EVDO rev. A";
case TelephonyManager.NETWORK_TYPE_EVDO_B: return "EVDO rev. B";
case TelephonyManager.NETWORK_TYPE_GPRS: return "GPRS";
case TelephonyManager.NETWORK_TYPE_HSDPA: return "HSDPA";
case TelephonyManager.NETWORK_TYPE_HSPA: return "HSPA";
case TelephonyManager.NETWORK_TYPE_HSPAP: return "HSPA+";
case TelephonyManager.NETWORK_TYPE_HSUPA: return "HSUPA";
case TelephonyManager.NETWORK_TYPE_IDEN: return "iDen";
case TelephonyManager.NETWORK_TYPE_LTE: return "LTE";
case TelephonyManager.NETWORK_TYPE_UMTS: return "UMTS";
case TelephonyManager.NETWORK_TYPE_UNKNOWN: return "Unknown";
}
throw new RuntimeException("New type of network");
}
edited Jun 11 '14 at 11:39
answered Nov 13 '13 at 22:05
Mr_and_Mrs_DMr_and_Mrs_D
15.5k23121257
15.5k23121257
RuntimeException is a crash; I preferdefault: return "Unknown new type";
. As of today, the code contains the full list of NETWORK_TYPE_xxx constants.
– 18446744073709551615
Sep 6 '14 at 7:18
@18446744073709551615: Well yes - one should have a custom checked exception probably - what I meant to say with RE is that it should not happen - the library should take care of it withNETWORK_TYPE_UNKNOWN
(by the name of it).
– Mr_and_Mrs_D
Sep 6 '14 at 9:48
OFF-TOPIC that's an interesting philosophical question: what value should getNetworkType() return when a new network type (say, NETWORK_TYPE_XYZ) appears and the phone registers in such a network? If it returns NETWORK_TYPE_XYZ, the old applications may crash (exactly like thethrow new RuntimeException()
above does). If they check the application manifest and return NETWORK_TYPE_UNKNOWN for applications built against the older versions of SDK, the developers will probably go crazy trying to find out why the old application does not see the new kind of network.
– 18446744073709551615
Sep 6 '14 at 10:43
@18446744073709551615:throw NewNetTypeException("Network from Mars: " + networkType)
-NewNetTypeException
being checked (private String networkType() throws NewNetTypeException { /*...*/}
- let the caller decide - perfectly on topic good Java style programming ;) Just did not want to add also an exception - it's Q&A here not Java seminars - but that is what I'd do.
– Mr_and_Mrs_D
Sep 6 '14 at 11:22
Upvoted for "I hate magic numbers"!
– Luis Mendo
Apr 26 '18 at 16:46
add a comment |
RuntimeException is a crash; I preferdefault: return "Unknown new type";
. As of today, the code contains the full list of NETWORK_TYPE_xxx constants.
– 18446744073709551615
Sep 6 '14 at 7:18
@18446744073709551615: Well yes - one should have a custom checked exception probably - what I meant to say with RE is that it should not happen - the library should take care of it withNETWORK_TYPE_UNKNOWN
(by the name of it).
– Mr_and_Mrs_D
Sep 6 '14 at 9:48
OFF-TOPIC that's an interesting philosophical question: what value should getNetworkType() return when a new network type (say, NETWORK_TYPE_XYZ) appears and the phone registers in such a network? If it returns NETWORK_TYPE_XYZ, the old applications may crash (exactly like thethrow new RuntimeException()
above does). If they check the application manifest and return NETWORK_TYPE_UNKNOWN for applications built against the older versions of SDK, the developers will probably go crazy trying to find out why the old application does not see the new kind of network.
– 18446744073709551615
Sep 6 '14 at 10:43
@18446744073709551615:throw NewNetTypeException("Network from Mars: " + networkType)
-NewNetTypeException
being checked (private String networkType() throws NewNetTypeException { /*...*/}
- let the caller decide - perfectly on topic good Java style programming ;) Just did not want to add also an exception - it's Q&A here not Java seminars - but that is what I'd do.
– Mr_and_Mrs_D
Sep 6 '14 at 11:22
Upvoted for "I hate magic numbers"!
– Luis Mendo
Apr 26 '18 at 16:46
RuntimeException is a crash; I prefer
default: return "Unknown new type";
. As of today, the code contains the full list of NETWORK_TYPE_xxx constants.– 18446744073709551615
Sep 6 '14 at 7:18
RuntimeException is a crash; I prefer
default: return "Unknown new type";
. As of today, the code contains the full list of NETWORK_TYPE_xxx constants.– 18446744073709551615
Sep 6 '14 at 7:18
@18446744073709551615: Well yes - one should have a custom checked exception probably - what I meant to say with RE is that it should not happen - the library should take care of it with
NETWORK_TYPE_UNKNOWN
(by the name of it).– Mr_and_Mrs_D
Sep 6 '14 at 9:48
@18446744073709551615: Well yes - one should have a custom checked exception probably - what I meant to say with RE is that it should not happen - the library should take care of it with
NETWORK_TYPE_UNKNOWN
(by the name of it).– Mr_and_Mrs_D
Sep 6 '14 at 9:48
OFF-TOPIC that's an interesting philosophical question: what value should getNetworkType() return when a new network type (say, NETWORK_TYPE_XYZ) appears and the phone registers in such a network? If it returns NETWORK_TYPE_XYZ, the old applications may crash (exactly like the
throw new RuntimeException()
above does). If they check the application manifest and return NETWORK_TYPE_UNKNOWN for applications built against the older versions of SDK, the developers will probably go crazy trying to find out why the old application does not see the new kind of network.– 18446744073709551615
Sep 6 '14 at 10:43
OFF-TOPIC that's an interesting philosophical question: what value should getNetworkType() return when a new network type (say, NETWORK_TYPE_XYZ) appears and the phone registers in such a network? If it returns NETWORK_TYPE_XYZ, the old applications may crash (exactly like the
throw new RuntimeException()
above does). If they check the application manifest and return NETWORK_TYPE_UNKNOWN for applications built against the older versions of SDK, the developers will probably go crazy trying to find out why the old application does not see the new kind of network.– 18446744073709551615
Sep 6 '14 at 10:43
@18446744073709551615:
throw NewNetTypeException("Network from Mars: " + networkType)
- NewNetTypeException
being checked (private String networkType() throws NewNetTypeException { /*...*/}
- let the caller decide - perfectly on topic good Java style programming ;) Just did not want to add also an exception - it's Q&A here not Java seminars - but that is what I'd do.– Mr_and_Mrs_D
Sep 6 '14 at 11:22
@18446744073709551615:
throw NewNetTypeException("Network from Mars: " + networkType)
- NewNetTypeException
being checked (private String networkType() throws NewNetTypeException { /*...*/}
- let the caller decide - perfectly on topic good Java style programming ;) Just did not want to add also an exception - it's Q&A here not Java seminars - but that is what I'd do.– Mr_and_Mrs_D
Sep 6 '14 at 11:22
Upvoted for "I hate magic numbers"!
– Luis Mendo
Apr 26 '18 at 16:46
Upvoted for "I hate magic numbers"!
– Luis Mendo
Apr 26 '18 at 16:46
add a comment |
This works for me to check the network type...
TelephonyManager teleMan =
(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
int networkType = teleMan.getNetworkType();
switch (networkType)
{
case 7:
textV1.setText("1xRTT");
break;
case 4:
textV1.setText("CDMA");
break;
case 2:
textV1.setText("EDGE");
break;
case 14:
textV1.setText("eHRPD");
break;
case 5:
textV1.setText("EVDO rev. 0");
break;
case 6:
textV1.setText("EVDO rev. A");
break;
case 12:
textV1.setText("EVDO rev. B");
break;
case 1:
textV1.setText("GPRS");
break;
case 8:
textV1.setText("HSDPA");
break;
case 10:
textV1.setText("HSPA");
break;
case 15:
textV1.setText("HSPA+");
break;
case 9:
textV1.setText("HSUPA");
break;
case 11:
textV1.setText("iDen");
break;
case 13:
textV1.setText("LTE");
break;
case 3:
textV1.setText("UMTS");
break;
case 0:
textV1.setText("Unknown");
break;
}
add a comment |
This works for me to check the network type...
TelephonyManager teleMan =
(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
int networkType = teleMan.getNetworkType();
switch (networkType)
{
case 7:
textV1.setText("1xRTT");
break;
case 4:
textV1.setText("CDMA");
break;
case 2:
textV1.setText("EDGE");
break;
case 14:
textV1.setText("eHRPD");
break;
case 5:
textV1.setText("EVDO rev. 0");
break;
case 6:
textV1.setText("EVDO rev. A");
break;
case 12:
textV1.setText("EVDO rev. B");
break;
case 1:
textV1.setText("GPRS");
break;
case 8:
textV1.setText("HSDPA");
break;
case 10:
textV1.setText("HSPA");
break;
case 15:
textV1.setText("HSPA+");
break;
case 9:
textV1.setText("HSUPA");
break;
case 11:
textV1.setText("iDen");
break;
case 13:
textV1.setText("LTE");
break;
case 3:
textV1.setText("UMTS");
break;
case 0:
textV1.setText("Unknown");
break;
}
add a comment |
This works for me to check the network type...
TelephonyManager teleMan =
(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
int networkType = teleMan.getNetworkType();
switch (networkType)
{
case 7:
textV1.setText("1xRTT");
break;
case 4:
textV1.setText("CDMA");
break;
case 2:
textV1.setText("EDGE");
break;
case 14:
textV1.setText("eHRPD");
break;
case 5:
textV1.setText("EVDO rev. 0");
break;
case 6:
textV1.setText("EVDO rev. A");
break;
case 12:
textV1.setText("EVDO rev. B");
break;
case 1:
textV1.setText("GPRS");
break;
case 8:
textV1.setText("HSDPA");
break;
case 10:
textV1.setText("HSPA");
break;
case 15:
textV1.setText("HSPA+");
break;
case 9:
textV1.setText("HSUPA");
break;
case 11:
textV1.setText("iDen");
break;
case 13:
textV1.setText("LTE");
break;
case 3:
textV1.setText("UMTS");
break;
case 0:
textV1.setText("Unknown");
break;
}
This works for me to check the network type...
TelephonyManager teleMan =
(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
int networkType = teleMan.getNetworkType();
switch (networkType)
{
case 7:
textV1.setText("1xRTT");
break;
case 4:
textV1.setText("CDMA");
break;
case 2:
textV1.setText("EDGE");
break;
case 14:
textV1.setText("eHRPD");
break;
case 5:
textV1.setText("EVDO rev. 0");
break;
case 6:
textV1.setText("EVDO rev. A");
break;
case 12:
textV1.setText("EVDO rev. B");
break;
case 1:
textV1.setText("GPRS");
break;
case 8:
textV1.setText("HSDPA");
break;
case 10:
textV1.setText("HSPA");
break;
case 15:
textV1.setText("HSPA+");
break;
case 9:
textV1.setText("HSUPA");
break;
case 11:
textV1.setText("iDen");
break;
case 13:
textV1.setText("LTE");
break;
case 3:
textV1.setText("UMTS");
break;
case 0:
textV1.setText("Unknown");
break;
}
edited Jul 22 '11 at 1:05
answered Jul 22 '11 at 0:21
outkkastoutkkast
2,8041136
2,8041136
add a comment |
add a comment |
To get the network type (I think your talking about wifi or mobile) you can use this code snippet:
ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
//mobile
State mobile = conMan.getNetworkInfo(0).getState();
//wifi
State wifi = conMan.getNetworkInfo(1).getState();
and then use it like that:
if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING) {
//mobile
} else if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING) {
//wifi
}
To get the type of the mobile network I would try TelephonyManager#getNetworkType or NetworkInfo#getSubtypeName
Yes this is checking weather i`am on wifi or not.. and it does work! but is there also a way of checking my network type?(for example i am onto IDEN/GSM..) ?
– Moshik
May 27 '10 at 8:25
i editet my ansewr
– RoflcoptrException
May 27 '10 at 8:31
It didnt work after i tried it with Wifi... it's still eneter into the first condisiton.. seems like NetworkInfo.State.CONNECTED always return true.. any idea?
– Moshik
May 31 '10 at 6:45
add a comment |
To get the network type (I think your talking about wifi or mobile) you can use this code snippet:
ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
//mobile
State mobile = conMan.getNetworkInfo(0).getState();
//wifi
State wifi = conMan.getNetworkInfo(1).getState();
and then use it like that:
if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING) {
//mobile
} else if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING) {
//wifi
}
To get the type of the mobile network I would try TelephonyManager#getNetworkType or NetworkInfo#getSubtypeName
Yes this is checking weather i`am on wifi or not.. and it does work! but is there also a way of checking my network type?(for example i am onto IDEN/GSM..) ?
– Moshik
May 27 '10 at 8:25
i editet my ansewr
– RoflcoptrException
May 27 '10 at 8:31
It didnt work after i tried it with Wifi... it's still eneter into the first condisiton.. seems like NetworkInfo.State.CONNECTED always return true.. any idea?
– Moshik
May 31 '10 at 6:45
add a comment |
To get the network type (I think your talking about wifi or mobile) you can use this code snippet:
ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
//mobile
State mobile = conMan.getNetworkInfo(0).getState();
//wifi
State wifi = conMan.getNetworkInfo(1).getState();
and then use it like that:
if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING) {
//mobile
} else if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING) {
//wifi
}
To get the type of the mobile network I would try TelephonyManager#getNetworkType or NetworkInfo#getSubtypeName
To get the network type (I think your talking about wifi or mobile) you can use this code snippet:
ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
//mobile
State mobile = conMan.getNetworkInfo(0).getState();
//wifi
State wifi = conMan.getNetworkInfo(1).getState();
and then use it like that:
if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING) {
//mobile
} else if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING) {
//wifi
}
To get the type of the mobile network I would try TelephonyManager#getNetworkType or NetworkInfo#getSubtypeName
edited May 27 '10 at 8:31
answered May 27 '10 at 8:07
RoflcoptrExceptionRoflcoptrException
39.7k31139196
39.7k31139196
Yes this is checking weather i`am on wifi or not.. and it does work! but is there also a way of checking my network type?(for example i am onto IDEN/GSM..) ?
– Moshik
May 27 '10 at 8:25
i editet my ansewr
– RoflcoptrException
May 27 '10 at 8:31
It didnt work after i tried it with Wifi... it's still eneter into the first condisiton.. seems like NetworkInfo.State.CONNECTED always return true.. any idea?
– Moshik
May 31 '10 at 6:45
add a comment |
Yes this is checking weather i`am on wifi or not.. and it does work! but is there also a way of checking my network type?(for example i am onto IDEN/GSM..) ?
– Moshik
May 27 '10 at 8:25
i editet my ansewr
– RoflcoptrException
May 27 '10 at 8:31
It didnt work after i tried it with Wifi... it's still eneter into the first condisiton.. seems like NetworkInfo.State.CONNECTED always return true.. any idea?
– Moshik
May 31 '10 at 6:45
Yes this is checking weather i`am on wifi or not.. and it does work! but is there also a way of checking my network type?(for example i am onto IDEN/GSM..) ?
– Moshik
May 27 '10 at 8:25
Yes this is checking weather i`am on wifi or not.. and it does work! but is there also a way of checking my network type?(for example i am onto IDEN/GSM..) ?
– Moshik
May 27 '10 at 8:25
i editet my ansewr
– RoflcoptrException
May 27 '10 at 8:31
i editet my ansewr
– RoflcoptrException
May 27 '10 at 8:31
It didnt work after i tried it with Wifi... it's still eneter into the first condisiton.. seems like NetworkInfo.State.CONNECTED always return true.. any idea?
– Moshik
May 31 '10 at 6:45
It didnt work after i tried it with Wifi... it's still eneter into the first condisiton.. seems like NetworkInfo.State.CONNECTED always return true.. any idea?
– Moshik
May 31 '10 at 6:45
add a comment |
I am using this function:
public String get_network()
{Activity act=(Activity)context;
String network_type="UNKNOWN";//maybe usb reverse tethering
NetworkInfo active_network=((ConnectivityManager)act.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (active_network!=null && active_network.isConnectedOrConnecting())
{if (active_network.getType()==ConnectivityManager.TYPE_WIFI)
{network_type="WIFI";
}
else if (active_network.getType()==ConnectivityManager.TYPE_MOBILE)
{network_type=((ConnectivityManager)act.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo().getSubtypeName();
}
}
return network_type;
}
add a comment |
I am using this function:
public String get_network()
{Activity act=(Activity)context;
String network_type="UNKNOWN";//maybe usb reverse tethering
NetworkInfo active_network=((ConnectivityManager)act.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (active_network!=null && active_network.isConnectedOrConnecting())
{if (active_network.getType()==ConnectivityManager.TYPE_WIFI)
{network_type="WIFI";
}
else if (active_network.getType()==ConnectivityManager.TYPE_MOBILE)
{network_type=((ConnectivityManager)act.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo().getSubtypeName();
}
}
return network_type;
}
add a comment |
I am using this function:
public String get_network()
{Activity act=(Activity)context;
String network_type="UNKNOWN";//maybe usb reverse tethering
NetworkInfo active_network=((ConnectivityManager)act.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (active_network!=null && active_network.isConnectedOrConnecting())
{if (active_network.getType()==ConnectivityManager.TYPE_WIFI)
{network_type="WIFI";
}
else if (active_network.getType()==ConnectivityManager.TYPE_MOBILE)
{network_type=((ConnectivityManager)act.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo().getSubtypeName();
}
}
return network_type;
}
I am using this function:
public String get_network()
{Activity act=(Activity)context;
String network_type="UNKNOWN";//maybe usb reverse tethering
NetworkInfo active_network=((ConnectivityManager)act.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (active_network!=null && active_network.isConnectedOrConnecting())
{if (active_network.getType()==ConnectivityManager.TYPE_WIFI)
{network_type="WIFI";
}
else if (active_network.getType()==ConnectivityManager.TYPE_MOBILE)
{network_type=((ConnectivityManager)act.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo().getSubtypeName();
}
}
return network_type;
}
answered Aug 5 '11 at 10:02
diyismdiyism
8,48654037
8,48654037
add a comment |
add a comment |
Best answer
public static String getNetworkType(Context context) {
TelephonyManager teleMan = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
int networkType = teleMan.getNetworkType();
switch ( networkType ) {
case TelephonyManager.NETWORK_TYPE_1xRTT:
return "1xRTT";
case TelephonyManager.NETWORK_TYPE_CDMA:
return "CDMA";
case TelephonyManager.NETWORK_TYPE_EDGE:
return "EDGE";
case TelephonyManager.NETWORK_TYPE_EHRPD:
return "eHRPD";
case TelephonyManager.NETWORK_TYPE_EVDO_0:
return "EVDO rev. 0";
case TelephonyManager.NETWORK_TYPE_EVDO_A:
return "EVDO rev. A";
case TelephonyManager.NETWORK_TYPE_EVDO_B:
return "EVDO rev. B";
case TelephonyManager.NETWORK_TYPE_GPRS:
return "GPRS";
case TelephonyManager.NETWORK_TYPE_HSDPA:
return "HSDPA";
case TelephonyManager.NETWORK_TYPE_HSPA:
return "HSPA";
case TelephonyManager.NETWORK_TYPE_HSPAP:
return "HSPA+";
case TelephonyManager.NETWORK_TYPE_HSUPA:
return "HSUPA";
case TelephonyManager.NETWORK_TYPE_IDEN:
return "iDen";
case TelephonyManager.NETWORK_TYPE_LTE:
return "LTE";
case TelephonyManager.NETWORK_TYPE_UMTS:
return "UMTS";
case TelephonyManager.NETWORK_TYPE_UNKNOWN:
return "Unknown";
}
return "New type of network";
}
add a comment |
Best answer
public static String getNetworkType(Context context) {
TelephonyManager teleMan = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
int networkType = teleMan.getNetworkType();
switch ( networkType ) {
case TelephonyManager.NETWORK_TYPE_1xRTT:
return "1xRTT";
case TelephonyManager.NETWORK_TYPE_CDMA:
return "CDMA";
case TelephonyManager.NETWORK_TYPE_EDGE:
return "EDGE";
case TelephonyManager.NETWORK_TYPE_EHRPD:
return "eHRPD";
case TelephonyManager.NETWORK_TYPE_EVDO_0:
return "EVDO rev. 0";
case TelephonyManager.NETWORK_TYPE_EVDO_A:
return "EVDO rev. A";
case TelephonyManager.NETWORK_TYPE_EVDO_B:
return "EVDO rev. B";
case TelephonyManager.NETWORK_TYPE_GPRS:
return "GPRS";
case TelephonyManager.NETWORK_TYPE_HSDPA:
return "HSDPA";
case TelephonyManager.NETWORK_TYPE_HSPA:
return "HSPA";
case TelephonyManager.NETWORK_TYPE_HSPAP:
return "HSPA+";
case TelephonyManager.NETWORK_TYPE_HSUPA:
return "HSUPA";
case TelephonyManager.NETWORK_TYPE_IDEN:
return "iDen";
case TelephonyManager.NETWORK_TYPE_LTE:
return "LTE";
case TelephonyManager.NETWORK_TYPE_UMTS:
return "UMTS";
case TelephonyManager.NETWORK_TYPE_UNKNOWN:
return "Unknown";
}
return "New type of network";
}
add a comment |
Best answer
public static String getNetworkType(Context context) {
TelephonyManager teleMan = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
int networkType = teleMan.getNetworkType();
switch ( networkType ) {
case TelephonyManager.NETWORK_TYPE_1xRTT:
return "1xRTT";
case TelephonyManager.NETWORK_TYPE_CDMA:
return "CDMA";
case TelephonyManager.NETWORK_TYPE_EDGE:
return "EDGE";
case TelephonyManager.NETWORK_TYPE_EHRPD:
return "eHRPD";
case TelephonyManager.NETWORK_TYPE_EVDO_0:
return "EVDO rev. 0";
case TelephonyManager.NETWORK_TYPE_EVDO_A:
return "EVDO rev. A";
case TelephonyManager.NETWORK_TYPE_EVDO_B:
return "EVDO rev. B";
case TelephonyManager.NETWORK_TYPE_GPRS:
return "GPRS";
case TelephonyManager.NETWORK_TYPE_HSDPA:
return "HSDPA";
case TelephonyManager.NETWORK_TYPE_HSPA:
return "HSPA";
case TelephonyManager.NETWORK_TYPE_HSPAP:
return "HSPA+";
case TelephonyManager.NETWORK_TYPE_HSUPA:
return "HSUPA";
case TelephonyManager.NETWORK_TYPE_IDEN:
return "iDen";
case TelephonyManager.NETWORK_TYPE_LTE:
return "LTE";
case TelephonyManager.NETWORK_TYPE_UMTS:
return "UMTS";
case TelephonyManager.NETWORK_TYPE_UNKNOWN:
return "Unknown";
}
return "New type of network";
}
Best answer
public static String getNetworkType(Context context) {
TelephonyManager teleMan = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
int networkType = teleMan.getNetworkType();
switch ( networkType ) {
case TelephonyManager.NETWORK_TYPE_1xRTT:
return "1xRTT";
case TelephonyManager.NETWORK_TYPE_CDMA:
return "CDMA";
case TelephonyManager.NETWORK_TYPE_EDGE:
return "EDGE";
case TelephonyManager.NETWORK_TYPE_EHRPD:
return "eHRPD";
case TelephonyManager.NETWORK_TYPE_EVDO_0:
return "EVDO rev. 0";
case TelephonyManager.NETWORK_TYPE_EVDO_A:
return "EVDO rev. A";
case TelephonyManager.NETWORK_TYPE_EVDO_B:
return "EVDO rev. B";
case TelephonyManager.NETWORK_TYPE_GPRS:
return "GPRS";
case TelephonyManager.NETWORK_TYPE_HSDPA:
return "HSDPA";
case TelephonyManager.NETWORK_TYPE_HSPA:
return "HSPA";
case TelephonyManager.NETWORK_TYPE_HSPAP:
return "HSPA+";
case TelephonyManager.NETWORK_TYPE_HSUPA:
return "HSUPA";
case TelephonyManager.NETWORK_TYPE_IDEN:
return "iDen";
case TelephonyManager.NETWORK_TYPE_LTE:
return "LTE";
case TelephonyManager.NETWORK_TYPE_UMTS:
return "UMTS";
case TelephonyManager.NETWORK_TYPE_UNKNOWN:
return "Unknown";
}
return "New type of network";
}
answered Nov 17 '15 at 18:05
sonidasonida
3,76813138
3,76813138
add a comment |
add a comment |
/** Network type is unknown */
public static final int NETWORK_TYPE_UNKNOWN = 0;
/** Current network is GPRS */
public static final int NETWORK_TYPE_GPRS = 1;
/** Current network is EDGE */
public static final int NETWORK_TYPE_EDGE = 2;
/** Current network is UMTS */
public static final int NETWORK_TYPE_UMTS = 3;
/** Current network is CDMA: Either IS95A or IS95B*/
public static final int NETWORK_TYPE_CDMA = 4;
/** Current network is EVDO revision 0*/
public static final int NETWORK_TYPE_EVDO_0 = 5;
/** Current network is EVDO revision A*/
public static final int NETWORK_TYPE_EVDO_A = 6;
/** Current network is 1xRTT*/
public static final int NETWORK_TYPE_1xRTT = 7;
/** Current network is HSDPA */
public static final int NETWORK_TYPE_HSDPA = 8;
/** Current network is HSUPA */
public static final int NETWORK_TYPE_HSUPA = 9;
/** Current network is HSPA */
public static final int NETWORK_TYPE_HSPA = 10;
/** Current network is iDen */
public static final int NETWORK_TYPE_IDEN = 11;
/** Current network is EVDO revision B*/
public static final int NETWORK_TYPE_EVDO_B = 12;
/** Current network is LTE */
public static final int NETWORK_TYPE_LTE = 13;
/** Current network is eHRPD */
public static final int NETWORK_TYPE_EHRPD = 14;
/** Current network is HSPA+ */
public static final int NETWORK_TYPE_HSPAP = 15;
/** Current network is GSM {@hide} */
public static final int NETWORK_TYPE_GSM = 16;
/** Current network is TD_SCDMA {@hide} */
public static final int NETWORK_TYPE_TD_SCDMA = 17;
/** Current network is IWLAN {@hide} */
public static final int NETWORK_TYPE_IWLAN = 18;
List of networkType Provided.
add a comment |
/** Network type is unknown */
public static final int NETWORK_TYPE_UNKNOWN = 0;
/** Current network is GPRS */
public static final int NETWORK_TYPE_GPRS = 1;
/** Current network is EDGE */
public static final int NETWORK_TYPE_EDGE = 2;
/** Current network is UMTS */
public static final int NETWORK_TYPE_UMTS = 3;
/** Current network is CDMA: Either IS95A or IS95B*/
public static final int NETWORK_TYPE_CDMA = 4;
/** Current network is EVDO revision 0*/
public static final int NETWORK_TYPE_EVDO_0 = 5;
/** Current network is EVDO revision A*/
public static final int NETWORK_TYPE_EVDO_A = 6;
/** Current network is 1xRTT*/
public static final int NETWORK_TYPE_1xRTT = 7;
/** Current network is HSDPA */
public static final int NETWORK_TYPE_HSDPA = 8;
/** Current network is HSUPA */
public static final int NETWORK_TYPE_HSUPA = 9;
/** Current network is HSPA */
public static final int NETWORK_TYPE_HSPA = 10;
/** Current network is iDen */
public static final int NETWORK_TYPE_IDEN = 11;
/** Current network is EVDO revision B*/
public static final int NETWORK_TYPE_EVDO_B = 12;
/** Current network is LTE */
public static final int NETWORK_TYPE_LTE = 13;
/** Current network is eHRPD */
public static final int NETWORK_TYPE_EHRPD = 14;
/** Current network is HSPA+ */
public static final int NETWORK_TYPE_HSPAP = 15;
/** Current network is GSM {@hide} */
public static final int NETWORK_TYPE_GSM = 16;
/** Current network is TD_SCDMA {@hide} */
public static final int NETWORK_TYPE_TD_SCDMA = 17;
/** Current network is IWLAN {@hide} */
public static final int NETWORK_TYPE_IWLAN = 18;
List of networkType Provided.
add a comment |
/** Network type is unknown */
public static final int NETWORK_TYPE_UNKNOWN = 0;
/** Current network is GPRS */
public static final int NETWORK_TYPE_GPRS = 1;
/** Current network is EDGE */
public static final int NETWORK_TYPE_EDGE = 2;
/** Current network is UMTS */
public static final int NETWORK_TYPE_UMTS = 3;
/** Current network is CDMA: Either IS95A or IS95B*/
public static final int NETWORK_TYPE_CDMA = 4;
/** Current network is EVDO revision 0*/
public static final int NETWORK_TYPE_EVDO_0 = 5;
/** Current network is EVDO revision A*/
public static final int NETWORK_TYPE_EVDO_A = 6;
/** Current network is 1xRTT*/
public static final int NETWORK_TYPE_1xRTT = 7;
/** Current network is HSDPA */
public static final int NETWORK_TYPE_HSDPA = 8;
/** Current network is HSUPA */
public static final int NETWORK_TYPE_HSUPA = 9;
/** Current network is HSPA */
public static final int NETWORK_TYPE_HSPA = 10;
/** Current network is iDen */
public static final int NETWORK_TYPE_IDEN = 11;
/** Current network is EVDO revision B*/
public static final int NETWORK_TYPE_EVDO_B = 12;
/** Current network is LTE */
public static final int NETWORK_TYPE_LTE = 13;
/** Current network is eHRPD */
public static final int NETWORK_TYPE_EHRPD = 14;
/** Current network is HSPA+ */
public static final int NETWORK_TYPE_HSPAP = 15;
/** Current network is GSM {@hide} */
public static final int NETWORK_TYPE_GSM = 16;
/** Current network is TD_SCDMA {@hide} */
public static final int NETWORK_TYPE_TD_SCDMA = 17;
/** Current network is IWLAN {@hide} */
public static final int NETWORK_TYPE_IWLAN = 18;
List of networkType Provided.
/** Network type is unknown */
public static final int NETWORK_TYPE_UNKNOWN = 0;
/** Current network is GPRS */
public static final int NETWORK_TYPE_GPRS = 1;
/** Current network is EDGE */
public static final int NETWORK_TYPE_EDGE = 2;
/** Current network is UMTS */
public static final int NETWORK_TYPE_UMTS = 3;
/** Current network is CDMA: Either IS95A or IS95B*/
public static final int NETWORK_TYPE_CDMA = 4;
/** Current network is EVDO revision 0*/
public static final int NETWORK_TYPE_EVDO_0 = 5;
/** Current network is EVDO revision A*/
public static final int NETWORK_TYPE_EVDO_A = 6;
/** Current network is 1xRTT*/
public static final int NETWORK_TYPE_1xRTT = 7;
/** Current network is HSDPA */
public static final int NETWORK_TYPE_HSDPA = 8;
/** Current network is HSUPA */
public static final int NETWORK_TYPE_HSUPA = 9;
/** Current network is HSPA */
public static final int NETWORK_TYPE_HSPA = 10;
/** Current network is iDen */
public static final int NETWORK_TYPE_IDEN = 11;
/** Current network is EVDO revision B*/
public static final int NETWORK_TYPE_EVDO_B = 12;
/** Current network is LTE */
public static final int NETWORK_TYPE_LTE = 13;
/** Current network is eHRPD */
public static final int NETWORK_TYPE_EHRPD = 14;
/** Current network is HSPA+ */
public static final int NETWORK_TYPE_HSPAP = 15;
/** Current network is GSM {@hide} */
public static final int NETWORK_TYPE_GSM = 16;
/** Current network is TD_SCDMA {@hide} */
public static final int NETWORK_TYPE_TD_SCDMA = 17;
/** Current network is IWLAN {@hide} */
public static final int NETWORK_TYPE_IWLAN = 18;
List of networkType Provided.
answered Mar 6 '16 at 18:08
Bamboo ChemistBamboo Chemist
412
412
add a comment |
add a comment |
Also, add below required permission in your AndroidManifest.xml.
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Otherwise, you will face app crash while getting ConnectivityManager handle due to security exception.
add a comment |
Also, add below required permission in your AndroidManifest.xml.
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Otherwise, you will face app crash while getting ConnectivityManager handle due to security exception.
add a comment |
Also, add below required permission in your AndroidManifest.xml.
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Otherwise, you will face app crash while getting ConnectivityManager handle due to security exception.
Also, add below required permission in your AndroidManifest.xml.
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Otherwise, you will face app crash while getting ConnectivityManager handle due to security exception.
edited Mar 17 '12 at 9:21
answered Mar 17 '12 at 9:11
Krishna ShettyKrishna Shetty
51511032
51511032
add a comment |
add a comment |
In my experience... it's best to use Android training guides for these types of efforts. It's easy to get null pointer exceptions when you use these classes, and it's especially bad when you try to detect these connections when the app first opens and then the app crashes.
You can use the ConnectivityManager to check that you're actually connected to the Internet, and if so, what type of connection is in place:
http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html
You can use the ConnectivityManager to determine the active wireless radio:
http://developer.android.com/training/efficient-downloads/connectivity_patterns.html
add a comment |
In my experience... it's best to use Android training guides for these types of efforts. It's easy to get null pointer exceptions when you use these classes, and it's especially bad when you try to detect these connections when the app first opens and then the app crashes.
You can use the ConnectivityManager to check that you're actually connected to the Internet, and if so, what type of connection is in place:
http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html
You can use the ConnectivityManager to determine the active wireless radio:
http://developer.android.com/training/efficient-downloads/connectivity_patterns.html
add a comment |
In my experience... it's best to use Android training guides for these types of efforts. It's easy to get null pointer exceptions when you use these classes, and it's especially bad when you try to detect these connections when the app first opens and then the app crashes.
You can use the ConnectivityManager to check that you're actually connected to the Internet, and if so, what type of connection is in place:
http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html
You can use the ConnectivityManager to determine the active wireless radio:
http://developer.android.com/training/efficient-downloads/connectivity_patterns.html
In my experience... it's best to use Android training guides for these types of efforts. It's easy to get null pointer exceptions when you use these classes, and it's especially bad when you try to detect these connections when the app first opens and then the app crashes.
You can use the ConnectivityManager to check that you're actually connected to the Internet, and if so, what type of connection is in place:
http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html
You can use the ConnectivityManager to determine the active wireless radio:
http://developer.android.com/training/efficient-downloads/connectivity_patterns.html
answered Jan 12 '13 at 0:14
Lou MordaLou Morda
3,21012941
3,21012941
add a comment |
add a comment |
Better use would be:
NetworkInfo activeNetworkInfo = ((ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (activeNetworkInfo == null) return false;
switch (activeNetworkInfo.getType()) {
case ConnectivityManager.TYPE_WIFI:
return true;
}
// public static final int TYPE_BLUETOOTH = 7;
// public static final int TYPE_DUMMY = 8;
// public static final int TYPE_ETHERNET = 9;
// public static final int TYPE_MOBILE = 0;
// public static final int TYPE_MOBILE_DUN = 4;
// /** @deprecated */
// @Deprecated
// public static final int TYPE_MOBILE_HIPRI = 5;
// /** @deprecated */
// @Deprecated
// public static final int TYPE_MOBILE_MMS = 2;
// /** @deprecated */
// @Deprecated
// public static final int TYPE_MOBILE_SUPL = 3;
// public static final int TYPE_VPN = 17;
// public static final int TYPE_WIFI = 1;
// public static final int TYPE_WIMAX = 6;
Which other types you want to define and handle is up to you...
For those wanting a string identifier, better use:
activeNetworkInfo.getTypeName()
activeNetworkInfo.getSubtypeName()
add a comment |
Better use would be:
NetworkInfo activeNetworkInfo = ((ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (activeNetworkInfo == null) return false;
switch (activeNetworkInfo.getType()) {
case ConnectivityManager.TYPE_WIFI:
return true;
}
// public static final int TYPE_BLUETOOTH = 7;
// public static final int TYPE_DUMMY = 8;
// public static final int TYPE_ETHERNET = 9;
// public static final int TYPE_MOBILE = 0;
// public static final int TYPE_MOBILE_DUN = 4;
// /** @deprecated */
// @Deprecated
// public static final int TYPE_MOBILE_HIPRI = 5;
// /** @deprecated */
// @Deprecated
// public static final int TYPE_MOBILE_MMS = 2;
// /** @deprecated */
// @Deprecated
// public static final int TYPE_MOBILE_SUPL = 3;
// public static final int TYPE_VPN = 17;
// public static final int TYPE_WIFI = 1;
// public static final int TYPE_WIMAX = 6;
Which other types you want to define and handle is up to you...
For those wanting a string identifier, better use:
activeNetworkInfo.getTypeName()
activeNetworkInfo.getSubtypeName()
add a comment |
Better use would be:
NetworkInfo activeNetworkInfo = ((ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (activeNetworkInfo == null) return false;
switch (activeNetworkInfo.getType()) {
case ConnectivityManager.TYPE_WIFI:
return true;
}
// public static final int TYPE_BLUETOOTH = 7;
// public static final int TYPE_DUMMY = 8;
// public static final int TYPE_ETHERNET = 9;
// public static final int TYPE_MOBILE = 0;
// public static final int TYPE_MOBILE_DUN = 4;
// /** @deprecated */
// @Deprecated
// public static final int TYPE_MOBILE_HIPRI = 5;
// /** @deprecated */
// @Deprecated
// public static final int TYPE_MOBILE_MMS = 2;
// /** @deprecated */
// @Deprecated
// public static final int TYPE_MOBILE_SUPL = 3;
// public static final int TYPE_VPN = 17;
// public static final int TYPE_WIFI = 1;
// public static final int TYPE_WIMAX = 6;
Which other types you want to define and handle is up to you...
For those wanting a string identifier, better use:
activeNetworkInfo.getTypeName()
activeNetworkInfo.getSubtypeName()
Better use would be:
NetworkInfo activeNetworkInfo = ((ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (activeNetworkInfo == null) return false;
switch (activeNetworkInfo.getType()) {
case ConnectivityManager.TYPE_WIFI:
return true;
}
// public static final int TYPE_BLUETOOTH = 7;
// public static final int TYPE_DUMMY = 8;
// public static final int TYPE_ETHERNET = 9;
// public static final int TYPE_MOBILE = 0;
// public static final int TYPE_MOBILE_DUN = 4;
// /** @deprecated */
// @Deprecated
// public static final int TYPE_MOBILE_HIPRI = 5;
// /** @deprecated */
// @Deprecated
// public static final int TYPE_MOBILE_MMS = 2;
// /** @deprecated */
// @Deprecated
// public static final int TYPE_MOBILE_SUPL = 3;
// public static final int TYPE_VPN = 17;
// public static final int TYPE_WIFI = 1;
// public static final int TYPE_WIMAX = 6;
Which other types you want to define and handle is up to you...
For those wanting a string identifier, better use:
activeNetworkInfo.getTypeName()
activeNetworkInfo.getSubtypeName()
answered Sep 1 '17 at 10:04
RickRick
73611222
73611222
add a comment |
add a comment |
//**Return type of connection to network
public static int getNetworkType(Context context) {
if (!isNetworkConnected(context))
return -1;
ConnectivityManager conMan = (ConnectivityManager) context.
getSystemService(Context.CONNECTIVITY_SERVICE);
//mobile
State mobile = conMan.getNetworkInfo(0).getState();
//wifi
State wifi = conMan.getNetworkInfo(1).getState();
int result = 0;
if (mobile == State.CONNECTED || mobile == State.CONNECTING) {
result |= NETWORK_MOBILE;
}
if (wifi == State.CONNECTED || wifi == State.CONNECTING) {
result |= NETWORK_WIFI;
}
return result;
}
add a comment |
//**Return type of connection to network
public static int getNetworkType(Context context) {
if (!isNetworkConnected(context))
return -1;
ConnectivityManager conMan = (ConnectivityManager) context.
getSystemService(Context.CONNECTIVITY_SERVICE);
//mobile
State mobile = conMan.getNetworkInfo(0).getState();
//wifi
State wifi = conMan.getNetworkInfo(1).getState();
int result = 0;
if (mobile == State.CONNECTED || mobile == State.CONNECTING) {
result |= NETWORK_MOBILE;
}
if (wifi == State.CONNECTED || wifi == State.CONNECTING) {
result |= NETWORK_WIFI;
}
return result;
}
add a comment |
//**Return type of connection to network
public static int getNetworkType(Context context) {
if (!isNetworkConnected(context))
return -1;
ConnectivityManager conMan = (ConnectivityManager) context.
getSystemService(Context.CONNECTIVITY_SERVICE);
//mobile
State mobile = conMan.getNetworkInfo(0).getState();
//wifi
State wifi = conMan.getNetworkInfo(1).getState();
int result = 0;
if (mobile == State.CONNECTED || mobile == State.CONNECTING) {
result |= NETWORK_MOBILE;
}
if (wifi == State.CONNECTED || wifi == State.CONNECTING) {
result |= NETWORK_WIFI;
}
return result;
}
//**Return type of connection to network
public static int getNetworkType(Context context) {
if (!isNetworkConnected(context))
return -1;
ConnectivityManager conMan = (ConnectivityManager) context.
getSystemService(Context.CONNECTIVITY_SERVICE);
//mobile
State mobile = conMan.getNetworkInfo(0).getState();
//wifi
State wifi = conMan.getNetworkInfo(1).getState();
int result = 0;
if (mobile == State.CONNECTED || mobile == State.CONNECTING) {
result |= NETWORK_MOBILE;
}
if (wifi == State.CONNECTED || wifi == State.CONNECTING) {
result |= NETWORK_WIFI;
}
return result;
}
answered Nov 1 '18 at 13:01
Milad shoeibiMilad shoeibi
113
113
add a comment |
add a comment |
If you want to know which type of network it is, you can use this :
private String getNetworkClass() {
// network type
TelephonyManager mTelephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
int networkType = mTelephonyManager != null ? mTelephonyManager.getNetworkType() : 0;
switch (networkType) {
case TelephonyManager.NETWORK_TYPE_UNKNOWN:
return "Unknown network";
case TelephonyManager.NETWORK_TYPE_GSM:
return " GSM";
case TelephonyManager.NETWORK_TYPE_CDMA:
case TelephonyManager.NETWORK_TYPE_1xRTT:
case TelephonyManager.NETWORK_TYPE_IDEN:
return " 2G";
case TelephonyManager.NETWORK_TYPE_GPRS:
return " GPRS (2.5G)";
case TelephonyManager.NETWORK_TYPE_EDGE:
return " EDGE (2.75G)";
case TelephonyManager.NETWORK_TYPE_UMTS:
case TelephonyManager.NETWORK_TYPE_EVDO_0:
case TelephonyManager.NETWORK_TYPE_EVDO_A:
case TelephonyManager.NETWORK_TYPE_EVDO_B:
return " 3G";
case TelephonyManager.NETWORK_TYPE_HSPA:
case TelephonyManager.NETWORK_TYPE_HSDPA:
case TelephonyManager.NETWORK_TYPE_HSUPA:
return " H (3G+)";
case TelephonyManager.NETWORK_TYPE_EHRPD:
case TelephonyManager.NETWORK_TYPE_HSPAP:
case TelephonyManager.NETWORK_TYPE_TD_SCDMA:
return " H+ (3G++)";
case TelephonyManager.NETWORK_TYPE_LTE:
case TelephonyManager.NETWORK_TYPE_IWLAN:
return " 4G";
default:
return " 4G+";
}
}
add a comment |
If you want to know which type of network it is, you can use this :
private String getNetworkClass() {
// network type
TelephonyManager mTelephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
int networkType = mTelephonyManager != null ? mTelephonyManager.getNetworkType() : 0;
switch (networkType) {
case TelephonyManager.NETWORK_TYPE_UNKNOWN:
return "Unknown network";
case TelephonyManager.NETWORK_TYPE_GSM:
return " GSM";
case TelephonyManager.NETWORK_TYPE_CDMA:
case TelephonyManager.NETWORK_TYPE_1xRTT:
case TelephonyManager.NETWORK_TYPE_IDEN:
return " 2G";
case TelephonyManager.NETWORK_TYPE_GPRS:
return " GPRS (2.5G)";
case TelephonyManager.NETWORK_TYPE_EDGE:
return " EDGE (2.75G)";
case TelephonyManager.NETWORK_TYPE_UMTS:
case TelephonyManager.NETWORK_TYPE_EVDO_0:
case TelephonyManager.NETWORK_TYPE_EVDO_A:
case TelephonyManager.NETWORK_TYPE_EVDO_B:
return " 3G";
case TelephonyManager.NETWORK_TYPE_HSPA:
case TelephonyManager.NETWORK_TYPE_HSDPA:
case TelephonyManager.NETWORK_TYPE_HSUPA:
return " H (3G+)";
case TelephonyManager.NETWORK_TYPE_EHRPD:
case TelephonyManager.NETWORK_TYPE_HSPAP:
case TelephonyManager.NETWORK_TYPE_TD_SCDMA:
return " H+ (3G++)";
case TelephonyManager.NETWORK_TYPE_LTE:
case TelephonyManager.NETWORK_TYPE_IWLAN:
return " 4G";
default:
return " 4G+";
}
}
add a comment |
If you want to know which type of network it is, you can use this :
private String getNetworkClass() {
// network type
TelephonyManager mTelephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
int networkType = mTelephonyManager != null ? mTelephonyManager.getNetworkType() : 0;
switch (networkType) {
case TelephonyManager.NETWORK_TYPE_UNKNOWN:
return "Unknown network";
case TelephonyManager.NETWORK_TYPE_GSM:
return " GSM";
case TelephonyManager.NETWORK_TYPE_CDMA:
case TelephonyManager.NETWORK_TYPE_1xRTT:
case TelephonyManager.NETWORK_TYPE_IDEN:
return " 2G";
case TelephonyManager.NETWORK_TYPE_GPRS:
return " GPRS (2.5G)";
case TelephonyManager.NETWORK_TYPE_EDGE:
return " EDGE (2.75G)";
case TelephonyManager.NETWORK_TYPE_UMTS:
case TelephonyManager.NETWORK_TYPE_EVDO_0:
case TelephonyManager.NETWORK_TYPE_EVDO_A:
case TelephonyManager.NETWORK_TYPE_EVDO_B:
return " 3G";
case TelephonyManager.NETWORK_TYPE_HSPA:
case TelephonyManager.NETWORK_TYPE_HSDPA:
case TelephonyManager.NETWORK_TYPE_HSUPA:
return " H (3G+)";
case TelephonyManager.NETWORK_TYPE_EHRPD:
case TelephonyManager.NETWORK_TYPE_HSPAP:
case TelephonyManager.NETWORK_TYPE_TD_SCDMA:
return " H+ (3G++)";
case TelephonyManager.NETWORK_TYPE_LTE:
case TelephonyManager.NETWORK_TYPE_IWLAN:
return " 4G";
default:
return " 4G+";
}
}
If you want to know which type of network it is, you can use this :
private String getNetworkClass() {
// network type
TelephonyManager mTelephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
int networkType = mTelephonyManager != null ? mTelephonyManager.getNetworkType() : 0;
switch (networkType) {
case TelephonyManager.NETWORK_TYPE_UNKNOWN:
return "Unknown network";
case TelephonyManager.NETWORK_TYPE_GSM:
return " GSM";
case TelephonyManager.NETWORK_TYPE_CDMA:
case TelephonyManager.NETWORK_TYPE_1xRTT:
case TelephonyManager.NETWORK_TYPE_IDEN:
return " 2G";
case TelephonyManager.NETWORK_TYPE_GPRS:
return " GPRS (2.5G)";
case TelephonyManager.NETWORK_TYPE_EDGE:
return " EDGE (2.75G)";
case TelephonyManager.NETWORK_TYPE_UMTS:
case TelephonyManager.NETWORK_TYPE_EVDO_0:
case TelephonyManager.NETWORK_TYPE_EVDO_A:
case TelephonyManager.NETWORK_TYPE_EVDO_B:
return " 3G";
case TelephonyManager.NETWORK_TYPE_HSPA:
case TelephonyManager.NETWORK_TYPE_HSDPA:
case TelephonyManager.NETWORK_TYPE_HSUPA:
return " H (3G+)";
case TelephonyManager.NETWORK_TYPE_EHRPD:
case TelephonyManager.NETWORK_TYPE_HSPAP:
case TelephonyManager.NETWORK_TYPE_TD_SCDMA:
return " H+ (3G++)";
case TelephonyManager.NETWORK_TYPE_LTE:
case TelephonyManager.NETWORK_TYPE_IWLAN:
return " 4G";
default:
return " 4G+";
}
}
answered Nov 21 '18 at 22:17
ChristianChristian
312216
312216
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2919414%2fget-network-type%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you select answer from following? So people reach to this thread can know the accepted answer.
– Mr. Sajid Shaikh
Nov 25 '15 at 8:46