Home » Android » Google Location Services Vs Android Location Services

Google Location Services Vs Android Location Services

Posted by: admin June 15, 2020 Leave a comment

Questions:

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 .

How to&Answers:

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

Answer:

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.