下面列出了android.net.wifi.WifiManager#isWifiEnabled ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static String getPhoneIp(Context application) {
WifiManager wifiManager = (WifiManager) application.getSystemService("wifi");
if (wifiManager.isWifiEnabled()) {
return intToIp(wifiManager.getConnectionInfo().getIpAddress());
}
try {
Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
while (en.hasMoreElements()) {
Enumeration<InetAddress> enumIpAddr = ((NetworkInterface) en.nextElement()).getInetAddresses();
while (enumIpAddr.hasMoreElements()) {
InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return null;
}
protected static void prepareInterfaces(WifiManager wifiManager, TelephonyManager telephonyManager)
{
// If wifi is On, we need to turn it off first
if(wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(false);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(ki4aService.preferences.getBoolean("cellular_switch",true) &&
ki4aService.preferences.getBoolean("airplane_switch",true))
{
// If data connection is not connected, we'll try to bring it up
if (telephonyManager.getDataState() != telephonyManager.DATA_CONNECTED &&
telephonyManager.getDataState() != telephonyManager.DATA_CONNECTING)
Util.refreshMobileData();
}
}
/**
* 打开或关闭WIFI
*
* @param mContext Context
* @param action 打开使用on 关闭使用off
*/
public static void onWifi(Context mContext, String action) {
WifiManager wm = ((WifiManager) mContext
.getSystemService(Context.WIFI_SERVICE));
if (action.toLowerCase().equalsIgnoreCase("on")) {
if (!wm.isWifiEnabled()) {
wm.setWifiEnabled(true);
}
}
if (action.toLowerCase().equalsIgnoreCase("off")) {
if (wm.isWifiEnabled()) {
wm.setWifiEnabled(false);
}
}
}
/**
* Start connecting to specific wifi network
*/
protected void handleWIFI() {
WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
if (!wifi.isWifiEnabled()) {
showWifiDisabledDialog();
}
else {
if (!permisionLocationOn()) {
setLocationPermission();
} else {
if (checkLocationTurnOn()) {
connectToSpecificNetwork();
}
}
}
}
/**
* get wifi networks
*/
String getWifi() {
final String redes = "";
try {
String connectivity_context = Context.WIFI_SERVICE;
final WifiManager wifi = (WifiManager) getSystemService(connectivity_context);
if (wifi.isWifiEnabled()) {
wifi.startScan();
}
else {
Util.wifiApagado = true;
wifi.setWifiEnabled(true);
wifi.startScan();
}
} catch(Exception e) {
Util.logDebug("Exception (getWifi): " + e.getMessage());
}
return redes;
}
@Override
public void onCreate() {
// The service is being created
Log.d(TAG, "creating new scan service");
queue = Volley.newRequestQueue(this);
// setup wifi
wifi = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
if (wifi.isWifiEnabled() == false) {
wifi.setWifiEnabled(true);
}
// register wifi intent filter
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
registerReceiver(mWifiScanReceiver, intentFilter);
try {
// setup bluetooth
Log.d(TAG, "setting up bluetooth");
if (receiver == null) {
receiver = new BluetoothBroadcastReceiver();
registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
}
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
private boolean checkWifiOnAndConnected() {
WifiManager wifiMgr = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
if (wifiMgr == null) {
return false;
}
if (wifiMgr.isWifiEnabled()) { // Wi-Fi adapter is ON
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
return wifiInfo.getNetworkId() != -1;
}
else {
return false; // Wi-Fi adapter is OFF
}
}
public static boolean checkWifiState(Context context)
{
try
{
WifiManager mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
if (!mWifiManager.isWifiEnabled() || wifiInfo.getSSID() == null)
{
return false;
}
return true;
}
catch (Exception e)
{
return false;
}
}
/**
* Checks if WiFi is turned on<br>
* Requires ACCESS_NETWORK_SATE, READ_PHONE_STATE permissions<br>
*
* @return true if WiFi is turned on; false otherwise
*/
public boolean isWifiEnabled() {
WifiManager wm = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
if (wm == null) {
return false;
}
return wm.isWifiEnabled();
}
/**
* Disable wifi
*
* @param context
* @return true if the operation succeeds (or if the existing state is the same as the requested state).
*/
@RequiresPermission(Manifest.permission.CHANGE_WIFI_STATE)
public static boolean disableWifi(Context context) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if (wifiManager.isWifiEnabled()) {
return wifiManager.setWifiEnabled(false);
}
return true;
}
/**
* 获取IP地址(网络为Wifi)
*/
@RequiresPermission(allOf = {Manifest.permission.CHANGE_WIFI_STATE,
Manifest.permission.ACCESS_WIFI_STATE})
public static String getWifiIp(@NonNull Context context) {
// 获取wifi服务
WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
// 判断wifi是否开启
if (null != wifiManager && !wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(true);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
return intToIp(ipAddress);
}
return null;
}
private void processCurrentWifiState(Context context) {
WifiManager wifi = (WifiManager) context.getSystemService(WIFI_SERVICE);
if (wifiEnabled != wifi.isWifiEnabled()) {
wifiEnabled = wifi.isWifiEnabled();
ToastUtils.longToast("Wifi " + (wifiEnabled ? "enabled" : "disabled"));
}
}
public static String getLocalIpAddress(Context context) {
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService("connectivity");
NetworkInfo wifi = connMgr.getNetworkInfo(1);
NetworkInfo mobile = connMgr.getNetworkInfo(0);
if (wifi.isAvailable()) {
WifiManager wifimanage = (WifiManager) context.getSystemService("wifi");
wifimanage.isWifiEnabled();
int i = wifimanage.getConnectionInfo().getIpAddress();
return (i & 255) + "." + ((i >> 8) & 255) + "." + ((i >> 16) & 255) + "." + ((i >> 24) & 255);
}
if (mobile.isAvailable()) {
try {
Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
while (en.hasMoreElements()) {
Enumeration<InetAddress> enumIpAddr = ((NetworkInterface) en.nextElement()).getInetAddresses();
while (enumIpAddr.hasMoreElements()) {
InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
}
return null;
}
/**
* Current Wifi hardware state. Needs correct permission to work.
*
* @param context
* @return state
*/
//@RequiresPermission(Manifest.permission.ACCESS_WIFI_STATE)
public static Status getWifiStatus(Context context) {
WifiManager wifi = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if (wifi == null) {
return Status.UNSUPPORTED;
} else if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_WIFI_STATE) != PackageManager.PERMISSION_GRANTED) {
return Status.NEEDS_PERMISSION;
} else if (wifi.isWifiEnabled()) {
return Status.ENABLED;
} else {
return Status.DISABLED;
}
}
private boolean scanWifiNetworks(){
final boolean isAirplaneEnabled = Settings.System.getInt(getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0) == 1;
final WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
if((isAirplaneEnabled && wifiManager.isWifiEnabled()) || wifiManager.isWifiEnabled()) {
wifiReciever = new WifiScanReceiver();
registerReceiver(wifiReciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
showWifiProgressbar();
this.wifiManager.startScan();
return true;
} else return false;
}
public static boolean isWifiEnabled () {
final WifiManager wifiManager = getWiFiManager(ArcusApplication.getArcusApplication());
return wifiManager.isWifiEnabled();
}
public static boolean isWifiEnabled(Context c){
WifiManager wifi = (WifiManager) c.getSystemService(Context.WIFI_SERVICE);
return wifi.isWifiEnabled();
}
@Override
public void onReceive(Context context, Intent intent) {
int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
WifiManager.WIFI_STATE_UNKNOWN);
String wifiStateText = "No State";
String action = intent.getAction();
if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
WifiManager manager = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
NetworkInfo networkInfo = intent
.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
NetworkInfo.State state = networkInfo.getState();
if (state == NetworkInfo.State.DISCONNECTED) {
if (manager.isWifiEnabled()) {
doUpdate(context);
wifiStateText = "WIFI_STATE_DISCONNECTED";
}
}
}
switch (wifiState) {
case WifiManager.WIFI_STATE_DISABLING:
wifiStateText = "WIFI_STATE_DISABLING";
break;
case WifiManager.WIFI_STATE_DISABLED:
wifiStateText = "WIFI_STATE_DISABLED";
break;
case WifiManager.WIFI_STATE_ENABLING:
wifiStateText = "WIFI_STATE_ENABLING";
break;
case WifiManager.WIFI_STATE_ENABLED:
wifiStateText = "WIFI_STATE_ENABLED";
if (state == 1) state--;
else
doUpdate(context);
break;
case WifiManager.WIFI_STATE_UNKNOWN:
wifiStateText = "WIFI_STATE_UNKNOWN";
break;
default:
break;
}
System.out.println("WIFI_recv: " + wifiStateText);
}
/**
* 打开或关闭wifi
* <p>
* 需添加权限
* {@code <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>}
* </p>
*
* @param context
* 上下文
* @param enabled
* {@code true}: 打开<br>
* {@code false}: 关闭
*/
public static void setWifiEnabled(Context context, boolean enabled) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if (enabled) {
if (!wifiManager.isWifiEnabled()) {
// wifiManager.setWifiEnabled(true);
}
} else {
if (wifiManager.isWifiEnabled()) {
// wifiManager.setWifiEnabled(false);
}
}
}
/**
* 判断wifi是否打开
* <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>}</p>
*
* @return {@code true}: 是<br>{@code false}: 否
*/
public static boolean getWifiEnabled() {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
return wifiManager.isWifiEnabled();
}