对于app来讲,不少时候都须要在本app中唤起其余app应用,本文简单的列举了安卓和react-native对于唤起其余app的一些作法。html
使用场景:react
安卓的url唤起实际上是采用了url scheme方式唤起,该方式和日常的url的结构基本一致;android
scheme://host:port/pathname?querystring; // 注意这里的scheme不是http护着https; // demo: test//baidu.com:9001/a/b?a1=1&a2=2
在B应用的AndroidManifest.xml文件中定义相关配置:web
B应用获取跳转过来的参数:react-native
Intent intent = getIntent(); String action = intent.getAction(); String scheme = intent.getScheme(); Set categories = intent.getCategories(); Uri data = intent.getData(); data.getHost(); data.getPath(); data.getPort();
A应用若是是网页:api
<a href='test://baidu.com:9001/a/b?...'>xxx</a> window.location = "test://baidu.com:9001/a/b?...";
A是一个app应用:
首先要知道B应用的url scheme的叫什么,而后再调用startActivity唤起;promise
Uri uri = Uri.parse("test://baidu.com:9001/a/b?a1=1&a2=2"); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent);
优势:不暴露包命;
缺点:host path schemeA应用和B应用得规定死;app
step1:建立一个应用类组件--- new ComponentName(string pkg, string classname);其中
pkg:就是AndroidManifest.xml中manifest组件定义的package的包名;
classname:就是xml文件中activity组件定义的android:name的类名;url
step2:建立一个意图Intent,而后添加组件到意图中;spa
Intent intent = new Intent() intent.setComponent(intent)
step3:最后一步就是启动另外一个应用 ---- startActivity(intent)
Intent intent = new Intent(Intent.ACTION\_MAIN); /**知道要跳转应用的包命与目标Activity**/ ComponentName componentName = new ComponentName("kuyu.com.xxxx", "kuyu.com.xxxx.xxx.login.WelcomeActivity"); intent.setComponent(componentName); intent.putExtra("", ""); //这里Intent传值 startActivity(intent);
rn能够经过Linking这个组件来唤起其余app应用(前提要知道对应app应用的url scheme),他主要提供了三个主要的api调用:
1.canOpenURL(url):判断当前url scheme是否在安卓机器上有安装过;
Linking.canOpenURL('weixin://') .then(isSupport => { ... }) .catch(e => {...})
2.openURL(url):在安卓上打开指定的url scheme的app应用;
Linking.canOpenURL('weixin://') .then(isSupport => { if (isSupport) { return Linking.openURL('weixin://'); } else { ... } }).catch(e => {...})
注意:本方法会返回一个Promise对象。若是用户在弹出的对话框中点击了确认或是 url 自动打开了,则 promise 成功完成。若是用户在弹出的对话框中点击取消或是没有对应应用能够处理此类型的 url,则 promise 会失败。
3.getInitialURL():若是本应用被另外一个应用调用时,会返回相应的链接地址;
genInitialURL().then(url => {...}).catch(e => {...});
web site:
使用Linking唤醒其它app及WebView
Intent属性详解一 component属性
带你了解Android的Scheme协议