gpt4 book ai didi

java - React Native ReactContext 生命周期停留在 BEFORE_CREATE

转载 作者:搜寻专家 更新时间:2023-11-01 08:30:44 26 4
gpt4 key购买 nike

我正在尝试在 React-Native 中运行后台服务。据我所知,我需要用原生 Java 编写它并将其连接到 react-native 代码。当我尝试发出事件时出现错误:

Tried to access a JS module before the React instance was fully set up. Calls to should only happen once initialize() has been called on your native module.

所以我添加了一个检查模块是否正在运行:

if (reactContext.getLifecycleState() == LifecycleState.RESUMED)

但它总是返回 false。生命周期停留在 BEFORE_CREATE。我应该如何发出我的事件。

服务:

public class TestService extends Service {

double distance = 0.0;
ReactContext reactContext;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
reactContext = new ReactContext(getApplicationContext());
new Timer().scheduleAtFixedRate(new TimerTask(){
@Override
public void run(){
WritableMap params = Arguments.createMap();
distance+= 0.7;
Log.d("LOG", "Trying to send distance: "+distance+" on lifecycle: "+reactContext.getLifecycleState());
params.putDouble("distance", distance);
sendEvent(reactContext, "updateDistance", params);
}
},0,1000);
return START_STICKY;
}

private void sendEvent(ReactContext reactContext, String eventName, @Nullable WritableMap params) {
if (reactContext.getLifecycleState() == LifecycleState.RESUMED) {
reactContext.getJSModule(DeviceEventManagerModule
.RCTDeviceEventEmitter.class)
.emit(eventName, params);
Log.d("LOG", "Sent distance: "+distance);
}
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}

模块:

public class ServiceModule extends ReactContextBaseJavaModule {
ReactContext reactContext;

public ServiceModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
this.initialize();
}

@ReactMethod
public void startTrackingService() {
Intent intent = new Intent(reactContext, TestService.class);
reactContext.startService(intent);
}

@Override
public String getName() {
return "ServiceModule";
}
}

包:

public class ServicePackage implements ReactPackage {
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new ServiceModule(reactContext));
return modules;
}

@Override
public List<Class<? extends JavaScriptModule>> createJSModules() {
return Collections.emptyList();
}

@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
}

主要应用:

@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new ReactNativePushNotificationPackage(),
new ServicePackage()
);
}

最佳答案

我解决了:)在服务中,我从不是同一个对象的基本上下文创建了一个新上下文。解决方法是广播来自服务的数据,然后使用 javascript 发送它们。

服务模块:

public class ServiceModule extends ReactContextBaseJavaModule {
public static String UPDATE = "updateDistance";
public static String DISTANCE = "distance";

private IntentFilter intentFilter;
private BroadcastReceiver receiver;

public ServiceModule(ReactApplicationContext reactContext) {
super(reactContext);
initializeBroadcastReceiver();
}

@ReactMethod
public void startTrackingService() {
Intent intent = new Intent(getReactApplicationContext(), TestService.class);
getReactApplicationContext().startService(intent);
}

@ReactMethod
public void stopTrackingService() {
Intent intent = new Intent(getReactApplicationContext(), TestService.class);
getReactApplicationContext().stopService(intent);
}

private void sendEvent(ReactContext reactContext, String eventName, @Nullable WritableMap params) {
if (reactContext.hasActiveCatalystInstance()) {
reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(eventName, params);
}
}

private void initializeBroadcastReceiver() {
intentFilter = new IntentFilter();
intentFilter.addAction(UPDATE);
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
WritableMap params = Arguments.createMap();
params.putDouble(DISTANCE, intent.getDoubleExtra(DISTANCE, 0));
sendEvent(getReactApplicationContext(), UPDATE, params);
}
};
getReactApplicationContext().registerReceiver(receiver, intentFilter);
}

@Override
public String getName() {
return "ServiceModule";
}
}

测试服务:

public class TestService extends Service {

double distance = 0.0;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new Timer().scheduleAtFixedRate(new TimerTask(){
@Override
public void run(){
Intent broadcastIntent = new Intent();
broadcastIntent.setAction(ServiceModule.UPDATE);
broadcastIntent.putExtra(ServiceModule.DISTANCE, distance);
sendBroadcast(broadcastIntent);
distance+= 0.7;
}
},0,1000);
return START_STICKY;
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}

关于java - React Native ReactContext 生命周期停留在 BEFORE_CREATE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41120491/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com