【发布时间】:2020-03-23 15:37:04
【问题描述】:
我目前正在开发一个需要 Android 的位置服务始终处于活动状态的应用。
现在我能够检测到用户在恢复应用时(在 onResume() 事件中)是激活还是停用了定位服务。
问题是,如果用户通过快速设置菜单停用定位服务,我无法检测到该事件。
我如何知道用户何时通过快速设置激活/停用设备的位置?
谢谢。
我的代码:
...
BroadcastReceiver mGpsSwitchStateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() != null && intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
// Make an action or refresh an already managed state.
Log.d("vtApp", "CHANGED");
}
}
};
currentActivity.registerReceiver(mGpsSwitchStateReceiver, new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));
我关注的参考文献:
How to trigger broadcast receiver when gps is turn on/off?
【问题讨论】: