Java 无法在某些设备上检索最后一个已知位置和当前位置
我们已经推出了一款利用GPS信息的Android应用程序,我们的少数用户报告说GPS不适用于他们。虽然我们已经投入了大量时间来改进代码库,但我们仍然无法为受影响的用户提供相关的改进。我们现在正在寻求社区的新想法,以向前迈进一步并解决这一问题。提前感谢您的反馈 方法 在我们的Android应用程序中,我们通过三个连续步骤识别用户位置:Java 无法在某些设备上检索最后一个已知位置和当前位置,java,android,gps,android-gps,Java,Android,Gps,Android Gps,我们已经推出了一款利用GPS信息的Android应用程序,我们的少数用户报告说GPS不适用于他们。虽然我们已经投入了大量时间来改进代码库,但我们仍然无法为受影响的用户提供相关的改进。我们现在正在寻求社区的新想法,以向前迈进一步并解决这一问题。提前感谢您的反馈 方法 在我们的Android应用程序中,我们通过三个连续步骤识别用户位置: 当前GPS位置(GPS\U提供商) 当前网络位置(网络提供商) 最后已知的GPS位置(GPS\U提供商) 如果一个步骤失败,则尝试下一个步骤。如果所有步骤均未成功,
- 确保设备上已启用GPS
- 确保向应用程序授予GPS权限(细粒度位置)
- 关闭应用程序,获取谷歌地图上的当前位置,然后重新启动应用程序
- 华为P8
- Oppo CPH1723
- 银河注4
- 是不是用户只是离线导致了这个问题?不,这应该通过谷歌地图测试和利用最后一个已知位置来排除
- 可能是设备设置造成的吗?没有,这已经被谷歌地图测试排除了
- 难道应用程序权限是罪魁祸首吗?没有,这已向用户澄清
- 可能是我们的代码有缺陷吗?也许,但我们无法识别任何明显的缺陷
- 你有没有试过其他的供应商?是的,我们也尝试了更新的融合位置提供者API,同样的问题也发生了
有什么想法吗?谢谢你的帮助 lastnownlocation几乎总是失败。只有当另一个应用程序(或您自己的应用程序)最近打开了位置服务,并且缓存了该值时,该功能才起作用。手机不会一直主动跟踪位置。99%的情况下,当您调用它时,将返回null。因此,如果1和2失败,3成功的可能性为零。我不知道确切原因。请检查以下几点
- 您是否遵循Android 8的GPS请求指南
- 电池节约模式是否已关闭
- 目标设备是否使用当前的google支持库进行了更新
另外,在一些设备中,由于位置数据的前一次或两次为空,我遇到了这种情况,因此您请求位置更新的次数也会增加。您能发布您所说的GPS错误消息的详细信息吗?嘿,这是我们向用户显示的错误。代码中显示了引发此错误的点,请参阅代码中的以下注释:/*!!!这就是我们碰壁并向用户返回GPS错误消息的地方*/
public class GeoRepository {
private static final String TAG = GeoRepository.class.getSimpleName();
public GeoRepository() {
}
public class GeoNoPermissionException extends RuntimeException {
public GeoNoPermissionException(String message, Throwable cause) {
super(message, cause);
}
}
public class GeoNoLocationException extends RuntimeException {
public GeoNoLocationException(String message, Throwable cause) {
super(message, cause);
}
}
public interface GeoLocationListener {
void onCompleted(Location location);
void onFailed(RuntimeException ex);
}
public void getCurrentLocation(Context context, GeoLocationListener listener) {
// The permissions for this is already being requested in the SplashActivity
boolean hasPermission = !(ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED);
if (!hasPermission) {
listener.onFailed(new GeoNoPermissionException("Missing permissions", null));
return;
}
// try initially with precise GPS provider
requestLocationOnce(context, LocationManager.GPS_PROVIDER, new GeoLocationListener() {
@Override
public void onCompleted(Location location) {
listener.onCompleted(location);
}
@Override
public void onFailed(RuntimeException ex) {
Log.e(TAG, "Could not find location on first attempt", ex);
requestLocationOnce(context, LocationManager.NETWORK_PROVIDER, new GeoLocationListener() {
@Override
public void onCompleted(Location location) {
listener.onCompleted(location);
}
@Override
public void onFailed(RuntimeException ex) {
Log.e(TAG, "Could not find location on second attempt", ex);
Location location = null;
try {
location = requestLastKnownLocation(context, LocationManager.GPS_PROVIDER);
} catch(RuntimeException ex2) {
Log.e(TAG, "Could not find location on third attempt", ex2);
/* !!! this is where we hit the wall and return a GPS-errormessage to the user !!! */
listener.onFailed(ex);
return;
}
if(location == null) {
Log.e(TAG, "Could not find location on third attempt, location is null");
listener.onFailed(ex);
return;
}
listener.onCompleted(location);
}
});
}
});
}
private void requestLocationOnce(Context context, String provider, GeoLocationListener listener) {
// The permissions for this is already being requested in the SplashActivity
boolean hasPermission = !(ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED);
if (!hasPermission) {
listener.onFailed(new GeoNoPermissionException("Missing permissions", null));
return;
}
Object syncObj = new Object();
CountDownLatch completedSignal = new CountDownLatch(1);
LocationManager locationManager = (LocationManager) FlourishApp.getContext().getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// this method may never get called hence we need a watchdog later on
// this happens for example if GPS permissions are there but GPS is turned off
synchronized(syncObj) {
if (completedSignal.getCount() > 0) {
completedSignal.countDown();
listener.onCompleted(location);
}
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
locationManager.requestSingleUpdate(
provider,
locationListener,
Looper.getMainLooper());
new Handler(Looper.getMainLooper()).postDelayed(() -> {
// cleanup event subscription
locationManager.removeUpdates(locationListener);
// watchdog to raise error if we don't get a callback within reasonable time
boolean receivedLocationUpdate = true;
synchronized (syncObj) {
if (completedSignal.getCount() > 0) {
completedSignal.countDown();
receivedLocationUpdate = false;
}
}
if(!receivedLocationUpdate) {
GeoNoLocationException ex = new GeoNoLocationException("No location could be determined", null);
listener.onFailed(ex);
}
}, Constants.LOCATION_MAX_WAIT); // LOCATION_MAX_WAIT = 50000
}
private Location requestLastKnownLocation(Context context, String provider) {
// The permissions for this is already being requested in the SplashActivity
boolean hasPermission = !(ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED);
if (!hasPermission) {
throw new GeoNoPermissionException("Missing permissions", null);
}
LocationManager locationManager = (LocationManager) FlourishApp.getContext().getSystemService(Context.LOCATION_SERVICE);
return locationManager.getLastKnownLocation(provider);
}
public interface GetCountryCodeByCurrentLocationListener {
void onCompleted(String countryCode);
void onFailed(RuntimeException ex);
}
}