How can I monitor the status of the GPS adapter using Android SDK?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 18:09:26

问题


I need to have my app monitor whether the GPS adapter is anabled or disabled. I don't care about the actual GPS functionality running at the monemt or not -- I need the status of the GPS adapter.

I can manually do it by calling:

String providers = Settings.Secure.getString(getContentResolver(),
        Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

If "gps" is listed, then it's active, otherwise it's not. But I can't find any documentation on getting notified when GPS is enabled or disabled by the user. I did try to use addGpsStatusListener in LocationManager, but that only notifies when the actual GPS fix is being acquired (like if the Maps app is being used).

Any ideas?


回答1:


LocationContentObserver Nevermind, I found the answer;

getContentResolver().registerContentObserver(
    Settings.Secure.getUriFor(Settings.System.LOCATION_PROVIDERS_ALLOWED), 
            false, 
            new LocationContentObserver ( new Handler())
    );

class LocationContentObserver extends ContentObserver {
  public LocationContentObserver( Handler h ) {
    super( h );
  }

  public void onChange(boolean selfChange) {
      SendNotification(false);
  }
}


来源:https://stackoverflow.com/questions/5026549/how-can-i-monitor-the-status-of-the-gps-adapter-using-android-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!