[英]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')
};
我是 React-Native 的新手。 嘗試在 react-native 中使用無頭 js 創建應用程序當我在oncreate
方法中啟動 MyTask 服務時,應用程序崩潰並且無法打開。 當我在沒有組件registerheadlessjs
任務的情況下注冊時,我收到錯誤為“未調用registercompoent
”如果有人有,請給我一個簡單的例子。
提前致謝
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
);
將假改為真
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.