Checking for network availability not working - android

I use the following code to check the network status of an android device (Source)
public boolean isNetworkAvailable() {
ConnectivityManager cm = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
return true;
}
return false;
}
After running it on my phone, I find that it does return "true" when network is available. But when I turn on the Airplane Mode on my phone, I find that it still returns "true". What is wrong here? Also, does a "true" mean that the wifi is also on or just that cellular network is available?
Thanks
PS: And yes, I have added the ACCESS_NETWORK_STATE permission in the manifest.

You are right. True value means that there is internet connection from wifi or cellular network. You can have air plane mode on but if you still have wifi on your method will return true.
See also: http://developer.android.com/reference/android/net/NetworkInfo.html#isConnected()

Related

Android : how to programatically determine whether the system has a 3G/4G radio?

I am programming a Nexus 7 and need to determine whether the system has a 3G/4G radio or not. How do I do that?
Use TelephonyManager.getPhoneType (). Returned values are
PHONE_TYPE_NONE
PHONE_TYPE_GSM
PHONE_TYPE_CDMA
PHONE_TYPE_SIP
Checking if CDMA or GSM should work
You can probably just check for the presence of a mobile network type.
boolean hasRadio()
{
ConnectivityManager connManager = (ConnectivityManager) getActivity().getSystemService(Activity.CONNECTIVITY_SERVICE);
return (connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) != null);
}

connectivitymanager.getActiveNetworkInfo() returning true when Internet is off (Android)?

This is the code I am using to determine if the phone is connected to the Internet:
public boolean isOnline() {
ConnectivityManager cm =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
On an emulator, this is returning true even when I turn my computer's Wi-Fi off. I see a 3G sign on the topmost bar of the emulator, even when the computer is off the Internet. Is there something wrong with my code, or is this an emulator problem?
You should call isConnected instead of isConnectedOrConnecting if you want to determine whether the device is connected at the time of call to isOnline.
Generally, there are many problems/bugs with hardware-features APIs on the emulator, this might be one of them. Your code is fine.

check internet activity

I done an app that uses a webview to listen audio from internet. Now, I add a method (isOnline) to check if there's (or not) an internet activity. I've a doubt: I can use this method running always in OnCreate?
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
If you block the execution of the onCreate() aka the UI thread will be blocked and your app will throw ANR (Application Not Responding) exception.
However your method doesn't tell you that you are connected to the interned, it just tell you that your device isConnectedOrConnecting to nearest router. What is after that you don't know.
For check real internet connection you need to make a request to a server, such as www.google.com and if you get the answer then you have the connection live.
And another thing, you don't need to monitor your connection continuously. In the most simple manner you can do this in a background thread from time to time, but only if you cannot get events from your radio player.

Android: How can I determine if Application starts with no signal or no service?

I have a phone state listener. I listen for service state, call state, and signal strength changes. This is all well and good.
BUT...
If my application starts in a condition with no signal strength, there is no "change" and I am not notified. I cannot, therefore, determine if I HAVE NO SIGNAL.
Is there a way to actively request/determine weather or not I have a signal or network service (not internet) on STARTUP??
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager != null && connectivityManager.getActiveNetworkInfo() != null)
{
// Network is available
}
else
{
// No connection available.
}
update:
You also need the permission:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
update2:
Looks like there are issues with connectivityManager so it's not 100% reliable:
http://code.google.com/p/android/issues/detail?id=11588
http://code.google.com/p/android/issues/detail?id=11891
http://code.google.com/p/android/issues/detail?id=11866

How to respect network use settings in Android

My app performs some backgound data collection and I'm adding support for the user network preferences, such as performing background updates and data roaming. I have the following checks already:
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if(cm.getBackgroundDataSetting()) {
...
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected()) {
with the required entries in the manifest:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
This all appears to be working fine, but I was wondering if I should be checking anything else? I was worried about checking for data roaming but the docs state that networkInfo.isAvailable() checks this for me. So are there any other checks I need to implement for network settings? Anything else in this area I should be aware of?
The user may change the settings while your background app is running. The API recommends that you listen to the broadcast message:
ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED
Perhaps you are checking cm.getBackgroundDataSetting() prior to sending data, and I suspect this would be sufficient. However, listening to the broadcast message will let you resume sending background data when the settings are changed.
I believe that either listening to the broadcast message or checking the settings before sending data will suffice. Android docs recommends the former.
Additionally to your checks I also do check for roaming state for 3g network:
NetworkInfo info = m_connectivityManager.getActiveNetworkInfo();
int netType = info.getType();
int netSubtype = info.getSubtype();
if (netType == ConnectivityManager.TYPE_WIFI || netType == ConnectivityManager.TYPE_WIMAX)
{
//no restrictions, do some networking
}
else if (netType == ConnectivityManager.TYPE_MOBILE &&
netSubtype == TelephonyManager.NETWORK_TYPE_UMTS)
{
//3G connection
if(!m_telephonyManager.isNetworkRoaming())
{
//do some networking
}
}
My application uses a lot of data, so I do not allow data downloading on non-3g mobile networks.
Like #inazaruk, I a also trying to prevent (for the user possibly expensive) network transfers when roaming or downloading images while on GPRS only. Therefore in Zwitscher I've implemented a NetworkHelper that checks against users preferences on romaing and minimal network state; https://github.com/pilhuhn/ZwitscherA/blob/master/src/de/bsd/zwitscher/helper/NetworkHelper.java
The matching preferences are here: https://github.com/pilhuhn/ZwitscherA/blob/master/res/xml/preferences.xml#L30

Categories

Resources