在Marshmallow上使用LocationListener for API 21

时间:2017-01-05 15:59:30

标签: android android-6.0-marshmallow locationmanager locationlistener

我有一个应用程序,可以收听去年使用API​​ 21实现的GPS更新。它已经在运行Lollipop的几款Android手机上进行了测试,例如LG G4。现在我将它安装在带有Marshmallow的LG G5上,我没有收到GPS更新,即从未调用过onLocationChanged()

授予权限(手机设置上的应用视图显示位置权限),以下语句为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,但没有成功。

我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

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

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