在运行时检查位置设置更改
Check location settings changes at runtime
我正在使用基本的位置更新行为来获取用户位置,如 this documentation 中所述。
它工作得很好,但我遇到了一些看似简单的事情:
我如何知道用户是否在应用 运行 时禁用了他的位置?
我可以在启动我的 activity 时使用 Settings Client 检查位置是否已启用,但如何在运行时执行此操作?
有一个广播: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);
我正在使用基本的位置更新行为来获取用户位置,如 this documentation 中所述。
它工作得很好,但我遇到了一些看似简单的事情:
我如何知道用户是否在应用 运行 时禁用了他的位置?
我可以在启动我的 activity 时使用 Settings Client 检查位置是否已启用,但如何在运行时执行此操作?
有一个广播: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);