React-native-background-action 无法读取 null 的属性“start”

问题描述 投票:0回答:1

我对存储库react-native-background-action有疑问。 它显示问题 类型错误,无法读取 null 的属性“start” 图片已附 enter image description here

我的代码示例

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 显示同样的问题。

如果您知道如何修复它,请告诉我。

简短说明我需要什么。 我需要一个函数,当应用程序处于非活动状态或关闭时,它可以获取当前时间等数据。

我过去的代码,这只是一个例子。

如果您知道任何存储库可以帮助我,请告诉我

javascript reactjs react-native background-process
1个回答
0
投票
  1. 尝试删除该应用程序并重新安装。由于您可能更新了 AndroidManifest.xlm,包括新的权限,因此需要重新安装它,而且很容易忘记这一点。
  2. 如果您已经尝试过,正如 devtyty 的 PR 中提到的,在使用 Android 14 时,您需要更改一些文件(可能库的维护人员很快就会修复这个问题)。

Change your code like described in this image

如果您想查看原始 PR,请查看这里

© www.soinside.com 2019 - 2024. All rights reserved.