I have an application that tries to be a good Samaritan, mainly when any services that should interact with the network are launched. I check if the device has the ability to connect:
HasConnectivity Method
private boolean hasConnectivity() {
NetworkInfo info = mConnectivity.getActiveNetworkInfo();
boolean connected = false;
int netType = -1;
int netSubtype = -1;
if (info == null) {
Log.w(sTag, "network info is null");
notifyUser(MyIntent.ACTION_VIEW_ALL, "There is no network connectivity.", false);
} else if (!mConnectivity.getBackgroundDataSetting()) {
Log.w(sTag, "background data setting is not enabled");
notifyUser(MyIntent.ACTION_VIEW_ALL, "background data setting is disabled", false);
} else {
netType = info.getType();
netSubtype = info.getSubtype();
if (netType == ConnectivityManager.TYPE_WIFI) {
connected = info.isConnected();
} else if (netType == ConnectivityManager.TYPE_MOBILE
&& netSubtype == TelephonyManager.NETWORK_TYPE_UMTS
&& !mTelephonyManager.isNetworkRoaming()
) {
connected = info.isConnected();
} else if (info.isRoaming()) {
notifyUser(MyIntent.ACTION_VIEW_ALL, "Currently Roaming skipping check.", false);
} else if (info.isAvailable()) {
connected = info.isConnected();
} else {
notifyUser(MyIntent.ACTION_VIEW_ALL, "..There is no network connectivity.", false);
}
}
return connected;
}
When this method returns false, I know that I do not have a connection, because the user is on the phone or 3G and Wi-Fi is not available
What is the best way to find out about connection availability without periodically checking network statistics using a timer ?
Is there any intentional action that I can observe with a broadcast receiver that will announce a change with the connection?
, , , .