【发布时间】:2017-01-09 23:49:38
【问题描述】:
我有一个应用程序可以侦听 GPS 更新,该应用程序是去年使用 API 21 实现的。它在运行 Lollipop 的几款 Android 手机上进行了测试,例如 LG G4。现在我将它安装在带有 Marshmallow 的 LG G5 上,但我没有收到 GPS 更新,即从未调用过 onLocationChanged()。
权限被授予(手机设置的App视图显示Location权限),以下语句为true
checkCallingOrSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
AndroidManifest.xml 包括以下几行:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.location.gps" />
我使用的代码如下:
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener = getLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
private LocationListener getLocationListener() {
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
handleLocationChange();
}
public void onStatusChanged(String provider, int status, Bundle extras) { }
public void onProviderEnabled(String provider) { }
public void onProviderDisabled(String provider) { }
};
return locationListener;
}
我也试过NETWORK_PROVIDER,没有成功。
我在这里错过了什么?
【问题讨论】:
-
检查您的应用是否有权访问位置的权限?因为可能是设备阻止了访问。
-
权限被授予(手机设置的App视图显示Location权限),以下语句为
truecheckCallingOrSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PE
标签: android android-6.0-marshmallow locationmanager locationlistener