最近做一個監聽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; } }