我需要获取当前位置信息。目前,我正在使用获取最后的已知位置 https://developer.android.com/reference/android/location/LocationManager#getLastKnownLocation(java.lang.String)但有时它可能会返回空或过时的信息。我正在尝试使用请求单次更新 https://developer.android.com/reference/android/location/LocationManager#requestSingleUpdate(java.lang.String,%20android.app.PendingIntent)但它在 API 级别 30 中已被弃用,它要求我使用获取当前位置 https://developer.android.com/reference/android/location/LocationManager#getCurrentLocation(java.lang.String,%20android.os.CancellationSignal,%20java.util.concurrent.Executor,%20java.util.function.Consumer%3Candroid.location.Location%3E)反而。我怎样才能实现这个目标?
这是我当前的代码供参考。
LocationManager locationManager = (LocationManager) ApplicationProvider.getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
float accuracy = location.getAccuracy();
double altitude = location.getAltitude();
double latitude = location.getLatitude();
double longitude = location.getLongitude();
float speed = location.getSpeed();
我已经解决了这是代码。
locationManager.getCurrentLocation(
LocationManager.GPS_PROVIDER,
null,
application.getMainExecutor(),
new Consumer<Location>() {
@Override
public void accept(Location location) {
// code
}
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)