我正在使用基本位置更新行为来获取用户位置,如 this documentation 中所述。 .
它工作得很好,但我被一些看似简单的东西困住了:
如何知道用户在应用运行时是否禁用了其位置?
我可以使用 Settings Client 检查位置是否启用当开始我的 Activity 时,但如何在运行时执行此操作?
最佳答案
有一个广播: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);
关于java - 在运行时检查位置设置更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48475348/