React native之IntentAndroid学习
来源:互联网 发布:淘宝童装小模特 编辑:程序博客网 时间:2024/05/16 17:27
IntentAndroid是RN提供的一个通用接口来调用外部链接,IntentAndroid有如下方法:
static openURL(url: string) 根据给定的URL启动对应的应用。static canOpenURL(url: string, callback: Function) 判断是否有已安装的应用可以处理传入的URL。static getInitialURL(callback: Function) 如果当前应用是通过深度链接和{@code Intent.ACTION_VIEW}调起的,则此方法会返回这个链接的值,否则返回null
编写OpenURLButton组件
在index.android.js相同目录下,新建一个openurl.js文件,内容如下:
'use strict';var React = require('react-native');var { IntentAndroid, StyleSheet, Text, TouchableNativeFeedback, AppRegistry, View, ToastAndroid,} = React;var OpenURLButton = React.createClass({ propTypes: { url: React.PropTypes.string, }, handleClick: function() { IntentAndroid.canOpenURL(this.props.url, (supported) => { //判断当前uri是否可以打开 if (supported) { IntentAndroid.openURL(this.props.url); } else { ToastAndroid.show("不能识别当前uri",ToastAndroid.SHORT); } }); }, //返回当前button组件显示的视图,这里是一个TouchableNativeFeedback render: function() { return ( <TouchableNativeFeedback onPress={this.handleClick}> <View style={styles.button}> <Text style={styles.text}>Open {this.props.url}</Text> </View> </TouchableNativeFeedback> ); }});var styles = StyleSheet.create({ container: { flex: 1, backgroundColor: 'white', padding: 10, paddingTop: 30, }, button: { padding: 10, backgroundColor: '#3B5998', marginBottom: 10, }, text: { color: 'white', },});// 导出当前OpenURLButton组件module.exports = OpenURLButton;
编写测试应用
这里,我编写了一个简单的测试应用,其androidManifest.xml主要内容如下:
<activity android:name="com.example.htmllauncher.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:host="haha" android:pathPrefix="/open" android:scheme="testapp" /> </intent-filter> </activity>
具体可以参考我之前的博客:android中通过浏览器启动nativeAPP
使用OpenURLButton组件
这里,由于将OpenURLButton单独使用文件进行封装,所以代码量就较少了,index.android.js内容如下:
'use strict';var React = require('react-native');var { AppRegistry,} = React;var UIExplorerBlock = require('./UIExplorerBlock');var OpenURLButton = require('./openurl');var secondProject = React.createClass({ render: function() { return ( <UIExplorerBlock title="Open external URLs"> <OpenURLButton url={'https://www.baidu.com'} /> <OpenURLButton url={'testapp://haha/open'} /> <OpenURLButton url={'testapp://haha/open?name=lisi&age=30&from=ucbroswer'} /> <OpenURLButton url={'testapp://AA/BB'} /> </UIExplorerBlock> ); },});AppRegistry.registerComponent('secondProject', () => secondProject);
看效果吧:
0 0
- React native之IntentAndroid学习
- React Native Android 组件IntentAndroid 实现拨打电话
- react native之API学习
- react native学习之Toast
- 学习react-native之Navigator
- React-native 之 AsyncStorage学习
- React Native 学习之路
- react-native 学习之旅
- React Native 学习之动画
- React Native 学习 之JSX
- React-Native学习笔记之React-Native升级
- react native组件学习之listview
- React-Native 学习之 Flex布局
- React Native学习之JavaScript语法转换器
- React Native学习之自定义一个组件
- React Native学习之TabBarIOS用法
- React-native学习笔记之<BackAndroid>
- react-native学习笔记之<TextInput>
- jclouds项目:在云环境中简化开发流程
- webstorm-主题和配色
- servlet之getRealPath获取路径问题。
- My blog
- 1046. 划拳(15)
- React native之IntentAndroid学习
- 学习老外用webstorm开发nodejs的技巧--代码提示DefinitelyTyped
- 基于java社会化海量数据采集爬虫框架搭建
- Log4j2介绍和特性实例(七)--代码中指定日志文件的名字
- 【工具】使用VS内置的单元测试
- Lua封装格式化html
- PAT 1081. Rational Sum (20)
- Lua封装格式化float
- 检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件时失败