Appstate继续在Android的React Native中进行更改 [英] Appstate keep on getting change in React native in Android

查看:322
本文介绍了Appstate继续在Android的React Native中进行更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on React native project and there I am taking location permissions. Also I have to track location permissions always like if user has given permission access after install the application and then after sometime user goes to the app settings in device settings and disable/revoked the permissions. Again once app comes from background to foreground, I have to check permission based on that, Needs to show the messages.

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

So that, I am using Appstate. But, In Android strangely, After installed the application, If user denied the permission with "Dont show again" checkbox, Then Appstate getting keep on changing with background and active always. It is keep on loop.

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

它持续不断,永远不会停止.

It goes on and never stop.

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

How to handle this? Any suggestions?

推荐答案

您可以使用标志来检查应用程序应处理后台还是仅仅是权限调用.

You can use a flag that check whether app should handle background or it's just a permission call.

const shouldHandleBackground = useRef(true)

const handler = (state) => { 
    if (state === 'active' && shouldHandleBackground.current) { 
        doStuff() 
    }
}

// when calling for permisson make the flag false

shouldHandleBackground.current = false
await Android.permission.LocationPermission()
shouldHandleBackground.current = true

在权限请求后,您可以将标志设为真

and after permission request you can make flag true

这篇关于Appstate继续在Android的React Native中进行更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆