Narendra Pal Narendra Pal - 28 days ago 4
Android Question

How to get notified when the gps in disabled by the user in android?

I am creating an app which uses GPS. For the first time in

onCreate()
I am checking whether the GPS is enabled or not. And if its not enabled then I am sending the user to the setting menu to set the GPS enabled.

Once the GPS is enabled then I starts executing my stuff. But if in between the user stops the GPS scrolling on Notification manager then how would I know this?

Because my method executed only once when the GPS is not enabled. After that its not getting executed when it is disabled again.

I just want to know whenever in between the work the GPS is disabled by the user. Just this information is required.


Here is my code.

class LocationFinderService extends Service{

boolean gps_enabled;
LocationManager locationManager;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
if(!isGpsEnabled())
{
startGpsForLocation();
stopSelf();
}

}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}

public boolean isGpsEnabled()
{
gps_enabled = (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)&& locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER));
Log.i("Log", "Gps: "+gps_enabled);
return gps_enabled;
}

public void startGpsForLocation()
{
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) ;
startActivity(intent);
Toast.makeText(this, "Please start the GPS", Toast.LENGTH_LONG).show();
}
}


I am calling this service on the button click of the Activity class.

Any help would be appreciated.

Answer
        lmGps.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,gpsLocationListener);
        lmGps.addGpsStatusListener(gpsStatusListener);

gpsLocationListner is

LocationListener gpsLocationListener=new LocationListener(){
        public void onLocationChanged(Location location){
            dGpsLat=location.getLatitude();
            dGpsLng=location.getLongitude();


        dLat=dGpsLat;
        dLng=dGpsLng;
        try{
        SaveLocation();
    }
        catch (Exception e) {
            sResponse=e.toString();
            return;
        }
    }
    public void onStatusChanged(String provider,int status,Bundle extras){}
    public void onProviderEnabled(String provider){}
    public void onProviderDisabled(String provider){}
};

gpsStatusListener

GpsStatus.Listener gpsStatusListener=new GpsStatus.Listener(){
        @Override
        public void onGpsStatusChanged(int event){
            if(event==GpsStatus.GPS_EVENT_SATELLITE_STATUS){
                try{
                    GpsStatus status=lmGps.getGpsStatus(null);
                    sats=status.getSatellites();
                    Iterator satI=sats.iterator();

                    int count=0;
                    while(satI.hasNext()){
                        GpsSatellite gpssatellite=(GpsSatellite) satI.next();
                        if(gpssatellite.usedInFix()){
                            count++;
                        }
                    }
                    iSalelliteCount=count;
                }
                catch(Exception ex){}
            }
        }
    };
Comments