且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检测,当用户打开/关闭GPS状态?

更新时间:2023-12-01 19:57:34

您可以听一个GpsStatus.Listener GPS状态 并与LocationManager注册。

You can listen the GPS status with a GpsStatus.Listener and register it with the LocationManager.

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.addGpsStatusListener(new android.location.GpsStatus.Listener()
{
    public void onGpsStatusChanged(int event)
    {
        switch(event)
        {
        case GPS_EVENT_STARTED:
            // do your tasks
            break;
        case GPS_EVENT_STOPPED:
            // do your tasks
            break;
        }
    }
});

您需要访问上下文(例如在活动或应用程序级)。

You need to have access to the context (for example in an "Activity" or "Application" class).

  • https://developer.android.com/reference/android/location/GpsStatus.Listener.html
  • https://developer.android.com/reference/android/location/LocationManager.html
  • https://developer.android.com/reference/android/location/GpsStatus.Listener.html
  • https://developer.android.com/reference/android/location/LocationManager.html