I have a question about the ConnectivityManager
and the NetworkInfo
classes in Android.
If I walked into a Tim Hortons, Starbucks, McDonalds, etc. who all have free wifi with a password or some sort of proxy, does the boolean isConnected()
return the proper flag (in this case you would be connected to wifi but the internet is not available until you login/register)
If this flag (or another flag in NetworkInfo
) does not return the proper value, is there something like iPhone Reachability
class to check this flag.
Thanks
AP
I used a BroadcastReceiver
to accept the CONNECTIVITY_CHANGE
.
private void debugIntent(Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
for (String key: extras.keySet()) {
LogText.appendLog(TAG + " key [" + key + "]: " +extras.get(key));
}
}
else {
LogText.appendLog(TAG + "no extras");
}
}
@SuppressWarnings("deprecation")
public boolean hasConnectivity(final Context context, final Intent intent) {
boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
NetworkInfo currentNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
NetworkInfo otherNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);
if(currentNetworkInfo != null){
return noConnectivity == false && currentNetworkInfo.isConnected();
}
return false;
}
public class ReachabilityTest extends AsyncTask<Void, Void, Boolean> {
private Context mContext;
private Intent mIntent;
private String mHostname;
private int mServicePort;
public ReachabilityTest(Context context, Intent intent, String hostname, int port) {
mContext = context.getApplicationContext(); // Avoid leaking the Activity!
mIntent = intent;
mHostname = hostname;
mServicePort = port;
}
@Override
protected Boolean doInBackground(Void... args) {
if (hasConnectivity(mContext, mIntent)) {
InetAddress address = isResolvable(mHostname);
if (address != null) {
if (canConnect(address, mServicePort)) {
return true;
}
}
}
return false;
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
Intent newIntent = new Intent(mContext, NetworkConnService.class);
newIntent.putExtra("hasConnectivity", result);
mContext.startService(newIntent);
}
@SuppressWarnings("deprecation")
private boolean hasConnectivity(final Context context, final Intent intent) {
boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
NetworkInfo currentNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
NetworkInfo otherNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);
if(currentNetworkInfo != null){
LogText.appendLog("Current Network Info: " + currentNetworkInfo.getTypeName() + " isConnected: " + currentNetworkInfo.isConnected());
return noConnectivity == false && currentNetworkInfo.isConnected();
}
if(otherNetworkInfo != null)
LogText.appendLog("Other Network Info: " + otherNetworkInfo.getTypeName() + " isConnected: " + otherNetworkInfo.isConnected());
return false;
}
private InetAddress isResolvable(String hostname) {
try {
return InetAddress.getByName(hostname);
}
catch (UnknownHostException e) {
LogText.appendLog("isResolvable " + e.getLocalizedMessage());
return null;
}
}
private boolean canConnect(InetAddress address, int port) {
Socket socket = new Socket();
SocketAddress socketAddress = new InetSocketAddress(address, port);
try {
socket.connect(socketAddress, 2000);
}
catch (IOException e) {
return false;
}
finally {
if (socket.isConnected()) {
try {
socket.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
}
}