8

As we know we have two alternates for making our applicaiton location aware Either we use Google’s Location Services API (part of Google Play services) or we use Androids Location API .

The problem I have here is that using the Google's services , we have to use interface com.google.android.gms.location.LocationListener that provides us with location updates. However unlike the android.location.LocationListener, the google's version doesnt' provides us the state of the location provider (gps or internet).

we have the following methods in the Androids Location Listener

abstract void   onLocationChanged(Location location)
Called when the location has changed.
abstract void   onProviderDisabled(String provider)
Called when the provider is disabled by the user.
abstract void   onProviderEnabled(String provider)
Called when the provider is enabled by the user.
abstract void   onStatusChanged(String provider, int status, Bundle extras)
Called when the provider status changes

.

However in google we have just one

abstract void   onLocationChanged(Location location)

How would I identify the changes in the provider if I am using the google services ? . For example in midst of the application usage , the user decides the shut off the GPS , I wanted to show a toast on this event.

I am in the dark how should I acheive that .

Muhammad Ahmed AbuTalib
  • 4,080
  • 4
  • 36
  • 59
  • https://developer.android.com/google/play-services/location.html http://developer.android.com/training/location/receive-location-updates.html Check This – Ando Masahashi Aug 06 '14 at 09:59

2 Answers2

8

You register a receiver to handle the actiion Providers_changed.

private static final String ACTION_GPS = "android.location.PROVIDERS_CHANGED";
private BroadcastReceiver yourReceiver;

private void checkGPS() {
    Context context = this.getApplicationContext();
    if (context != null) {
        Button btnGPS = (Button) findViewById(R.id.btnGPS);
        if (btnGPS != null) {
            LocationManager locationManager = (LocationManager) context
                    .getSystemService(Context.LOCATION_SERVICE);
            boolean b = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);
            locationManager = null;
            if (b) {
                if (btnGPS.getVisibility() != View.GONE) {
                    btnGPS.setVisibility(View.GONE);
                }
            } else {
                if (btnGPS.getVisibility() != View.VISIBLE) {
                    btnGPS.setVisibility(View.VISIBLE);
                }
            }
        }
    }
}

private void registerReceiverGPS() {
    if (yourReceiver == null) {
        // INTENT FILTER FOR GPS MONITORING
        final IntentFilter theFilter = new IntentFilter();
        theFilter.addAction(ACTION_GPS);
        yourReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent != null) {
                    String s = intent.getAction();
                    if (s != null) {
                        if (s.equals(ACTION_GPS)) {
                            checkGPS();
                        }
                    }
                }
            }
        };
        registerReceiver(yourReceiver, theFilter);
    }
}


 @Override
 public void onResume() {
 super.onResume();
 checkGPS();
 registerReceiverGPS();



    @Override
public void onStop() {
    super.onStop();
    // Do not forget to unregister the receiver!!!
    if (yourReceiver != null) {
        unregisterReceiver(yourReceiver);
        yourReceiver = null;
    }
danny117
  • 5,581
  • 1
  • 26
  • 35
4

the google's version doesnt' provides us the state of the location provider (gps or internet)

In part, that is because it is not using any single location provider.

How would I identify the changes in the provider if I am using the google services ?

You could try listening for PROVIDERS_CHANGED_ACTION broadcasts.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • So can you suggest any mechanism through which I can inform the user that he has forgotten to turn on his Locations sensors from the settings ? . I dont suppose this is too much to ask for . This might happen a lot of times. The user would be waiting for the locations to update ill known of the fact that he has forgotten to turn on the adapters. – Muhammad Ahmed AbuTalib Aug 06 '14 at 11:29
  • 2
    @MuhammadAhmedAbuTalib: "So can you suggest any mechanism through which I can inform the user that he has forgotten to turn on his Locations sensors from the settings ?" -- use `LocationManager` and `isProviderEnabled()`. Just because you get your locations via `LocationClient` does not mean that you are blocked from using `LocationManager` for other things. And, just because `LocationClient` provides locations does not mean that `LocationClient` has to completely duplicate the rest of the `LocationManager` API. – CommonsWare Aug 06 '14 at 11:37