简体   繁体   English

从快速设置检测位置设置更改

[英]Detecting location settings change from quick settings

I'm currently developing an app that requires the Android's location service to always be active.我目前正在开发一个应用程序,需要 Android 的位置服务始终处于活动状态。

Right now I'm able to detect if the user activates or deactivates the location service when he resumes the app (in the onResume() event ).现在我能够检测到用户在恢复应用程序时是激活还是停用位置服务(在 onResume() 事件中)。

The problem is that, if the user deactivates the location service throught the quick settings menu, I am not able to detect the event.问题是,如果用户通过快速设置菜单停用位置服务,我将无法检测到该事件。

How can I know when the user activates / deactivates the device's location through the quick settings?如何通过快速设置知道用户何时激活/停用设备的位置?

Thank you.谢谢你。

My code:我的代码:

...

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

References I followed:我遵循的参考文献:

How to trigger broadcast receiver when gps is turn on/off? gps打开/关闭时如何触发广播接收器?

Android Quick Settings notifications? Android 快速设置通知?

Changing Location Settings 更改位置设置

I had the same problem.我有同样的问题。 this worked for me这对我有用

intentFilter.addAction(android.location.LocationManager.PROVIDERS_CHANGED_ACTION);

not to confuse it with不要混淆

Intent.ACTION_PROVIDER_CHANGED

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM