Google Location Services Vs Android Location Services

后端 未结 2 1200
温柔的废话
温柔的废话 2021-01-05 04:37

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 Lo

相关标签:
2条回答
  • 2021-01-05 04:58

    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;
        }
    
    0 讨论(0)
  • 2021-01-05 05:00

    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.

    0 讨论(0)
提交回复
热议问题