java - 在运行时检查位置设置更改

标签 java android google-maps geolocation

我正在使用基本位置更新行为来获取用户位置,如 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/

相关文章:

java - 如何在 Android 中重置 AtomicInteger 唯一 ID?

java - [Android map API] : Google-play-services prompting update on phone but not in emulator.

java - EntityManager 总是返回 null

java - 进行 DFS 时尝试中的原始值 (int)

java - 在单个 Activity 中使用 2 个或更多 Firebase 实时数据库

android - 缺少 Google Play 服务

google-maps - 如何使用GPRS检测手机位置?

java - 如何重定向到 Spring MVC 中的 View

java - 无法使用 pyjks 打开 JCEKS keystore

android - java.lang.OutOfMemoryError : bitmap size exceeds VM budget - Android