use*_*854 3 gps android location
我需要让我的应用程序监控GPS适配器是否已启用或已禁用.我不关心在monemt上运行的实际GPS功能 - 我需要GPS适配器的状态.
我可以通过调用手动执行:
String providers = Settings.Secure.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
Run Code Online (Sandbox Code Playgroud)
如果列出了"gps",那么它是活动的,否则它不是.但是,当用户启用或禁用GPS时,我找不到任何有关获得通知的文档.我确实尝试在LocationManager中使用addGpsStatusListener,但这只会在获取实际GPS修复时通知(例如,如果正在使用地图应用程序).
有任何想法吗?
LocationContentObserver没关系,我找到了答案;
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);
}
}
Run Code Online (Sandbox Code Playgroud)