[英]React Native: running JS code from Java Module in background with AlarmManager
I'm looking for some way to execute JS code from Java Native Module. 我正在寻找一些从Java Native Module执行JS代码的方法。 I found solution with Timer , but it use Callbacks and it works only when app is in background ( React-native background process(android) handling? ).
我找到了使用Timer的解决方案,但是它使用了Callbacks,它只适用于app在后台( React-native后台进程(android)处理? )。
I'm using AlarmManager to wake up Java's BroadcastReceiver, but I have no idea how, from this point, run Javascript code. 我正在使用AlarmManager唤醒Java的BroadcastReceiver,但我不知道从这一点开始如何运行Javascript代码。 This code isn't a React Component, but it use some react sdk and modules (facebooksdk, googlesdk, Keychain, Asyncstorage).
此代码不是React组件,但它使用了一些react sdk和模块(facebooksdk,googlesdk,Keychain,Asyncstorage)。
I tried to create listener on DeviceEventEmitter (due to https://facebook.github.io/react-native/docs/native-modules-android.html ) and run in BroadcastReceiver something like 我尝试在DeviceEventEmitter上创建监听器 (由于https://facebook.github.io/react-native/docs/native-modules-android.html )并在BroadcastReceiver中运行
public class Alarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ReactApplicationContext contxt = (ReactApplicationContext) context;
contxt.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("runTask", null);
}
But I have cast runtime error: 但我有投射运行时错误:
Caused by: java.lang.ClassCastException: android.app.ReceiverRestrictedContext cannot be cast to com.facebook.react.bridge.ReactApplicationContext
Maybe it's not possible to run JS from Java Module in background, especially when application is closed. 也许在后台运行JS模块是不可能的,尤其是在应用程序关闭时。 Any tips are welcome.
欢迎任何提示。
Check out the transistorsoft github account 看看transistoroft github帐户
Specifically: 特别:
I've had some success with these. 我已经取得了一些成功。
However they all have their drawbacks. 然而它们都有它们的缺点。 Up to this point I have not found a package that is ACTUALLY running JS in the background.
到目前为止,我还没有找到一个在后台运行JS的程序包。 The RN team is supposed to be working on it, but nothing yet.
RN团队应该正在努力,但还没有。
The solution in this case would be to code whatever you need to run in the background in java. 在这种情况下,解决方案是编写您需要在java后台运行的任何代码。 Ultimately this is what I did.
最终这就是我所做的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.