0

In my application on button click I'm maKing the Gps "ON" or "OFF"

If GPS is ON then If we press home button, application will go to background then when the application comes to foreground once again the GPS should be in enabled(ON) position

something like this

  final LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    boolean statusOfGPS = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

    if(statusOfGPS == true){
              keep the GPS in "ON" position

    }
     else if(statusOfGPS == false){
        keep the GPS in "OFF" position
    } 

How can we implement in oncreate() , so that when the application starts we can set the gps on or off by reading the state

Help is always appreciated! Thanks

Randroid
  • 3,688
  • 5
  • 30
  • 55
  • you can not get GPS on Starting/Loading of the application, because GPS data takes time to fetch. – Lucifer Jun 05 '12 at 06:16

2 Answers2

2

You can create a new thread, periodically check (for example once every 5 seconds) the status and correspondingly update GPS.

Nick
  • 949
  • 1
  • 11
  • 31
1

How can we implement in oncreate() , so that when the application starts we can set the gps on or off by reading the state

So you can set the GPS on in onResume() method which is called when activity is going from background and in method for example onPause() set GPS off, this is called when activity is going to background.

When you want to check status of GPS, you can use onGpsStatusChanged() and call getGpsStatu( for get status of GPS or use GpsStatusListener()

public void onGpsStatusChanged(int event) {
        if (event == GpsStatus.GPS_EVENT_STARTED) {
          // something to do

        } else if (event == GpsStatus.GPS_EVENT_STOPPED) {
            // something to do

        } else if (event == GpsStatus.GPS_EVENT_FIRST_FIX) {
           // something to do
        }

Have look also at How can I check the current status of the GPS receiver?

Community
  • 1
  • 1
Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
  • Thanks, for WIFI I could able to read the status in oncreate and its working for me, but not for gps. what could be the reason? – Randroid Jun 05 '12 at 06:18
  • so i don't know immediately now exactly, there can be more reasons. but onCreate method isn't right place. – Simon Dorociak Jun 05 '12 at 06:20