Я пытаюсь получить текущее местоположение пользователя, но на некоторых устройствах он работает, но на некоторых застрял в ожидании местоположения.Получить текущее местоположение пользователя
Вот мой код:
private final LocationListener mLocationListener = new LocationListener() {
@Override
public void onLocationChanged(final Location location) {
// TODO
Log.v("--", "get address 121");
Main.this.location = location;
getAddress();
locationManager.removeUpdates(mLocationListener);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Log.v("--", "provider enabled");
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Log.v("--", "provider disabled");
}
};
public void putExtra() {
Intent intent = new Intent(this, Settings.class);
intent.putExtra("timesobj", times);
startActivity(intent);
}
/**
* Get current/last known location and display city, country name and update
* calculations
* */
public void getAddress() {
Log.v("--", "get address 1");
boolean isGPSProviderEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean network_enabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Log.v("--", "get address 31 " + isGPSProviderEnabled + " gps - "
+ isConnectedToNetwork());
if (isGPSProviderEnabled || network_enabled) {
Log.v("--", "get address 2");
Criteria c = new Criteria();
Log.v("--", "provider " + locationManager.getBestProvider(c, true));
location = locationManager.getLastKnownLocation(locationManager
.getBestProvider(c, false));
if (location == null) {
Log.v("--", "get address 6");
locationManager.requestLocationUpdates(
locationManager.getBestProvider(c, false), 1000, 100,
mLocationListener);
} else {
Log.v("--", "get address 3");
if (isConnectedToNetwork()) {
new AsyncTask<Void, Void, Void>() {
protected Void doInBackground(Void... params) {
try {
com.myapp.utils.Geocoder geocoder = new com.myapp.utils.Geocoder(
Main.this);
GeocoderModel geocoderModel = geocoder
.getFromLocation(
location.getLatitude(),
location.getLongitude(), 5);
city = geocoderModel.getCity();
country = geocoderModel.getCountry();
prefs.edit().putString(Constants.CITY, city)
.apply();
Log.v("--", "get address 4");
} catch (IOException e) {
Log.v("--", "get address 11");
e.printStackTrace();
} catch (LimitExceededException e) {
Log.v("--", "get address 12");
e.printStackTrace();
}
return null;
};
protected void onPostExecute(Void result) {
prefs.edit().putString(Constants.COUNTRY, country)
.apply();
prefs.edit().putString(Constants.CITY, city)
.apply();
populateList(location);
};
}.execute();
} else {
city = prefs.getString(Constants.CITY,
getString(R.string.app_name));
Log.v("--", "get address 33 " + location.getLatitude());
populateList(location);
}
}
} else {
Log.v("--", "get address 5");
startGpsEnableDialog();
}
}
и вот журнала, что у меня есть:
V/-- (5498): get address 1
V/-- (5498): get address 31 true gps - true
V/-- (5498): get address 2
V/-- (5498): provider gp
V/-- (5498): get address 6
Может кто-то поможет мне, что может быть неправильно в этом коде, и скажите мне, как я могу правильно определить местоположение пользователя?
Спасибо!
вероятно эти устройства не поддерживаются Gps – koutuk
@koutuk я не разжижаю, проблемное устройство Nexus5 –
или это из-за недельным датчик GPS на этих устройствах, а также я хочу отметить, что GPS не работает в все ситуации, такие как внутренние позиции –