天天看点

如何在react-native中发送获取Broadcast

如何在react-native中发送获取Broadcast

在reactContext中提供了sendEventToJS的方法,可以向RN端发送Event,在下包装了下,方便在Native中与RN使用Broadcast交流。

使用方法

npm install react-native-system-broadcast
           

栗子

import {NavigationPage} from 'teaset';
import {Fragment} from 'react';
import Label from 'teaset/components/Label/Label';
import React from 'react';
import RNBroadCast from 'react-native-system-broadcast';
import {DeviceEventEmitter, View} from 'react-native';

export default class MainPage extends NavigationPage {
    constructor(props) {
        super(props);
    }

    componentDidMount() {
        RNBroadCast.on('android.intent.action.CLOSE_SYSTEM_DIALOGS');
        let aEvent = DeviceEventEmitter.addListenter('android.intent.action.CLOSE_SYSTEM_DIALOGS', (data) => {
            console.log(JSON.stringify(data));
        });
    }

    componentWillUnmount() {
        RNBroadCast.removeAll();
    }

    renderPage(): null {
        return (
            <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
                <Label text={'某云播放器'} size={'xl'} type={'danger'}/>
                <Label text={' '} size={'xl'} type={'danger'}/>
                <Label text={'播放'} size={'xl'} type={'danger'} onPress={() =>
                    RNBroadCast.sendBroadCast('MyAPP.play', {songName: 'XXX'})}/>
                <Label text={'暂停'} size={'xl'} type={'danger'}/>
                <Label text={'读取'} size={'xl'} type={'detail'}/>
            </View>
        );
    }
}


           

可以调用Native中的服务哟

继续阅读