ConnectivityManager/NetworkInfo 和公共 wifi 代理



我有一个关于Android中ConnectivityManagerNetworkInfo类的问题。

如果我走进蒂姆霍顿斯、星巴克、麦当劳等,他们都有带有密码或某种代理的免费 wifi,布尔值isConnected()返回正确的标志(在这种情况下,您将连接到 wifi,但在您登录/注册之前互联网不可用)

如果此标志(或NetworkInfo中的另一个标志)没有返回正确的值,是否有类似 iPhone Reachability 类来检查此标志。

谢谢

美联社

我用BroadcastReceiver来接受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;
    }
}

最新更新