如何将ManiActivity onCreate事件发送到React Native?

时间:2018-12-25 21:05:45

标签: java react-native

我正在处理一个闹钟,我不知道如何从MainActivity将Event发送到React Native。到目前为止,这是我设法做到的:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mInitialProps = new Bundle();
        final Bundle bundle = mActivity.getIntent().getExtras();      
        ReactInstanceManager mReactInstanceManager = getReactNativeHost().getReactInstanceManager();
        ReactApplicationContext context = (ReactApplicationContext) mReactInstanceManager.getCurrentReactContext();    
        if (context == null) {
            mReactInstanceManager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {
                public void onReactContextInitialized(ReactContext context) {
                    if (bundle != null && bundle.containsKey("sendAlarm")) {
                        if (bundle.getString("sendAlarm").equals("sendAlarmOn")) {
                            LauncherModule.startAlarm(mActivity); // works 
                            LauncherModule.sendAlarmEvent(); // doesn't work. Should run after alarm manager starts app which previously had been killed
                        }
                    }                                
                }
            });
        } else {
            if (bundle != null && bundle.containsKey("sendAlarm")) {
                if (bundle.getString("sendAlarm").equals("sendAlarmOn")) {
                    LauncherModule.startAlarm(mActivity); // works
                    LauncherModule.sendAlarmEvent(); // works and sends event only when app was left open
                }
            }                  
        }
        super.onCreate(savedInstanceState);
    }

仅当应用保持打开状态并且警报管理器重新启动应用本身时,该代码才有效。如果我关闭该应用程序并启动警报管理器,则似乎只有startAlarm函数(具有声音效果)会被蜂鸣触发。

无论我将sendEvent函数放在Mainactivity还是其他地方(例如外部模块),无论我做什么,如果我关闭应用程序,它都不会发送事件。对于这个问题Send data from Android activity to React Native,我也尝试将getReactInstanceManager().getCurrentReactContext()while组合使用,但无济于事。

还尝试创建设置为true onCreate的bolean beeing,然后发送事件onStart或onRestart。也无济于事。

有什么建议吗?

编辑:这是sendEvent函数的外观:

public final void sendEvent(String eventName, boolean isAlarmOn) {  
    getReactInstanceManager().getCurrentReactContext()
        .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
        .emit(eventName, isAlarmOn);
}

1 个答案:

答案 0 :(得分:0)

解决方案

好吧,我认为答案不是使用sendEvent方法onCreate,因为(我可能是错误的)侦听器似乎在事件发送后被初始化了。因此,没有人会听这个事件。

虽然在onStart,onRestart和onPause内似乎运行良好。

我们该怎么办? React Native为ReactActivityDelegate提供了初始道具。并且可以完成工作!

ReactActivityDelegate中的

MainActivity应该如下所示:

public class ActivityDelegate extends ReactActivityDelegate {
    private Bundle mInitialProps = null;
    private final @Nullable Activity mActivity; 

    public ActivityDelegate(Activity activity, String mainComponentName) {
        super(activity, mainComponentName);
        this.mActivity = activity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mInitialProps = new Bundle();
        final Bundle bundle = mActivity.getIntent().getExtras();  
        if (bundle != null && bundle.containsKey("sendAlarm")) {
            if (bundle.getString("sendAlarm").equals("sendAlarmOn")) {
                mInitialProps.putBoolean("alarmOn", true);
            }
        }       
        super.onCreate(savedInstanceState);
    }

    @Override
    protected Bundle getLaunchOptions() {
        return mInitialProps;
    }
};

@Override
protected ReactActivityDelegate createReactActivityDelegate() {
    return new ActivityDelegate(this, getMainComponentName());
}

然后在主应用程序组件(通常为index.android.js)中调用propTypes并使用它们运行代码:

static propTypes = {
    alarmOn: PropTypes.boolean
}

componentDidMount() {
    if (this.props.alarmOn === true) {
        // your code
    }
}

Voila!

您可以在此处找到完整的示例:https://github.com/vasyl91/react-native-android-alarms