在我的應用程序中,我嘗試使用networkProvider搜索android設備的粗略位置。我只在我的位置管理器中使用networkProvider,但如果我不打開GPS傳感器,它將不起作用。Android GPS網絡提供者不工作
無論GPS傳感器開啓還是關閉,網絡提供者是否應該給出一個粗略的位置?
這是我的代碼。
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
if(isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
Log.e("Network","suc");
else
Log.e("Network", "fail");
if (!isNetworkEnabled) {
} else {
this.isGetLocation = true;
if (isNetworkEnabled) {
Log.e("GpsInfo", "isNetworkEnabled true");
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null) {
Log.e("GpsInfoClass", "Location manager not NULL");
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
lat = location.getLatitude();
lon = location.getLongitude();
Log.e("GpsInfoClass", lon + ", " + lat);
}else{
Log.e("GpsInfoClass", "Location NULL");
}
}else{
Log.e("GpsInfo", "Location Manager is null");
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
在我的日誌中,它打印網絡(標記)失敗(日誌)。
這是什麼意思是說,
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
總是返回false,如果GPS傳感器處於關閉狀態。這是爲什麼發生?我錯過了如何獲取粗略位置?