我希望我的 android 设备连接到 wifi 热点。我创建了一个新的wificonfiguration
并将其添加到 中wifimanager
,这wificonfiguration
有NetworkId
。然后我调用该函数wifi.enableNetwork(NetworkId, true).
之后,我认为请求者将通过获取IP地址,身份验证,最后物理连接到热点。那么有没有办法识别wifi是否物理连接?
我更喜欢类似处理程序的方法。
我希望我的 android 设备连接到 wifi 热点。我创建了一个新的wificonfiguration
并将其添加到 中wifimanager
,这wificonfiguration
有NetworkId
。然后我调用该函数wifi.enableNetwork(NetworkId, true).
之后,我认为请求者将通过获取IP地址,身份验证,最后物理连接到热点。那么有没有办法识别wifi是否物理连接?
我更喜欢类似处理程序的方法。
你可以试试这个:
ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifi.isConnected()) {
// Your code here
}
编辑:更多细节:
BroadcastReceiver
在您的清单中注册 a ,如下所示:
<receiver android:name="WifiReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
<action android:name="android.net.wifi.STATE_CHANGE"/>
</intent-filter>
</receiver>
然后将上面的代码放在onReceive()
接收器的方法上,如下所示:
@Override
public void onReceive(Context context, final Intent intent) {
ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifi.isConnected()) {
// Your code here
}
}
你可以检查所有的网络。如果您只想要 WIFI,您可以取消检查其他 2 个网络。
public static boolean hasInternetConnection()
{
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifiNetwork != null && wifiNetwork.isConnected())
{
return true;
}
NetworkInfo mobileNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mobileNetwork != null && mobileNetwork.isConnected())
{
return true;
}
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnected())
{
return true;
}
return false;
}
不要忘记在清单中添加以下内容:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
这可能对您有所帮助。
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;
Log.i("ConnectionAvailable", "" + connectionavailable);
} catch (Exception e) {
System.out.println("Inside utils catch clause , exception is"
+ e.toString());
e.printStackTrace();
}
}
return haveConnectedWifi || haveConnectedMobile;
}
getNetworkInfo(int) 方法已弃用。你可以应用这样的东西
ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
if (activeNetwork != null) {
// connected to the internet
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
// connected to wifi
} else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
// connected to the mobile network
}
} else {
// not connected to the internet
}
另外,请在 AndroidManifest.xml 下添加此权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />