访问位置服务时Android应用程序缺少状态栏图标

时间:2016-08-10 11:20:37

标签: android location-services android-gps

好的,这是一个奇怪的,但我可能会遗漏一些显而易见的事情。我目前正在使用此功能访问用户位置:

 public String[] getGeoCoords() {
    String[] geoCoords = new String[2];
    String mprovider = locationManager.getBestProvider(criteria, false);
    if (mprovider != null && !mprovider.equals("")) {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return geoCoords;
        }
        Location loc = locationManager.getLastKnownLocation(mprovider);
        if(loc== null)
            loc = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (loc == null)
            loc = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
        if (loc == null)
            loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        geoCoords[0] = String.valueOf(loc.getLatitude());
        geoCoords[1] = String.valueOf(loc.getLongitude());
    }
    return geoCoords;
}

这将按预期返回位置,但访问用户位置时通常获得的图标不会显示在状态栏中。

在我的清单中我有

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您正在访问getLastKnownLocation(),它会直接返回上一个可用位置,否则返回null。

当您请求像这样的位置更新时,您正在谈论的图标就会出现..

LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {

            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {

            }

            @Override
            public void onProviderEnabled(String provider) {

            }

            @Override
            public void onProviderDisabled(String provider) {

            }
        });

所以当它搜索卫星时,它会显示gps图标,当它找到一个onLocationChanged(Location location)时,将会调用捕获的位置。