简体   繁体   English

Headless JS 后台任务未在 React-Native 中运行

[英]Headless JS Background task is not running in React-Native

import {AppRegistry} from 'react-native';
import App from './App';
import task from './src/task';
import {name as appName} from './app.json';


//AppRegistry.registerComponent('App',()=>App);
AppRegistry.registerHeadlessTask('SomeTaskName', () =>
task
);

package com.convertwebsitetoapp;
import android.content.Intent;
import android.os.Bundle;
import com.facebook.react.HeadlessJsTaskService;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.jstasks.HeadlessJsTaskConfig;
import javax.annotation.Nullable;

public class MyTaskService extends HeadlessJsTaskService {

  @Override
  protected @Nullable HeadlessJsTaskConfig getTaskConfig(Intent intent) {
    Bundle extras = intent.getExtras();
    if (extras != null) {
      return new HeadlessJsTaskConfig(
          "SomeTaskName",
          Arguments.fromBundle(extras),
          5000, // timeout for the task
          false // optional: defines whether or not  the task is allowed in foreground. Default is false
        );
    }
    return null;
  }
}

task.js
module.exports = async (taskData) => {
    console.log('Demo')
    
  };








I am new in React-Native.我是 React-Native 的新手。 Trying to create an app using headless js in react-native When i start MyTask service inside oncreate method then the app is crashing and not opening.尝试在 react-native 中使用无头 js 创建应用程序当我在oncreate方法中启动 MyTask 服务时,应用程序崩溃并且无法打开。 And when am registering without component registerheadlessjs task then am getting the error as " registercompoent was not called" Please give me one simple example if anyone has.当我在没有组件registerheadlessjs任务的情况下注册时,我收到错误为“未调用registercompoent ”如果有人有,请给我一个简单的例子。

Thanks in advance提前致谢

return new HeadlessJsTaskConfig(
      "SomeTaskName",
      Arguments.fromBundle(extras),
      5000, // timeout for the task
      true// optional: defines whether or not  the task is allowed in foreground. Default is false
    );

Change false to true将假改为真

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM