我正在开发 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
它继续,永不停止.
如何处理?有什么建议吗?
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模板网!