首页 > 解决方案 > React Native Headless js,Module AppRegistry 未注册(调用startHeadlessTask)

问题描述

我有反应原生后台服务问题 - Headless JS

我正在使用Expo框架

Android Studio 错误信息:

E/ReactNativeJS: The Expo SDK requires Expo to run. It appears the native Expo modules are unavailable and this code is not running on Expo. Visit https://docs.expo.io to learn more about developing an Expo project.
E/unknown:ReactNative: Unable to launch redbox because react activity is not available, here is the error that redbox would've displayed: The Expo SDK requires Expo to run. It appears the native Expo modules are unavailable and this code is not running on Expo. Visit https://docs.expo.io to learn more about developing an Expo project.
E/ReactNativeJS: Module AppRegistry is not a registered callable module (calling startHeadlessTask) 
E/unknown:ReactNative: Unable to launch redbox because react activity is not available, here is the error that redbox would've displayed: Module AppRegistry is not a registered callable module (calling startHeadlessTask)

JS API:(App.js)

const LockScreenOpenService = async (taskData) => {
   DeviceEventEmitter.addListener('LockScreenOpen', function(e: Event) {
      // handle event.
      console.log("== Debug: Received LockScreen View Open");
   });
};

AppRegistry.registerHeadlessTask('LockScreenOpenService', () => 
LockScreenOpenService);

LockScreenOpenService.java:

public class LockScreenOpenService extends HeadlessJsTaskService {

@Override
protected @Nullable
HeadlessJsTaskConfig getTaskConfig(Intent intent) {
    Bundle extras = intent.getExtras();
    if (extras != null) {
        return new HeadlessJsTaskConfig(
                "LockScreenOpenService",
                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;
}
}

服务开始:

 public void startLockViewService(){
  Intent listenIntent = new Intent(getApplication(), LockScreenOpenService.class);
  Bundle bundle = new Bundle();

  listenIntent.putExtras(bundle);
  startService(listenIntent);
}

AndroidManifest.xml

    <service
    android:name=".lockscreen.LockScreenOpenService"
    android:enabled="true"
    android:exported="true" />

我不知道为什么会出现“Module AppRegistry 不是注册的可调用模块(调用 startHeadlessTask)”!?

标签: react-nativeexpo

解决方案


推荐阅读