简体   繁体   English

使用 React Native 监控设备位置设置

[英]Monitor device location settings with React Native

Is there a way to monitor when a phone has their location settings (not permissions) turned on or off?有没有办法监控手机何时打开或关闭其位置设置(不是权限)?

I know there's a way to check with react-native-device-info with the code below, but is there a way to add a listener to constantly check?我知道有一种方法可以使用下面的代码检查react-native-device-info ,但是有没有办法添加一个监听器来不断检查?

DeviceInfo.isLocationEnabled().then((enabled) => {
  // true or false
});

When users change location setting, Users open setting page.当用户更改位置设置时,用户打开设置页面。 It means you should know if app is foreground or background.这意味着您应该知道应用程序是前台还是后台。

In that case, you can use AppState ( https://reactnative.dev/docs/appstate ).在这种情况下,您可以使用 AppState ( https://reactnative.dev/docs/appstate )。

To monitor when a phone has their location settings (not permissions) turned on or off, You can implement like this.要监控手机何时打开或关闭其位置设置(不是权限),您可以这样实现。

import {AppState} from 'react-native';

useEffect(() => {
  const subscription = AppState.addEventListener('change', (nextAppState) => {
    // if app is foreground.
    if (nextAppState === 'active') {
      DeviceInfo.isLocationEnabled().then((enabled: boolean) => {
        // true or false
      });
    }
  });
  return () => {
    subscription.remove();
  };
}, []);

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

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