简体   繁体   English

React Native:使用AlarmManager在后台运行来自Java Module的JS代码

[英]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.

相关问题 Realm React-Native:从JS(反应本机代码)和android(java)访问同一领域 - Realm React-Native: Access same realm from JS(react native code) and android(java) Headless JS 后台任务未在 React-Native 中运行 - Headless JS Background task is not running in React-Native 如何从本地Java代码调用JS? - How to call JS from native Java code? 找不到'react-native-implementation.js'中的模块 - Cannot find module from 'react-native-implementation.js' 无法从 App.js 解析模块 - React Native - Unable to resolve module from App.js - React Native 有什么方法可以通过java本机模块(反应本机桥接器)访问领域实例(由javascript代码创建)? - Is there any way to access a realm instance (created by a javascript code) by a java native module (react native bridge)? npm 模块在 react.js 项目和 react native 项目中使用:如何在代码中定义不同的行为? - npm module to use both in a react.js project and in a react native project: how to define the different behaviour in the code? 在自定义 Java React Native 模块中调用 moveTaskToBack() 以编程方式将应用程序移动到后台 - Call moveTaskToBack() in custom Java React Native module to programmatically move app to background 我可以使用什么方法将本机模块中的事件发送到React Native中的JS - What is the method can I use to send an event from native module to JS in React Native JS服务器已经在运行React Native应用 - JS server already running react native app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM