为什么getNetworkInfo()不起作用?

时间:2011-07-15 09:35:43

标签: android wifi connectivity wifimanager

我仍然想检查手机是否使用wifi网络...我已经发布了这个问题,但线程看起来已经死了:Android ... how to find out if I'm on a wifi internet?

ConnectivityManager connectivity = (ConnectivityManager)c.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiInfo = connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifiInfo.getState() == NetworkInfo.State.CONNECTED || wifiInfo.getState() == NetworkInfo.State.CONNECTING) 
{
    return true;
}

这总是在wifiInfo = connectivity.getNetworkInfo()崩溃,不管怎样我改变代码......我甚至试过这个:

WifiManager wman = (WifiManager)c.getSystemService(Context.WIFI_SERVICE);
WifiInfo wi = wman.getConnectionInfo();
if(wi.getNetworkId() == -1)
    return false;
else
    return true;

同样在... wman.getConnectionInfo();

崩溃

0 个答案:

没有答案