本文介绍了使用Android WorkManager响应Native HeadlessJs任务调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Android WorkManager创建一个React Native后台同步任务.正确,定期地调用WorkManager类中的doWork方法.我面临的问题是如何从doWork方法中调用react本机函数.在我当前的实现中,未调用JavaScript函数.
I'm creating a React Native background sync task with Android WorkManager. The doWork method in WorkManager class is called periodically, correctly. The problem I'm facing is how to call the react native function from the doWork method.In my current implementation the JavaScript function is not called.
React Native中的无头任务
// index.js
AppRegistry.registerHeadlessTask('SyncHeadlessTask', () => SyncHeadlessTask);
// SyncHeadlessTask.js
module.exports = async (taskData) => {
console.log('task running');
};
我在Java中的HeadlessTask
public class SyncHeadlessTaskService extends HeadlessJsTaskService {
@Override
protected @Nullable HeadlessJsTaskConfig getTaskConfig(Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
return new HeadlessJsTaskConfig(
"SyncHeadlessTask",
Arguments.fromBundle(extras),
10000, // timeout for the task
true // optional: defines whether or not the task is allowed in foreground. Default is false
);
}
return null;
}
}
我的工人
public class SyncWorker extends Worker {
private static final String TAG = "SyncWorker";
public SyncWorker(@Nonnull Context context, @Nonnull WorkerParameters workerParams) {
super(context, workerParams);
}
@Nonnull
@Override
public Result doWork() {
return Result.success();
}
}
我的模块
public class SyncModule extends ReactContextBaseJavaModule {
private static final String MODULE_NAME = "SyncManager";
private Context mContext;
private PeriodicWorkRequest workRequest;
private static final String TAG = "SyncModule";
SyncModule(@Nonnull ReactApplicationContext reactContext) {
super(reactContext);
mContext = reactContext;
workRequest = new PeriodicWorkRequest.Builder(SyncWorker.class, 15, TimeUnit.MINUTES).build();
}
@ReactMethod
public void startSync() {
WorkManager.getInstance().enqueueUniquePeriodicWork("SyncWork", ExistingPeriodicWorkPolicy.KEEP, workRequest);
}
@ReactMethod
public void stopSync() {
WorkManager.getInstance().cancelUniqueWork("SyncWork");
}
@Nonnull
@Override
public String getName() {
return MODULE_NAME;
}
}
我的包裹
public class SyncPackage implements ReactPackage {
@Nonnull
@Override
public List<NativeModule> createNativeModules(@Nonnull ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new SyncModule(reactContext));
return modules;
}
@Nonnull
@Override
public List<ViewManager> createViewManagers(@Nonnull ReactApplicationContext reactContext) {
return Collections.emptyList();
}
}
推荐答案
您需要将服务作为服务启动:
You need start your task as a service:
@Nonnull
@Override
public Result doWork() {
Intent service = new Intent(getApplicationContext(), SyncHeadlessTaskService.class);
Bundle bundle = new Bundle();
bundle.putString("foo", "bar");
service.putExtras(bundle);
getApplicationContext().startService(service);
return Result.success();
}
不要忘记配置您的AndroidManifest:
Don't forget to configure your AndroidManifest:
<service android:name="your.package.SyncHeadlessTaskService" />
这篇关于使用Android WorkManager响应Native HeadlessJs任务调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!