首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ConnectivityManager/NetworkInfo和公共wifi代理

ConnectivityManager/NetworkInfo和公共wifi代理
EN

Stack Overflow用户
提问于 2015-01-12 19:12:42
回答 1查看 490关注 0票数 1

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

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

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

谢谢

接入点

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-11 16:40:13

我使用BroadcastReceiver来接受CONNECTIVITY_CHANGE

代码语言:javascript
复制
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;
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27909083

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档