唤醒方式:一、URL Schemeshtml
二、android appLinkandroid
三、chrome intentgit
<activity android:name=".ui.activity.SplashActivity" android:exported="true" android:screenOrientation="portrait" android:theme="@style/NormalSplash"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <!--DeepLink h5唤醒app配置--> <intent-filter> <!--ACTION_VIEW:支持被检索--> <action android:name="android.intent.action.VIEW" /> <!--CATEGORY_DEFAULT:响应隐式Intent--> <category android:name="android.intent.category.DEFAULT" /> <!--CATEGORY_BROWSABLE:可被Web浏览器唤起--> <category android:name="android.intent.category.BROWSABLE" /> <!--data:一个或多个,必须含有scheme标签,决定被唤起的URL格式--> <data android:host="app.puxinwangxiao.com" android:scheme="pxwxstudent" /> <!-- <data android:host="app.puxinwangxiao.com" android:scheme="pxwxstudent" android:pathPrefix="/pxwx"/> <data android:host="app.puxinwangxiao.com" android:scheme="pxwxstudent" android:path="/pxwx/user"/> --> </intent-filter> </activity>
注意:App能够配置多个支持唤起的Activitychrome
Activity能够支持被多个URL唤起json
若一个App配置了多个支持唤起的Activity,它们的scheme和host通常一致,而后经过path、pathPrefix等进行定向区分浏览器
Uri数据的解析能够在Activity中经过getIntent().getData()实现安全
@Override public void onCreate(Bundle savesInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash); // 尝试获取WebApp页面上过来的URL Uri uri = getIntent().getData(); if (uri != null) { // scheme部分 String scheme=data.getScheme(); // host部分 String host=data.getHost(); // 访问路径 String path=data.getPath(); //参数 Set<String> paramKeySet=data.getQueryParameterNames(); } }
<!--1.经过a标签打开,点击标签是启动--> <!-- 注意这里的href格式 -- > <a href="pxwxstudent://app.puxinwangxiao.com">open android app</a> <!--2.经过iframe打开,设置iframe.src即会启动--> <iframe src="pxwxstudent://app.puxinwangxiao.com"></iframe> <!--3.直接经过window.location 进行跳转--> window.location.href= "pxwxstudent://app.puxinwangxiao.com";
Intent intent = new Intent(); intent.setData(Uri.parse("pxwxstudent://app.puxinwangxiao.com/")); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent);
Android M以上版本能够经过AppLinks,让用户在点击一个连接时跳转到App的指定页面;前提是这个App已经安装并通过验证。服务器
App Links的最大的做用,就是能够避免从页面唤醒App时出现的选择浏览器选项框;微信
前提是必须注册相应的Scheme,就能够实现直接打开关联的App。app
Android App Links有如下几点好处:
安全性/特殊性:因为Android App Links使用了HTTP/HTTPS URL的方式向开发者的服务器进行链接认证,因此其余应用没法使用咱们的连接无缝的用户体验:当用户未安装咱们的应用时,因为使用的是HTTP/HTTPS URL,会直接打开一个网页,咱们能够在这个网页中展现应用介绍等,而不是显示404或者是其余错误页面
支持Instant Apps:可使用App Links直接打开一个未安装的Instant App
支持Google Search或其余浏览器:用户能够直接在Google Search/Google Assistant/手机浏览器/屏幕搜索中直接经过点击一个URL来打开咱们的指定页面
https://developer.android.com...
建立intent filter
咱们在此处先假设用户是经过http://resource.puxinwangxiao...。
Android Studio 2.3之后提供了App Links Assistant来帮助开发者快速在AndroidManifest.xml中建立须要配置的intent filter,使用App Links Assistant有如下几个步骤:
点击Android Studio的菜单栏中的Tools > App Links Assistant点击Open URL Mapping Editor,而后在对话框底部点击+去添加一个新的URL mapping
在弹出的Add URL Mapping对话框中输入对应的内容,包括Host、Path、Activity, 输入完成后点击OK
注:App Link 支持多个域名
使用App Links Assistant在manifest文件中自动生成的内容以下:
<activity android:name=".ui.activity.SplashActivity" android:exported="true" android:screenOrientation="portrait" android:theme="@style/NormalSplash"> <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:scheme="http" android:host="resource.puxinwangxiao.com" android:path="/pxwx" /> </intent-filter> </activity>
检查URL Mapping是否配置正确
App Links Assistant提供了检查URL Mapping是否配置正确的快捷方式,操做以下:
点击Open URL Mapping Editor,而后在Check URL Mapping对话框中输入URL,当输入一个可以成功匹配到Acitivty的URL后,输入框下方会显示This URL maps to xxxxx(app)
处理App Links进入应用的场景
经过App Links Assistant -> Select Activity选择以前配置的URL对应的Activity, 点击Insert Code便可在onCreate方法中插入获取从App Links跳转而来的URL的代码,生成代码以下
// ATTENTION: This was auto-generated to handle app links. Intent appLinkIntent = getIntent(); String appLinkAction = appLinkIntent.getAction(); Uri appLinkData = appLinkIntent.getData();
检查assetlinks.json是否上传成功
我这里检测后提示Network error.不影响 主要是http://resource.puxinwangxiao...
若是要让网站和不一样的应用关联起来
网站能够在同一个assetlinks.json文件里声明和不一样的app的关系。下面这个文件列出了两个声明,这两个声明声明了网站和两个应用之间的关联,这个文件位于https://app-pre.puxinwangxiao...。
[{ "relation": ["delegate_permission/common.handle_all_urls"], "target": { "namespace": "android_app", "package_name": "com.pxwx.student", "sha256_cert_fingerprints": ["BD:EF:57:3D:01:D0:32:79:6E:32:73:18:32:E2:36:B9:35:1B:9C:7D:0F:F0:B0:A9:BE:91:18:CE:27:1A:D8:4C"] } }, { "relation": ["delegate_permission/common.handle_all_urls"], "target": { "namespace": "android_app", "package_name": "com.pxwx.assistant", "sha256_cert_fingerprints": ["BD:EF:57:3D:01:D0:32:79:6E:32:73:18:32:E2:36:B9:35:1B:9C:7D:0F:F0:B0:A9:BE:91:18:CE:27:1A:D8:4C"] } }]
注意:path、 pathPrefix、 pathPattern 之间的区别
例如: https://app-pre.puxinwangxiao...
在不少应用中须要咱们从浏览器中直接启动应用,大多数采用的是上面提到的第一种scheme的方式,问题是若是手机中没有应用,该url会跳转到一个错误的界面。
google官方在chrome中推出了一种Android Intents的方式来实现应用启动,经过在iframe中设置src为
intent:HOST/URI-path // Optional host #Intent; package=[string]; action=[string]; category=[string]; component=[string]; scheme=[string]; end;
mainfest文件中定义要启动的activity
<activity android:name=".ui.activity.SplashActivity" android:exported="true" android:screenOrientation="portrait" android:theme="@style/NormalSplash"> <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="app.puxinwangxiao.com" android:scheme="pxwxstudent" /> </intent-filter> </activity>
定义一个a标签为
<a href="intent://app.puxinwangxiao.com/#Intent;scheme=pxwxstudent;package=com.xxx.xxx;end">open Android App</a>
在浏览器中点击a标签,就能够启动应用程序的对应activity了.
若是手机中没有相应的应用,防止跳转到错误页面,将a标签设置为
<a href="intent://app.puxinwangxiao.com/#Intent;scheme=pxwxstudent;package=com.xxx.xxx;S.browser_fallback_url=https://www.puxinwangxiao.com;end">open Android App</a>
这样若是没有对应应用,该连接就会跳转到S.browser_fallback_url指定的url上。
URL Scheme只须要原生App开发时注册Scheme便可,用户点击此类连接时,会自动唤醒App,并借助URL Router机制跳转到指定页面。
URL Scheme兼容性高,但却存在许多限制:
因为这些限制的存在,安卓发布了本身的第二套方案:Android的App Links。
三种方案都有各自的兼容性,这几项技术是基于系统平台的,每一个系统版本的迭代后,配置方式都会有新的变化,国内的第三方平台openinstall也提供了专项功能,毕竟是专门作这个的,兼容性也都经受过市场验证,懒得本身研究的,能够直接集成使用,参考下。