android 中wifi的应用
加入权限
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
主要代码
//打开状态
wifiManager=(WifiManager)ActivityMain.this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
System.out.println("wifi state-->"+wifiManager.getWifiState());
Toast.makeText(ActivityMain.this, "当前wifi网卡状态为"+wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();
}
}
//关闭状态
wifiManager=(WifiManager)ActivityMain.this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);
System.out.println("wifi state-->"+wifiManager.getWifiState());
Toast.makeText(ActivityMain.this, "当前wifi网卡状态为"+wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();
}
//检查状态
wifiManager=(WifiManager)ActivityMain.this.getSystemService(Context.WIFI_SERVICE);
//wifiManager.setWifiEnabled(false);
System.out.println("wifi state-->"+wifiManager.getWifiState());
Toast.makeText(ActivityMain.this, "当前wifi网卡状态为"+wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();
}
}
}
相关连接http://lpqsun-126-com.iteye.com/blog/1087847
http://zkl-1987.iteye.com/blog/1054913