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

4

2 回答 2

2

您可以创建一个新线程,定期检查(例如每 5 秒一次)状态并相应地更新 GPS。

于 2012-06-05T06:07:40.437 回答
1

我们如何在 oncreate() 中实现,以便在应用程序启动时,我们可以通过读取状态来设置 gps 的开启或关闭

因此,您可以onResume()在活动从后台运行时调用的方法中设置 GPS,例如在方法中onPause()设置 GPS 关闭,当活动进入后台时调用。

当您想检查 GPS 的状态时,您可以使用onGpsStatusChanged()并调用 getGpsStatu( 获取 GPS 的状态或使用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
        }

还请查看如何检查 GPS 接收器的当前状态?

于 2012-06-05T06:10:18.803 回答