问题描述
刚收到运行棉花糖(6.0.1)的Galaxy S7(Edge),发现我的应用程序使用 android.permission.ACCESS_COARSE_LOCATION
并针对Sdk版本22(Lollipop)出现问题.当我调用 locationManager.getLastKnownLocation()
时,它总是返回NULL.这是我正在运行的内容:
Just received a Galaxy S7 (Edge) running Marshmallow (6.0.1) and find that it has an issue with my app that uses android.permission.ACCESS_COARSE_LOCATION
and targets Sdk Version 22 (Lollipop). When I call locationManager.getLastKnownLocation()
it always returns NULL. Here's what I'm running:
public Location getLastKnownLocationObject(Context context) {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
try {
if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
// Location is always null on S7 (only)
Log.i(TAG, ">>> getLastKnownLocationObject() getLastKnownLocation: " + location);
return location;
}
} catch (Exception e) {
Log.e(TAG, ">>> getLastKnownLocationObject() exception", e);
}
return null;
}
相同的代码在我尝试过的所有其他设备上都可以正常运行:Galaxy S5(5.0.1),Nexus 7(5.0.1),Nexus 9(6.0.1)和Samsung Tab3(4.4.2)
This same code works fine on every other device I've tried: Galaxy S5 (5.0.1), Nexus 7 (5.0.1), Nexus 9 (6.0.1), and Samsung Tab3 (4.4.2)
请注意,如果我将清单更改为使用ACCESS_FINE_LOCATION,则以上代码在每个设备上都可以正常工作.不幸的是,管理层目前不允许我更改权限.
Note that if I change the manifest to use ACCESS_FINE_LOCATION, the above code works fine on every device. Unfortunately, management won't allow me to change permissions at this time.
在SO上看到的一些答案中,建议在执行getLastKnownLocation()之前先调用以下命令,但这没有任何效果.
In a few answers I've seen here on SO, it's suggested to call the following prior to doing the getLastKnownLocation(), but that didn't have any effect.
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(final Location location) {
// NEVER CALLED on Samsung S7 for LocationManager.NETWORK_PROVIDER
}
});
因此,目前,我觉得这是新三星Galaxy S7独有的问题.
So at the moment, I feel this is an issue exclusive to the new Samsung Galaxy S7.
推荐答案
根据我对Samsung设备的经验.如果重新启动电话,但您未启用GPS,则不会获得位置信息.如果您启用了GPS(例如通过运行Google Maps),那么您将获得一个位置.三星不会为GPS,网络或无源提供商返回位置,除非在设备加电周期中找到了位置.
From my experience with Samsung devices. If you reboot the phone and you DON'T enable GPS you will not get a location. If you enable GPS as some point - maybe by running Google Maps, then you will get a location. Samsung does not return a location for GPS, Network or Passive providers unless it has gotten a location during the devices power on cycle.
我在代码中进行的任何调用都无法使其启动并激活并获得位置.他们可以在其他设备上正常工作.三星似乎也不会很长时间地缓存此信息.
None of the calls I have made in code ever get it to kick in and activate and get a location. They work fine on other devices. Samsung does not seem to cache this information for very long either.
这篇关于LocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)在Galaxy S7上始终返回NULL(仅)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!