Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

gps

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.

like image 644
Narendra Pal Avatar asked Nov 23 '12 06:11

Narendra Pal


2 Answers

        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){}
            }
        }
    };
like image 128
raghav chopra Avatar answered Oct 11 '22 01:10

raghav chopra


You just need to Create one BroadcastReceiver

public class InternetConnectionBroadcast extends BroadcastReceiver 
 {
    private Handler handler = new Handler();
    public static String state;
    private Context netcontext;
    public void onReceive(Context context, Intent intent) 
    {
     netcontext = context;
     NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);

     if(null != info)
     {
             state = getNetworkStateString(info.getState());
             Log.i("----------Network State",state);
     }
}

private String getNetworkStateString(NetworkInfo.State state)
{
    String stateString = "Unknown";
    switch(state)
    {
            case CONNECTED:         stateString = "Connected";      break;

            case CONNECTING:        stateString = "Connecting";     break;

            case DISCONNECTED:      

                stateString = "Disconnected";  
                handler.removeCallbacks(sendUpdatesToUI);
                handler.post(sendUpdatesToUI);

                break;

            case DISCONNECTING:     stateString = "Disconnecting";  break;

            case SUSPENDED:         stateString = "Suspended";      break;

            default:                stateString = "Unknown";        break;
    }
    return stateString;
}


private Runnable sendUpdatesToUI = new Runnable() 
{
     public void run() 
     {
             Toast.makeText(netcontext,"Please Check Your Internet Connection",Toast.LENGTH_SHORT).show();   
     }
}; }

Also Register into manifest.xml

<receiver android:name="com.example.utility.InternetConnectionBroadcast">
     <intent-filter>
     <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> 
     </intent-filter>
     </receiver>

Now whenever your connection will lost, it will notify message to the user.

like image 35
Miral Dhokiya Avatar answered Oct 11 '22 02:10

Miral Dhokiya