React Native - Headless JS 示例 [英] React Native - Headless JS example

查看:42
本文介绍了React Native - Headless JS 示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于此的 react native 文档似乎不完整.我正在遵循这个指南,它似乎没有带我去任何地方:

The react native documentation about this seems more than incomplete. I was following this guide which doesn't seem to lead me anywhere:

https://facebook.github.io/react-native/releases/next/docs/headless-js-android.html#headless-js

我应该把 .java 文件放在哪里?我应该如何开始任务?

Where am I supposed to put the .java file? How am I supposed to start the task?

推荐答案

我遇到了和你一样的问题.我是原生 Android 开发人员的新手,我遵循了文档,但它们对我不起作用.结果我误解了,并认为我可以从我的本机代码开始实际任务.但是,它不是那样工作的.您编写要在任务启动时运行的 JavaScript 代码,但您需要从本机代码启动该任务.因此,您已经执行的步骤是设置任务,但没有任何内容告诉他们开始.

I had the same problem as you did. I'm new to native Android dev and I followed the docs, but they didn't work for me. Turns out I misunderstood and thought that I can start the actual task FROM my react native code. It doesn't work that way, though. You write the JavaScript code you want to run when the task is started, but you need to start that task from native code. So, steps you did already are setting up the task, but nothing is telling them to start.

我将以我的项目为例:

在我的 JavaScript 中:

In my JavaScript:

    /* 
      This code basically registers WHAT javascript code
      I want to call when the 'disconnect' service is started
    */

    const disconnectTask = () => async () => {
      if (connectionIsActive) {
        user.goOffline();
      }
    };

    AppRegistry.registerHeadlessTask('disconnect', disconnectTask);

然后我在这里创建了 java 文件:

Then I created the java file here:

projectroot/android/app/src/main/java/com/mycompany/app/ForceOfflineService.java

projectroot/android/app/src/main/java/com/mycompany/app/ForceOfflineService.java

package com.mycompany.app;
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 com.facebook.react.bridge.WritableMap;
import javax.annotation.Nullable;

public class ForceOfflineService extends HeadlessJsTaskService {
  @Override
  @Nullable
  protected HeadlessJsTaskConfig getTaskConfig(Intent intent) {
      Bundle extras = intent.getExtras();
      // WritableMap data = extras != null ? Arguments.fromBundle(extras) : null;
      
      // ---- EDIT ---- the line above no longer works, since null is not an accepted value anymore.
      WritableMap data = extras != null ? Arguments.fromBundle(extras) : Arguments.createMap();

      // Make sure that the string below ("disconnect" in this case)
      // matches the string used to register the task in your JS

      return new HeadlessJsTaskConfig(
              "disconnect",
              data,
              5000);
  }
}

然后在你的安卓清单里面(我的在这里:projectroot/android/app/src/main/AndroidManifest.xml)您必须在应用程序标签内注册服务

then inside your android manifest (mine is here: projectroot/android/app/src/main/AndroidManifest.xml) you must register the service inside the application tag

<manifest>
  <application>

    <!-- other existing configuration -->

    <service android:name=".ForceOfflineService" />
  </application>
</manifest>

最后,您需要使用本机代码来启动服务.在我的 MainActivity.kt 中,当调用 onStop 生命周期钩子时启动服务:

Lastly, you need to use the native code to start the service. In my MainActivity.kt I start the service when the onStop lifecycle hook is called:

 override fun onStop() {
    super.onStop()

    Handler(Looper.getMainLooper()).postDelayed(Runnable {
      val service = Intent(applicationContext, ForceOfflineService::class.java)
      applicationContext.startService(service)
    }, IDLE_TIME_BEFORE_DISCONNECT)
  }

就是这样!

这篇关于React Native - Headless JS 示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆