【问题标题】:Check location settings changes at runtime在运行时检查位置设置更改
【发布时间】:2018-01-27 11:31:29
【问题描述】:

我正在使用基本位置更新行为来获取用户位置,如 this documentation 中所述。

它工作得很好,但我被一些看似简单的东西卡住了:

我如何知道用户在应用运行时是否禁用了他的位置信息?

我可以在开始我的活动时使用Settings Client 检查是否启用了位置,但如何在运行时执行此操作?

【问题讨论】:

    标签: java android google-maps geolocation


    【解决方案1】:

    有一个广播:https://developer.android.com/reference/android/location/LocationManager.html#MODE_CHANGED_ACTION

    代码类似于:

    private final IntentFilter filter =
              new IntentFilter(LocationManager.MODE_CHANGED_ACTION);
    
    private final BroadcastReceiver receiver = new BroadcastReceiver() {
       @Override public void onReceive(Context context, Intent intent) {
           if(LocationManager.MODE_CHANGED_ACTION.equals(intent.getAction()) {
                // check here the new location status
           }
       }
    };
    
    // then to register
    context.registerReceiver(receiver, filter);
    /// and unregister
    context.unregisterReceiver(receiver);
    

    【讨论】:

    • 完美!我只需要在请求位置更新时注册此 BroadcastReceiver,并在我的活动终止时取消注册。谢谢!
    猜你喜欢
    • 2018-08-04
    • 2015-08-07
    • 1970-01-01
    • 2011-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多