問題描述
我正在開發 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模板網!