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秒に1回)ステータスを確認し、それに応じて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 に答える