• <small id='rCKVZ'></small><noframes id='rCKVZ'>

    <legend id='rCKVZ'><style id='rCKVZ'><dir id='rCKVZ'><q id='rCKVZ'></q></dir></style></legend>
    1. <i id='rCKVZ'><tr id='rCKVZ'><dt id='rCKVZ'><q id='rCKVZ'><span id='rCKVZ'><b id='rCKVZ'><form id='rCKVZ'><ins id='rCKVZ'></ins><ul id='rCKVZ'></ul><sub id='rCKVZ'></sub></form><legend id='rCKVZ'></legend><bdo id='rCKVZ'><pre id='rCKVZ'><center id='rCKVZ'></center></pre></bdo></b><th id='rCKVZ'></th></span></q></dt></tr></i><div id='rCKVZ'><tfoot id='rCKVZ'></tfoot><dl id='rCKVZ'><fieldset id='rCKVZ'></fieldset></dl></div>

      <tfoot id='rCKVZ'></tfoot>
          <bdo id='rCKVZ'></bdo><ul id='rCKVZ'></ul>

      1. Appstate 不断在 Android 中的 React Native 中获得变化

        时间:2023-09-01

              <tbody id='GZo5z'></tbody>

            <tfoot id='GZo5z'></tfoot>
            1. <small id='GZo5z'></small><noframes id='GZo5z'>

              <i id='GZo5z'><tr id='GZo5z'><dt id='GZo5z'><q id='GZo5z'><span id='GZo5z'><b id='GZo5z'><form id='GZo5z'><ins id='GZo5z'></ins><ul id='GZo5z'></ul><sub id='GZo5z'></sub></form><legend id='GZo5z'></legend><bdo id='GZo5z'><pre id='GZo5z'><center id='GZo5z'></center></pre></bdo></b><th id='GZo5z'></th></span></q></dt></tr></i><div id='GZo5z'><tfoot id='GZo5z'></tfoot><dl id='GZo5z'><fieldset id='GZo5z'></fieldset></dl></div>
            2. <legend id='GZo5z'><style id='GZo5z'><dir id='GZo5z'><q id='GZo5z'></q></dir></style></legend>

              • <bdo id='GZo5z'></bdo><ul id='GZo5z'></ul>
                • 本文介绍了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 会随着 backgroundactive 不断变化.它一直在循环.

                  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
                  

                  它继续,永不停止.

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

                  How to handle this? Any suggestions?

                  推荐答案

                  我也遇到了同样的问题.不要使用 AppState.有问题.

                  I had the same problem. Do not use AppState. Is faulty.

                  问题在于 RN 对背景"的定义.react-native 使用 android 的活动(UI 线程的持有者和你的 UI 所在的位置)onPause 回调作为发送背景"的触发器.信号.但是,onPause 每次出现在您的活动视图层次结构前面时都会调用,例如对话框(例如权限框)、其他活动(例如文件选择器)等;对于 android react-native,背景";表示被外来 ​​UI 元素/android 任务遮蔽";而不是暂停并发送到后台执行其他操作",从而导致您看到的循环.最短的解决方案是在 ReactActivity 中覆盖 onPause,并添加控制条件以确保 super.onPause 仅在您实际进入后台时调用,例如检查您的任务堆栈,或者是否正在调用权限对话框,因此您避免这种循环/错误调用.第二种选择是提供您自己的应用生命周期事件,并提供明确的触发条件.

                  the problem lies within RN's definition of "background". react-native uses android's activity (the holder of the UI thread and where your UI lives) onPause callback as the trigger for sending the "background" signal. But, onPause is called everytime SOMETHING comes in front of your activity's view hierachy, like Dialogs (like the permission box), other activities (like a file picker), etc; for android react-native, "background" means "shadowed by a foreign UI element/android task" rather than "paused and sent to background to do something else", thus causing the loops you see. The shortest solution is to override onPause in your ReactActivity, and add control conditions to make sure super.onPause is only called when you are actually going to background, like checking your task stack, or if the permission dialog is being called, so you avoid this kind of loop/faulty call. A second option would be to provide your own app lifecycle event instead, with clear triggering conditions.

                  这篇关于Appstate 不断在 Android 中的 React Native 中获得变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:navigator.geolocation.getCurrentPosition 在 android google c 下一篇:navigator.geolocation.getCurrentPosition 在 Android 浏览器上

                  相关文章

                  最新文章

                  <small id='wLJZ0'></small><noframes id='wLJZ0'>

                  <tfoot id='wLJZ0'></tfoot>
                  1. <i id='wLJZ0'><tr id='wLJZ0'><dt id='wLJZ0'><q id='wLJZ0'><span id='wLJZ0'><b id='wLJZ0'><form id='wLJZ0'><ins id='wLJZ0'></ins><ul id='wLJZ0'></ul><sub id='wLJZ0'></sub></form><legend id='wLJZ0'></legend><bdo id='wLJZ0'><pre id='wLJZ0'><center id='wLJZ0'></center></pre></bdo></b><th id='wLJZ0'></th></span></q></dt></tr></i><div id='wLJZ0'><tfoot id='wLJZ0'></tfoot><dl id='wLJZ0'><fieldset id='wLJZ0'></fieldset></dl></div>
                    <legend id='wLJZ0'><style id='wLJZ0'><dir id='wLJZ0'><q id='wLJZ0'></q></dir></style></legend>

                    • <bdo id='wLJZ0'></bdo><ul id='wLJZ0'></ul>