【问题标题】:Headless JS Background task is not running in React-Native无头 JS 后台任务未在 React-Native 中运行
【发布时间】:2021-05-21 05:27:07
【问题描述】:
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')
    
  };








我是 React-Native 的新手。尝试在 react-native 中使用无头 js 创建应用程序 当我在 oncreate 方法中启动 MyTask 服务时 该应用程序崩溃且无法打开。当我在没有组件registerheadlessjs 任务的情况下注册时,我收到错误为“registercompoent 未被调用” 如果有人有,请给我一个简单的例子。

提前致谢

【问题讨论】:

  • 现在我可以开始我的无头任务,但它不在后台运行。当我终止进程时它会停止。任何帮助将不胜感激!
  • 这里也一样,我想在换天(凌晨 12:00)时运行一些 javascript 代码,即使应用程序没有运行。

标签: javascript java android react-native kotlin


【解决方案1】:
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
    );

把假改成真

【讨论】:

    猜你喜欢
    • 2016-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多