我希望我的Android设备连接到wifi热点。
我创建了一个新的wificonfiguration
并将其添加到wifimanager
,此wificonfiguration
有NetworkId
。然后我调用函数wifi.enableNetwork(NetworkId, true).
之后,我认为请求者将通过获取IP地址,身份验证,并最终物理连接到热点。 那么有没有办法确定wifi是否物理连接?
我更喜欢类似处理程序的方法。
答案 0 :(得分:10)
你可以试试这个:
ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifi.isConnected()) {
// Your code here
}
编辑:更多详情:
在清单中注册BroadcastReceiver
,如下所示:
<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
}
}
答案 1 :(得分:6)
您可以检查所有网络。如果您只想要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" />
答案 2 :(得分:2)
这可能会对你有所帮助。
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;
}
答案 3 :(得分:0)
不推荐使用getNetworkInfo(int)方法。你可以申请这样的东西
function callbackReadFilesAndSend(res, req){
var data = ""
for(int i=0; i<numOfFiles;i++){
//read lines from files
data+=...//data from Files[i]
}
res.send(data)
}
另外,请在AndroidManifest.xml下添加此权限
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
}