【发布时间】:2023-03-03 08:27:23
【问题描述】:
我正在尝试获取当前用户位置,但在某些设备上它可以工作,但在某些设备上我卡在等待位置。
这是我的代码:
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 我不认为,有问题的设备是 Nexus5
-
-
locationManager.getBestProvider(c, false)-> 你收到了一个禁用的提供者。
标签: java android android-location