lia*_*dee 72 javascript ios react-native
我在React Native中构建了一个小型iOS应用程序来进行位置跟踪,定期将lat/lng发送到用户选择的服务器.但是,这只适用于应用程序位于前台的情况.当用户在其他应用程序中时,如何在后台运行此任务?
Dan*_*idt 53
目前,遗憾的是,不支持任何类型的后台任务.您指的功能将是后台计时器.这样的计时器就是本产品的痛苦(功能请求),你可以通过它来表示对此功能的需求增加.
编辑12/2016:仍然没有真正的选择.你有RN 0.33以来的Headless JS API,但它只适用于android.如果它在前台运行,你的应用程序也会崩溃所以你必须小心使用它.感谢@Feng指出这一点.
Mat*_*son 22
现在有react-native-background-task,这是Android和iOS的单一API.
fro*_*der 10
在过去几个月里,React Native生态系统一直在以惊人的速度发展,并且已经出现了一些插件来解决无法在后台运行代码的痛苦.
https://github.com/transistorsoft/react-native-background-fetch - 定期唤醒30秒以运行一些任意JS代码.不适合高分辨率地理定位,因为唤醒之间的时间将是15分钟或更长.
https://github.com/transistorsoft/react-native-background-geolocation - 更适合这种情况,专门针对后台的地理位置.
我用它,它似乎起作用:https : //github.com/ocetnik/react-native-background-timer
定期发送事件(即使应用在后台运行)。
您可以使用setInterval和setTimeout函数。此API与react-native的API相同,可用于快速用后台计时器替换现有的计时器。
import BackgroundTimer from 'react-native-background-timer';
// Start a timer that runs continuous after X milliseconds
const intervalId = BackgroundTimer.setInterval(() => {
// this will be executed every 200 ms
// even when app is the the background
console.log('tic');
}, 200);
// Cancel the timer when you are done with it
BackgroundTimer.clearInterval(intervalId);
// Start a timer that runs once after X milliseconds
const timeoutId = BackgroundTimer.setTimeout(() => {
// this will be executed once after 10 seconds
// even when app is the the background
console.log('tac');
}, 10000);
// Cancel the timeout if necessary
BackgroundTimer.clearTimeout(timeoutId);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
53439 次 |
| 最近记录: |