-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.nativeapi.js
More file actions
165 lines (146 loc) · 4.09 KB
/
test.nativeapi.js
File metadata and controls
165 lines (146 loc) · 4.09 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* Created by tiger on 16/6/9.
*/
import React, {
Component
} from 'react';
import {
AppRegistry,
StyleSheet,
View,Text,
ScrollView,
ToastAndroid,
TouchableHighlight,
Alert,
AppState,
NetInfo,
AsyncStorage,
Dimensions,
PixelRatio,
BackAndroid,
Linking,
} from 'react-native';
var {NativeModules} = require('react-native');
class CustomButton extends Component {
render() {
return (
<TouchableHighlight
style={styles.button}
underlayColor="#a5a5a5"
onPress={this.props.onPress}>
<Text style={styles.buttonText}>{this.props.text}</Text>
</TouchableHighlight>
);
}
}
var backCount = 2;
var url = "http://wap.sogou.com";
var schema = "awesome://test";
class NativeAPIDemo extends Component {
// 构造
constructor(props) {
super(props);
this._handleAppStateChange = this.handleAppStateChange.bind(this);
// 初始状态
this.state = {
_appState: AppState.currentState
};
}
componentWillMount() {
AppState.addEventListener('change', this._handleAppStateChange);
}
componentWillUnMount() {
AppState.addEventListener('change', this._handleAppStateChange);
}
handleAppStateChange(appState) {
//ToastAndroid.show("Current AppState : " + appState, ToastAndroid.SHORT);
}
componentDidMount() {
BackAndroid.addEventListener(BackAndroid.DEVICE_BACK_EVENT, function () {
if (backCount >= 1) {
ToastAndroid.show('back key.', ToastAndroid.SHORT);
backCount--;
return true;
} else {
return false;
}
});
NativeModules.IntentModule.dataToJS((msg) => {
ToastAndroid.show("Intent Data : " + msg, ToastAndroid.SHORT);
},
(result) => {
ToastAndroid.show("Intent Error : " + result, ToastAndroid.SHORT);
});
}
openLink() {
//Linking.canOpenURL(url).then(support => {
// if(support){
// Linking.openURL(url);
// } else {
// console.log('Can not open url : ' + url);
// }
//});
// test schema
Linking.canOpenURL(schema).then(support => {
if (support) {
Linking.openURL(schema);
} else {
console.log('Can not open url : ' + schema);
}
});
}
render() {
return (
<View>
<Text>Current AppState : {this.state._appState}</Text>
<Text>Screen Width : {Dimensions.get('window').width}</Text>
<Text>Screen Height : {Dimensions.get('window').height}</Text>
<Text>Screen DPI : {PixelRatio.get()}</Text>
<CustomButton
text="弹出Alert"
onPress={() => Alert.alert("提示","测试Alert",
[
{text:'Cancel', onPress:()=> ToastAndroid.show("Cancel is clicked.", ToastAndroid.SHORT)},
{text:'Ok', onPress:()=> ToastAndroid.show("Ok is clicked.", ToastAndroid.SHORT)}
])}/>
<CustomButton
text="Open Link"
onPress={this.openLink}/>
<CustomButton
text="点击自定义Toast方法"
onPress={() => NativeModules.ToastCustomAndroid.show("CustomToastAndroid",
NativeModules.ToastCustomAndroid.SHORT)}/>
<CustomButton
text="点击测试封装方法"
onPress={() => NativeModules.ToastCustomAndroid.measureLayout(
(msg) => {ToastAndroid.show(msg, ToastAndroid.SHORT)},
(x,y,w,h) => {ToastAndroid.show(x + "," + y + "," + w + "," + h, ToastAndroid.SHORT)}
)}/>
<CustomButton
text="点击跳转到TestActivity界面"
onPress={() => NativeModules.IntentModule.startActivityFromJS("com.awesomeproject.TestActivity", "startActivityFromJS.")}
/>
<CustomButton
text="点击跳转到Activity界面,并且等待数据返回..."
onPress={
() => NativeModules.IntentModule.startActivityForResultFromJS("com.awesomeproject.TestActivity", 200,
(msg) => {
ToastAndroid.show('SetResult Data :' + msg, ToastAndroid.SHORT);
},
(result) => {
ToastAndroid.show('Error : ' + result, ToastAndroid.SHORT);
})}/>
</View>
);
}
}
const styles = StyleSheet.create({
button: {
margin: 5,
backgroundColor: 'white',
padding: 15,
borderBottomWidth: StyleSheet.hairlineWidth,
borderBottomColor: '#cdcdcd'
}
});
AppRegistry.registerComponent("NativeAPIDemo", () => NativeAPIDemo);