Appstate在Android的React native中不断得到改变

问题描述 投票:3回答:1

我正在研究React本机项目,在那里我获得了位置许可。另外,我还必须始终跟踪位置权限,就像用户在安装应用程序后是否已授予权限访问权限一样,然后一段时间后,用户进入设备设置中的应用程序设置并禁用/撤销权限。同样,一旦应用程序从后台运行到前台,我必须基于该权限检查权限,需要显示消息。

因此,我正在使用Appstate。但是,奇怪的是,在Android中,安装应用程序后,如果用户通过“不再显示”复选框拒绝了该权限,则Appstate始终以backgroundactive进行更改。它保持循环。

componentDidMount = async () => {
    AppState.addEventListener('change', this.handleAppStateChange);
  };

  componentWillUnmount() {
    AppState.removeEventListener('change', this.handleAppStateChange);
    Geolocation.clearWatch(this.watchID);
  }

  handleAppStateChange = async nextAppState => {
    const {appState} = this.state;
    console.log('nextAppState -->', nextAppState);
    console.log('appState -->', appState);
    if (appState === 'active') {
      // do this
      this.showLoader();
      await this.requestAndroidLocationPermission();
    } else if (appState === 'background') {
      // do that
    } else if (appState === 'inactive') {
      // do that other thing
    }

    this.setState({appState: nextAppState});
  };

requestAndroidLocationPermission = async () => {
    try {
      const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
        {},
      );
      if (granted === PermissionsAndroid.RESULTS.GRANTED) {
        this.getLatitudeLongitude();
      } else if (granted === PermissionsAndroid.RESULTS.NEVER_ASK_AGAIN) {
        this.hideLoader();
        this.setState({
          errorMessage: 'Location permission is denied',
          isLoading: false,
        });
      } else {
        this.hideLoader();
        this.requestAndroidLocationPermission();
      }
    } catch (err) {
      console.warn(err);
    }
  };

拒绝的权限下,不再显示]后继续打印(循环)>

appState --> active
nextAppState --> background
appState --> active
nextAppState --> background
appState --> active
nextAppState --> background
appState --> active

持续进行,永不停止。

如何处理?有什么建议吗?

我正在研究React本机项目,在那里我获得了位置许可。此外,我还必须始终跟踪位置权限,就像用户在安装应用程序后是否已授予权限访问权限一样。...

android react-native geolocation location addeventlistener
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.