我怎样才能使用Android SDK监控GPS适配器的状态?[英] How can I monitor the status of the GPS adapter using Android SDK?

本文是小编为大家收集整理的关于我怎样才能使用Android SDK监控GPS适配器的状态?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我需要让我的应用程序监视器是GPS适配器是隔离还是禁用.我不在乎是否在Monemt运行的实际GPS功能 - 我需要GPS适配器的状态.

我可以通过致电:

手动执行此操作.
String providers = Settings.Secure.getString(getContentResolver(),
        Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

如果列出了" GPS",则它是活动的,否则否则就没有.但是,我找不到任何文档,即当用户启用或禁用GPS时获得通知.我确实尝试在LocationManager中使用AddGpsStatusListener,但这只有在获取实际的GPS修复时通知(例如使用MAPS应用程序).

有什么想法?

推荐答案

location -contentobserver Nevermind,我找到了答案;

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://www.itbaoku.cn/post/102724.html

问题描述

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?

推荐答案

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);
  }
}