TelephonyManager.getNetworkType() and HSDPA problems :( - android

I have been using the following code to establish which network the device is using :
TelephonyManager tempManager;
tempManager= (TelephonyManager)myContext.getSystemService(Context.TELEPHONY_SERVICE);
int result = 0;
if(tempManager != null && tempManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS) //do we have a UMTS connection ?
{
result = 2;
}
else if(tempManager != null && tempManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_GPRS) //or is it just a shabby 2g connection ?
{
result = 1;
}
else if(tempManager != null && tempManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_UNKNOWN) //or is it just a shabby 2g connection ?
{
result = 4;
}
return result;
It works pretty well unless I get on a HSDPA connection, in that case it will always return 0 as a result, which in my case makes my software think it has no connection at all :(
Anyone who knows whats happening, has some experience regarding this and most importantly has some solution to this problem ???
Thanks in advance

There is a enum also for HSDPA
To check if there is connection and also get a type, I would rather user getActiveNetworkInfo and isConnected. It returns null when there is no connection. You can also check the type of connection by getType and getSubtypeName methods or you can mix with your approach.

This is the code I am using. It will safely check for a connection and then return the connection type. For my application, I only care about Wifi, Mobile, or not connected so that is what this function will return. Change for your own situation.
//Check weather Internet connection is available or not
public int checkConnectionType()
{
final ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo activeNetInfo = conMgr.getActiveNetworkInfo();
if (activeNetInfo != null && activeNetInfo.isAvailable() && activeNetInfo.isConnected())
{
int type = activeNetInfo.getType();
if (type == ConnectivityManager.TYPE_MOBILE || type == ConnectivityManager.TYPE_MOBILE_DUN
|| type == ConnectivityManager.TYPE_MOBILE_HIPRI || type == ConnectivityManager.TYPE_MOBILE_MMS
|| type == ConnectivityManager.TYPE_MOBILE_SUPL || type == ConnectivityManager.TYPE_WIMAX)
{
return ConnectivityManager.TYPE_MOBILE;
}
else if (type == ConnectivityManager.TYPE_WIFI)
{
return ConnectivityManager.TYPE_WIFI;
}
else
{
// Unknown connection type, so to be safe say mobile
return ConnectivityManager.TYPE_MOBILE;
}
}
else
{
// return not connected
return -1;
}
}
You will need this permission in your app's manifest
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Related

When my android app is coming to foreground sometimes ConnectivityManager.activeNetwork object is null even when there is a good internet connection

I am working on android app where I need to check for internet connection if available I hit the api inside onStart method else No-Internet popup opens. But sometimes(not everytime) when my app comes to foreground after hitting home button and then reopening the app ConnectivityManager.activeNetwork object is null. Why would this might happen or what approach I can follow to escape this problem. Can I delay the api request using handler?
Following is my method to check internet connection. When I call this method in onStart "cm" object is null sometimes hence opening no-internetpopup.
fun isConnected(): Boolean {
val cm = instance.getSystemService(Context.CONNECTIVITY_SERVICE) as
ConnectivityManager
if (cm != null) {
if (Build.VERSION.SDK_INT < 23) {
val ni = cm.activeNetworkInfo
if (ni != null) {
return (ni.isConnected && (ni.type == ConnectivityManager.TYPE_WIFI
|| ni.type == ConnectivityManager.TYPE_MOBILE
|| ni.type == ConnectivityManager.TYPE_BLUETOOTH))
}
} else {
Log.d("no_internet", "1" + cm.activeNetwork)
val n = cm.activeNetwork
if (n != null) {
val nc = cm.getNetworkCapabilities(n)
if (nc != null) {
return (nc.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)
|| nc.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)
|| nc.hasTransport(NetworkCapabilities.TRANSPORT_BLUETOOTH))
}
}
}
}
return false
}
Note:This issue occurs in some devices with o/s android 11, specifically samsung SM-M315F
this only occurs when you use the method on onStart?
Maybe you should check you Manifest for <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Android App crashes on 4G internet connection but works fine on GSM mode Only

I have created an android application which user Http Post and Get calls to read and write data from web server. Nothing too flashy also. My phone/SIM has 4G. Every time the app tries to connect, thing go slow and most of the time the app crashes. So I changed settings to GSM only. Guess what? The app works much better. What could be the possible reasons for it?
Use the following method in order to detect all available type of networks at your client's disposal :
public static boolean checkNetworkRechability(Context context) {
Boolean bNetwork = false;
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
for (NetworkInfo networkInfo : connectivityManager.getAllNetworkInfo()) {
int netType = networkInfo.getType();
int netSubType = networkInfo.getSubtype();
if (netType == ConnectivityManager.TYPE_WIFI) {
bNetwork = networkInfo.isConnected();
if (bNetwork == true)
break;
} else if (netType == ConnectivityManager.TYPE_MOBILE && netSubType != TelephonyManager.NETWORK_TYPE_UNKNOWN) {
bNetwork = networkInfo.isConnected();
if (bNetwork == true)
break;
} else {
bNetwork = false;
}
}
if (!bNetwork) {
Log.i(TAG, "You are not in network");
}
Log.i(TAG, "bNetwork : " + bNetwork);
return bNetwork;
}

how to check net connection in android phone [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Android - detect whether there is an Internet connection available
How to check network connection enable or disable in WIFI and 3G(data plan) in mobile?
In my cell when I run my app then this application is crashed because net connection is not there.so how to give the message that the connection is not there like. or on your wif
please help me
thank you
See below code.
ConnectivityManager conMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
// ARE WE CONNECTED TO THE NET
if (conMgr.getActiveNetworkInfo() != null
&& conMgr.getActiveNetworkInfo().isAvailable()
&& conMgr.getActiveNetworkInfo().isConnected()) {
return true;
// internet connection is wroking
} else {
return false;
// internet connection is not wroking
}
Use this method in your Activity and call this when you want to know whether the net is available or not.
public boolean isInternetOn(Context ctx) {
this.mContext = ctx;
ConnectivityManager Connect_Manager = (ConnectivityManager) mContext
.getSystemService(Context.CONNECTIVITY_SERVICE);
State connected = NetworkInfo.State.CONNECTED;
State connecting = NetworkInfo.State.CONNECTING;
State disconnected = NetworkInfo.State.DISCONNECTED;
State info0 = Connect_Manager.getNetworkInfo(0).getState();
State info1 = Connect_Manager.getNetworkInfo(1).getState();
// ARE WE CONNECTED TO THE NET
if (info0 == connected || info0 == connecting || info1 == connecting
|| info1 == connected) {
// MESSAGE TO SCREEN FOR TESTING (IF REQ)
// Toast.makeText(this, connectionType + " connected",
// Toast.LENGTH_SHORT).show();
Log.d("Internet", "Connected");
return true;
} else if (info0 == disconnected || info1 == disconnected) {
Log.d("Internet", "DisConnected");
// System.out.println("Not Connected");
return false;
}
return false;
}
Now to check Internet Conncetion,
if (isInternetOn(this))
{
//Do stuff
}
else
{
//show alert
}

Checking the internet connection in android is either through Wifi or through GSM/3G?

How can I check using code whether the connection made is through Wifi or GSM/3G in android?
From here: Detect network connection type on Android
// Only update if WiFi or 3G is connected and not roaming
int netType = info.getType();
int netSubtype = info.getSubtype();
if (netType == ConnectivityManager.TYPE_WIFI) {
return info.isConnected();
} else if (netType == ConnectivityManager.TYPE_MOBILE
&& netSubtype == TelephonyManager.NETWORK_TYPE_UMTS
&& !mTelephony.isNetworkRoaming()) {
return info.isConnected();
} else {
return false;
}
Better yet, you can use TelephonyManager.
TelephonyManager tm = (TelephonyManager)
getSystemService(TELEPHONY_SERVICE);
String operatorname = tm.getNetworkOperatorName();
Please check this link http://developer.android.com/reference/android/telephony/TelephonyManager.html

How to determine Android internet connection?

How can I determine the current internet connection type available to an Android device? e.g. have it return WiFi, 3G, none.
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.
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
see this post, I answered the same question
how to save request and resend in android app
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

Categories

Resources