mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-23 11:57:46 +08:00
Android: Adding sendIntent on Linking module (#22302)
Summary: This PR implements "Add a standardized way to send intents on Android" discussed in https://github.com/react-native-community/discussions-and-proposals/issues/34. Pull Request resolved: https://github.com/facebook/react-native/pull/22302 Differential Revision: D13374186 Pulled By: cpojer fbshipit-source-id: 2f0b9b9f46e99f382b6c35b1914e75df23a7fd74
This commit is contained in:
committed by
Facebook Github Bot
parent
cc4211c72f
commit
e8a6cb5e18
@@ -12,9 +12,11 @@
|
||||
const React = require('react');
|
||||
const {
|
||||
Linking,
|
||||
Platform,
|
||||
StyleSheet,
|
||||
Text,
|
||||
TouchableOpacity,
|
||||
ToastAndroid,
|
||||
View,
|
||||
} = require('react-native');
|
||||
|
||||
@@ -46,20 +48,56 @@ class OpenURLButton extends React.Component<Props> {
|
||||
}
|
||||
}
|
||||
|
||||
class SendIntentButton extends React.Component<Props> {
|
||||
handleIntent = async () => {
|
||||
try {
|
||||
await Linking.sendIntent(this.props.action, this.props.extras);
|
||||
} catch (e) {
|
||||
ToastAndroid.show(e.message, ToastAndroid.LONG);
|
||||
}
|
||||
};
|
||||
|
||||
render() {
|
||||
return (
|
||||
<TouchableOpacity onPress={this.handleIntent}>
|
||||
<View style={[styles.button, styles.buttonIntent]}>
|
||||
<Text style={styles.text}>{this.props.action}</Text>
|
||||
</View>
|
||||
</TouchableOpacity>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class IntentAndroidExample extends React.Component {
|
||||
static title = 'Linking';
|
||||
static description = 'Shows how to use Linking to open URLs.';
|
||||
|
||||
render() {
|
||||
return (
|
||||
<RNTesterBlock title="Open external URLs">
|
||||
<OpenURLButton url={'https://www.facebook.com'} />
|
||||
<OpenURLButton url={'http://www.facebook.com'} />
|
||||
<OpenURLButton url={'http://facebook.com'} />
|
||||
<OpenURLButton url={'fb://notifications'} />
|
||||
<OpenURLButton url={'geo:37.484847,-122.148386'} />
|
||||
<OpenURLButton url={'tel:9876543210'} />
|
||||
</RNTesterBlock>
|
||||
<View>
|
||||
<RNTesterBlock title="Open external URLs">
|
||||
<OpenURLButton url={'https://www.facebook.com'} />
|
||||
<OpenURLButton url={'http://www.facebook.com'} />
|
||||
<OpenURLButton url={'http://facebook.com'} />
|
||||
<OpenURLButton url={'fb://notifications'} />
|
||||
<OpenURLButton url={'geo:37.484847,-122.148386'} />
|
||||
<OpenURLButton url={'tel:9876543210'} />
|
||||
</RNTesterBlock>
|
||||
{Platform.OS === 'android' && (
|
||||
<RNTesterBlock title="Send intents">
|
||||
<SendIntentButton action="android.intent.action.POWER_USAGE_SUMMARY" />
|
||||
<Text style={styles.textSeparator}>
|
||||
Next one will crash if Facebook app is not installed.
|
||||
</Text>
|
||||
<SendIntentButton
|
||||
action="android.settings.APP_NOTIFICATION_SETTINGS"
|
||||
extras={[
|
||||
{'android.provider.extra.APP_PACKAGE': 'com.facebook.katana'},
|
||||
]}
|
||||
/>
|
||||
</RNTesterBlock>
|
||||
)}
|
||||
</View>
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -70,9 +108,15 @@ const styles = StyleSheet.create({
|
||||
backgroundColor: '#3B5998',
|
||||
marginBottom: 10,
|
||||
},
|
||||
buttonIntent: {
|
||||
backgroundColor: '#009688',
|
||||
},
|
||||
text: {
|
||||
color: 'white',
|
||||
},
|
||||
textSeparator: {
|
||||
paddingBottom: 8,
|
||||
},
|
||||
});
|
||||
|
||||
module.exports = IntentAndroidExample;
|
||||
|
||||
Reference in New Issue
Block a user