我对存储库react-native-background-action有疑问。 它显示问题 类型错误,无法读取 null 的属性“start” 图片已附
我的代码示例
import { View, Button } from 'react-native';
import BackgroundService from 'react-native-background-actions';
const sleep = (time:number) => new Promise<void>((resolve) => setTimeout(() => resolve(), time));
const veryIntensiveTask = async (taskDataArguments:any) => {
console.log("in intensive task");
const { delay } = taskDataArguments;
await new Promise( async (resolve) => {
for (let i = 0; BackgroundService.isRunning(); i++) {
console.log(i);
await sleep(delay);
}
});
};
const options = {
taskName: 'Example',
taskTitle: 'ExampleTask title',
taskDesc: 'ExampleTask description',
taskIcon: {
name: 'ic_launcher',
type: 'mipmap',
},
color: '#ff00ff',
parameters: {
delay: 1000,
},
};
export default function Background(){
const startBackgoundJob=async ()=>{
await BackgroundService.start(veryIntensiveTask, options);
console.log("background service started");
};
const updateBackgroundJob=async ()=>{
await BackgroundService.updateNotification({taskDesc: 'New ExampleTask description'});
console.log("background service updated");
};
const stopBackgroundJob=async ()=>{
await BackgroundService.stop();
console.log("background service stopped");
};
return(
<View style={{flex:1,display:"flex",justifyContent:"center",alignItems:"center"}}>
<Button title="start background job" onPress={startBackgoundJob}/>
<Button title="update background job" onPress={updateBackgroundJob}/>
<Button title="stop background job" onPress={stopBackgroundJob}/>
</View>
)
}
rn-foreground-service 显示同样的问题。
如果您知道如何修复它,请告诉我。
简短说明我需要什么。 我需要一个函数,当应用程序处于非活动状态或关闭时,它可以获取当前时间等数据。
我过去的代码,这只是一个例子。
如果您知道任何存储库可以帮助我,请告诉我
如果您想查看原始 PR,请查看这里。