喚醒方式:一、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也提供了專項功能,畢竟是專門作這個的,兼容性也都經受過市場驗證,懶得本身研究的,能夠直接集成使用,參考下。