0

There is any solution to check if user disable GPS in settings? I open my app, open top toolbar of android system, disable GPS and close this toolbar. In this moment I want to app check if status of GPS was changed. I use check if GPS is active in onResume(), but this solution works only when user enable GPS, when disable onResume() is not called. Any ideas?

Edit: This is may class:

public class PrivacyActivity extends BaseActivity  implements GpsStatus.Listener, LocationListener{


   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_privacy);

   }

   @Override
   protected void onPause() {
      super.onPause();
   }

   @Override
   protected void onResume() {
      super.onResume();
   }

   @Override
   public void onGpsStatusChanged(int event) {
      switch (event) {
         case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
            Toast.makeText(this, "ads", Toast.LENGTH_LONG).show();

            break;
         case GpsStatus.GPS_EVENT_FIRST_FIX:
            Toast.makeText(this, "ads1", Toast.LENGTH_LONG).show();

            break;
      }
   }

   @Override
   public void onLocationChanged(Location location) {

   }

   @Override
   public void onStatusChanged(String provider, int status, Bundle extras) {
      Toast.makeText(getApplicationContext(), "ads2", Toast.LENGTH_LONG).show();
   }

   @Override
   public void onProviderEnabled(String provider) {
      Toast.makeText(getApplicationContext(), "ads2", Toast.LENGTH_LONG).show();
   }

   @Override
   public void onProviderDisabled(String provider) {
      Toast.makeText(getApplicationContext(), "ads2", Toast.LENGTH_LONG).show();
   }
}

and when I disable gps I didn't see toast.

edi233
  • 3,511
  • 13
  • 56
  • 97
  • Possible duplicate of [How can I check the current status of the GPS receiver?](http://stackoverflow.com/questions/2021176/how-can-i-check-the-current-status-of-the-gps-receiver) – Doug Stevenson Mar 29 '16 at 19:56

2 Answers2

2

You can use the LocationListener class

// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
      // Called when a new location is found by the network location provider.
      makeUseOfNewLocation(location);
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {}

    public void onProviderEnabled(String provider) {
        Log.i("Example", "GPS is ON");
    }

    public void onProviderDisabled(String provider) {
        Log.i("Example", "GPS is OFF");
    }
  };

// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

You can get more info in http://developer.android.com/guide/topics/location/strategies.html

chihau
  • 363
  • 4
  • 7
0

Reference the locationListener first.

LocationListener locationListener;

locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            LatLng myPlace = new LatLng(location.getLatitude(), location.getLongitude());
            mMap.addMarker(new MarkerOptions().position(myPlace).title("me"));
            mMap.moveCamera(CameraUpdateFactory.newLatLng(myPlace));
            mMap.animateCamera(CameraUpdateFactory.zoomTo(8.0f));
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);
        }
    };

onProviderDisabled method will be fired if the user has disabled GPS from settings. The intent inside the method will take the user to location settings directly.

Tested and works fine.

Aamir Ali
  • 203
  • 1
  • 8