【发布时间】:2017-10-15 19:09:24
【问题描述】:
private void getLocation(){
locationManager = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
List<String> providers = locationManager.getAllProviders();
for (String provider : providers) {
Log.e("GPS_provider: ", provider);
}
Criteria criteria = new Criteria();
bestProvider = locationManager.getBestProvider(criteria, false);
Log.e("Best_provider: ", bestProvider);
locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
Log.e("Loc_changed", ""+ location.getLatitude() + location.getLongitude());
mLocation = location;
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {
Log.e("Provider_enabled:", provider);
}
public void onProviderDisabled(String provider) {
Log.e("Provider_disabled:", provider);
}
};
// Register the listener with the Location Manager to receive location updates
try {
locationManager.requestLocationUpdates(bestProvider, 0, 0, locationListener);
mLocation = locationManager.getLastKnownLocation(bestProvider);
if (mLocation == null){
mLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Log.e("GPS network", "true");
}
} catch (SecurityException e){
Log.e("GPS problem", "GPS problem "+e.getMessage());
}
}
我正在尝试在执行 HTTP GET 检索我的记录之前读取位置:
private void loadBusinesses(){
gpsMsg.setVisibility(View.INVISIBLE);
getLocation();
currentView = GEOVIEW;
businessesList.nextUrl = "null";
if (!businessesList.isEmpty()){
Log.e("businessList ","not empty");
businessesList.clear();
notifyAdapter();
}
Double latitude;
Double longitude;
try{
latitude = mLocation.getLatitude();
longitude = mLocation.getLongitude();
} catch (NullPointerException e){
Log.e("GPS", "Location unavailable");
gpsMsg.setVisibility(View.VISIBLE);
swipeContainer.setRefreshing(false);
return;
}
}
即使我检查 GPS 是否打开,当我尝试使用 GPS 位置时,我总是会得到空值,然后它会从 NETWORK 获取位置。 出于这个原因,我尝试将 GPS 设置设置为“仅 GPS”,但我得到 NULL,所以没有位置。 我阅读了所有其他帖子,但我一直在 GPS 上取零。我正在使用 USB Debug 在真实设备上进行仿真。
有什么想法吗?
【问题讨论】:
标签: java android android-asynctask android-location android-gps