我知道如何检查wifi是否启用。
代码:
WifiManager wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
if(wifi.isWifiEnabled())
{
//Code execution comes here
}
但是如何确定用户是否实际已连接到附近的wifi网络(或任何无线网络)?
编辑:我的意思是问,如果用户在输入密码后登录到wifi网络,那么他只能使用该wifi。那么无论如何都要检查是否已连接(登录)到任何wifi网络?
答案 0 :(得分:1)
您应该能够使用ConnectivityManager来获取Wifi适配器的状态。如果它已经连接,你可以check ......
检查wifi是否被连接的方法: -
public static Boolean checkWIFI(Activity activity) {
Log.d("checkWIFI", "checkWIFI");
ConnectivityManager cm = (ConnectivityManager) activity
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
Log.d("NetworkInfo", "NetworkInfo" + netInfo);
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
} else if (netInfo != null
&& (netInfo.getState() == NetworkInfo.State.DISCONNECTED
|| netInfo.getState() == NetworkInfo.State.DISCONNECTING
|| netInfo.getState() == NetworkInfo.State.SUSPENDED || netInfo
.getState() == NetworkInfo.State.UNKNOWN)) {
return false;
} else {
return false;
}
}
您需要在清单中添加权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET" />
祝你好运!!
答案 1 :(得分:0)
试试这个:
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWifi.isConnected()) {
// Do whatever
}
希望这会有所帮助.. :)
答案 2 :(得分:0)
试试这个
ConnectivityManager con=(ConnectivityManager)getSystemService(Activity.CONNECTIVITY_SERVICE);
boolean wifi=con.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
//check Internet connection
if(wifi)
{
//do what you want
}else{
}
还添加了必需权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
希望这可以解决问题
答案 3 :(得分:0)
/**
*
* Method to check internet connection is available
*/
public static boolean isInternetAvailable(Context context) {
boolean haveConnectedWifi = false;
boolean haveConnectedMobile = false;
boolean connectionavailable = false;
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
NetworkInfo informationabtnet = cm.getActiveNetworkInfo();
for (NetworkInfo ni : netInfo) {
try {
if (ni.getTypeName().equalsIgnoreCase("WIFI"))
if (ni.isConnected())
haveConnectedWifi = true;
if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
if (ni.isConnected())
haveConnectedMobile = true;
if (informationabtnet.isAvailable()
&& informationabtnet.isConnected())
connectionavailable = true;
} catch (Exception e) {
// TODO: handle exception
System.out.println("Inside utils catch clause , exception is"
+ e.toString());
e.printStackTrace();
}
}
return haveConnectedWifi || haveConnectedMobile;
}
我在我的项目中尝试了类似的代码并且正在运行。希望这可以帮助你......:)
答案 4 :(得分:0)
只需为wifi网络调用此方法,这可能会对您有所帮助:)
这样调用checkWiFi(this);
//使用此方法传递context
此处我通过this
进行当前活动
public static final boolean checkWiFi(Context cn)
{
ConnectivityManager connManager = (ConnectivityManager) cn.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWifi.isConnected())
return true;
else
return false;
}
也获得许可
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />