最近做一个监听wifi开启、关闭、连接监听的项目
手机设置中连接成功后 在代码中监听不到连接的哪一个Wi-Fi
String ssid = connectedWifiInfo.getSSID(); int networkId = connectedWifiInfo.getNetworkId(); wifi连接上了 ssid==<unknown ssid>,networkId==0
1、手机定位服务(VIVO) 、位置信息(小米) 没有开启的时候,开启wifi 连接成功之后,在广播中也监听不到;
2、开启过程:
2-1 首先判断用户是否打开系统定位服务
/*判断用户是否打开系统定位服务*/
public boolean isLocationEnabled() {
int locationMode = 0;
String locationProviders;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
try {
locationMode = Settings.Secure.getInt(getContentResolver(), Settings.Secure.LOCATION_MODE);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
return false;
}
return locationMode != Settings.Secure.LOCATION_MODE_OFF;
} else {
locationProviders = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
return !TextUtils.isEmpty(locationProviders);
}
}
2-2 弹出对话框提醒;
2-3 开启
private void startSettingLocationService() { Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); //可以在onActivityResult中根据该请求码判断用户是否已经在设置页面打开位置服务 startActivityForResult(intent, START_ACTIVITY_FOR_START_LOCATION_SERVICE); }
2-4 定位服务开启与否提示
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case START_ACTIVITY_FOR_START_LOCATION_SERVICE:
Log.e(TAG, "onActivityResult: data==" + data);
//TODO
if (isLocationEnabled()) {
Toast.makeText(instance, "定位服务开启成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(instance, "定位服务开启失败", Toast.LENGTH_SHORT).show();
}
break;
}
}