-
Notifications
You must be signed in to change notification settings - Fork 175
/
index.android.js
101 lines (90 loc) · 2.52 KB
/
index.android.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
'use strict'
import React, { Component } from 'react';
import Storage from 'react-native-storage';
import { AsyncStorage } from 'react-native';
import {
AppRegistry,
StyleSheet,
Text,
Image,
View,
NativeModules,
ToastAndroid,
DeviceEventEmitter
} from 'react-native';
let title = 'React Native界面';
export default class HotRN extends Component {
componentWillMount() {
let result = NativeModules.commModule.Constant;
console.log('原生端返回的常量值为:' + result);
}
/**
* 接收原生调用
*/
componentDidMount() {
DeviceEventEmitter.addListener('nativeCallRn',(msg)=>{
title = "React Native界面,收到数据:" + global.patchImgNames;
ToastAndroid.show("发送成功", ToastAndroid.SHORT);
})
}
/**
* 调用原生代码
*/
skipNativeCall() {
let phone = '18637070949';
NativeModules.commModule.rnCallNative(phone);
}
/**
* Callback 通信方式
*/
callbackComm(msg) {
NativeModules.commModule.rnCallNativeFromCallback(msg,(result) => {
ToastAndroid.show("CallBack收到消息:" + result, ToastAndroid.SHORT);
})
}
/**
* Promise 通信方式
*/
promiseComm(msg) {
NativeModules.commModule.rnCallNativeFromPromise(msg).then(
(result) =>{
ToastAndroid.show("Promise收到消息:" + result, ToastAndroid.SHORT)
}
).catch((error) =>{console.log(error)});
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome} >
{title}
</Text>
<Text style={styles.welcome} onPress={this.skipNativeCall.bind(this)}>
跳转到拨号界面
</Text>
<Text style={styles.welcome} onPress={this.callbackComm.bind(this,'callback发送啦')}>
Callback通信方式
</Text>
<Text style={styles.welcome} onPress={this.promiseComm.bind(this,'promise发送啦')}>
Promise通信方式
</Text>
<Image source={require('./images/ic_1.png')} />
<Image source={require('./images/ic_2.png')} />
<Image source={require('./images/ic_4.png')} />
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#FFFFFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
}
});
AppRegistry.registerComponent('HotRN', () => HotRN);