【问题标题】:Using LocationListener for API 21 on Marshmallow在 Marshmallow 上使用 API 21 的 LocationListener
【发布时间】: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


【解决方案1】:

我意识到 GPS 传感器没有为手机提供任何更新。例如,当我将位置精度限制为仅 GPS 传感器时,谷歌地图永远无法获得位置修复。

如果我将手机连接到互联网,那么NETWORK_PROVIDER 会提供位置更新。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-08
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多