問題描述
擁有了ACCESS_WIFI_STATE、ACCESS_NETWORK_STATE、ACCESS_FINE_LOCATION等權限後,獲取當前鏈接Wifi的Ssid依然返“unknown ssid”
手動解決
檢查手機是否打開了位置信息,即手機是否允許定位。一般在下拉菜單欄中有個"位置信息"開關。
代碼判斷
可以使用LocationManager判斷是否開啓了網絡定位。
/**
* 判斷是否打開網絡定位
* @param context 用於獲取LocationManager
* @return true/false
*/
public static boolean isOpenGps(Context context){
LocationManager locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
if (locationManager != null){
//這裏只判斷了是否開啓網絡定位
//還有GPS定位 LocationManager.GPS_PROVIDER
return locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}else {
return false;
}
}
獲取網絡信息的工具類
最後附上最近寫的獲取網絡信息的一個工具類,需要在Application中調用init()。
工具類的GitHub地址