Search code examples
androidlocation-servicesandroid-gps

Android app missing status bar icon when accessing location services


Ok this is a weird one but I maybe missing something that is obvious. I am currently accessing the user location using this function:

 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;
}

This returns the location as expected but the icon that you normally get when accessing the user location does not appear in the status bar.

In my Manifest I have

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

Any ideas?


Solution

  • you are accessing getLastKnownLocation() which returns last available location directly else it returns null.

    The icon your are talking about comes when you request for location update like this..

    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) {
    
                }
            });
    

    so while it searches for satellites, it shows that gps icon and when it found one onLocationChanged(Location location) will be called with captured location.