How to execute a function after 1minute of the app goes to the background in react-native

huangapple go评论103阅读模式
英文:

How to execute a function after 1minute of the app goes to the background in react-native

问题

我想在 React Native 中实现一个功能,在应用程序在后台停留1分钟后,同时改变状态和导航。我已经使用了我的代码,但它没有起作用。

  1. useEffect(() => {
  2. let timeoutId;
  3. const handleAppStateChange = (nextAppState) => {
  4. // nextAppState 可能是 'active', 'background', 或 'inactive'
  5. if (nextAppState === 'background') {
  6. // 应用程序正在切换到后台
  7. console.log('应用程序在后台');
  8. isBackgndRef.current = true;
  9. timeoutId = setTimeout(() => {
  10. checkCondition();
  11. }, 20000);
  12. // 在这里可以执行任何与后台相关的任务
  13. } else if (nextAppState === 'active') {
  14. // 应用程序正在切换到前台
  15. console.log('应用程序在前台');
  16. // 在这里可以执行任何与前台相关的任务
  17. if (isBackgndRef.current == true) {
  18. console.log("检查中");
  19. clearTimeout(timeoutId);
  20. isBackgndRef.current = false;
  21. }
  22. }
  23. }
  24. // 组件挂载时添加事件监听器
  25. AppState.addEventListener('change', handleAppStateChange);
  26. // 组件卸载时清除事件监听器
  27. return () => {
  28. AppState.removeEventListener('change', handleAppStateChange);
  29. clearTimeout(timeoutId);
  30. console.log("返回调用");
  31. };
  32. }, []);
英文:

I want to execute a function and changing state along with navigation change when the app goes to the background after 1 minute of it in react native, i have used my code but it does not working..

  1. useEffect(() => {
  2. let timeoutId;
  3. const handleAppStateChange = (nextAppState) => {
  4. // nextAppState can be 'active', 'background', or 'inactive'
  5. if (nextAppState === 'background') {
  6. // Your app is transitioning to the background
  7. console.log('App is in the background');
  8. isBackgndRef.current = true
  9. timeoutId = setTimeout(() => {
  10. checkCondition();
  11. }, 20000);
  12. // You can perform any background-related tasks here
  13. } else if (nextAppState === 'active') {
  14. // Your app is transitioning to the foreground
  15. console.log('App is in the foreground');
  16. // You can perform any foreground-related tasks here
  17. if (isBackgndRef.current == true) {console.log("checking");
  18. clearTimeout(timeoutId);
  19. isBackgndRef.current = false;
  20. }
  21. }
  22. }
  23. // Add the event listener when the component mounts
  24. AppState.addEventListener('change', handleAppStateChange);
  25. // Clean up the event listener when the component unmounts
  26. return () => {
  27. AppState.removeEventListener('change', handleAppStateChange);
  28. clearTimeout(timeoutId); console.log("return called");
  29. };
  30. }, []);

答案1

得分: 1

对于后台应用程序状态,请使用react-native-background-timer插件中的setTimeout(),而不是react-native的setTimeout()。

使用以下链接找到react-native-background-timer插件:
https://www.npmjs.com/package/react-native-background-timer

英文:

For the background app state, please use setTimeout() from the
react-native-background-timer plugin instead of react-native setTimout().

Use the following link to find the react-native-background-timer plugin:
https://www.npmjs.com/package/react-native-background-timer

huangapple
  • 本文由 发表于 2023年7月27日 16:58:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76778115.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定