简体   繁体   中英

Get information of Location Services enabled/disabled using Fused Location Provider

I am working on an app which uses Google Maps and tracks user's location. I want to change the visibility of the "You are here!" marker when the user closes Location Services by hand or services goes to condition of inaccessible. This method can return the information of whether Location Services enabled or not:

private boolean isLocationServicesEnabled() {
    LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
    if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER) || lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        return true;
    }
    return false;
}

However, I do not want to detect the status of Location Services for once. Whenever the user opens or closes Location Services, I should detect it and change the visibility of marker. In short, this sloppy pseudocode explains what I want to listen:

while application runs
   if location services turn off
      change marker visibility to false
   else
      change marker visibility to true 

I did not find a way to achieve this task without android.location.LocationListener, want to achieve it just using Fused Location Provider. Do you have any idea? Here is the core structure I used if you want to see:

http://www.androidhive.info/2015/02/android-location-api-using-google-play-services/ (Check the title of "Complete Code")

Doing this the proper way involves a huge amount of code. You have to make use of the location SettingsApi class.

The main entry point for interacting with the location settings-enabler APIs.

This API makes it easy for an app to ensure that the device's system settings are properly configured for the app's location needs.

Fortunately there is a full blown sample provided by Google on github

How about implementing a gps listener?

mLocationManager.addGpsStatusListener(new GpsStatus.Listener(){

    @Override
    public void onGpsStatusChanged(int event) {
        if(event==GpsStatus.GPS_EVENT_STARTED){
            Log.d(TAG,"Gps Started");
        }else if(event==GpsStatus.GPS_EVENT_STOPPED){
            Log.d(TAG,"Gps Stopped");
        }
    }
});

modify the above code to change visibility of your marker. Hope this works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM