- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试在 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/
当我收到一条短信时,我正在尝试发送一条消息以做出原生 react 。我真的需要帮助,但我不知道自己做错了什么! 这是我创建的模块,用于管理发送事件以响应 native 。 public class S
我想从 ReactNative 代码开始我的服务。 这是我用来桥接的模块。 public class HeartbeatModule extends ReactContextBaseJavaModul
我正在尝试在 React-Native 中运行后台服务。据我所知,我需要用原生 Java 编写它并将其连接到 react-native 代码。当我尝试发出事件时出现错误: Tried to acces
我需要通过以下方式在 MainActivity 的 onCreate 函数中调用 native 模块:context.getNativeModule(RNBackgroundToForegroundS
由于 React-Native AppState API 仅适用于 iOS,因此我正在为我的应用程序的 Android 端编写一个 appState 事件发射器。在 Java 端的应用程序 MainA
我试图创建一个应用程序,它会使用 BroadcastReciver 捕获手机的来电。从我的 BroadcastReciver 我想使用 this 将数字作为事件发送到我的 JS 文件方法。 我已经检查
我想使用方法sendEvent(),但是如何获取ReactContext reactContext? private void sendEvent(ReactContext reactContext,
我想在 MyCustomModuleClass 中调用一个方法,从另一个类扩展 ReactContextBaseJavaModule MyCustomModule.java public class
我是一名优秀的程序员,十分优秀!