Android 從網頁中跳轉到APP並傳遞數據

最近項目中要實現從網頁跳轉回app的功能,查找資料後發現發現,SDK已經爲頁面跳轉回應用提供了基本的數據支持。我們只需在應用裏和被分享的網頁進行簡單的設置,即可實現此功能。

我們先看一下網頁跳轉實現的原理:

就Android平臺而言,URI主要分三個部分:scheme, authority and path。其中authority又分爲host和port。格式如下: 
scheme://host:port/path 
舉個實際的例子: 
content://com.example.project:200/folder/subfolder/etc 
\---------/  \---------------------------/ \---/ \--------------------------/ 
scheme                 host               port        path 
                \--------------------------------/ 
                          authority    

現在大家應該知道data flag中那些屬性的含義了吧,看下data flag 
<data android:host="string" 
      android:mimeType="string" 
      android:path="string" 
      android:pathPattern="string" 
      android:pathPrefix="string" 
      android:port="string" 
      android:scheme="string" /> 

修改ManiFest文件,設置Activity的接收Action的屬性

 <intent-filter>
                <data
                    android:host="192.168.167.33"
                    android:path="/mi-tracker-web/download.html"
                    android:port="8088"
                    android:scheme="howie" />

                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
            </intent-filter>
跳轉的html界面:

<html>
<body>
<a id="applink1" href="howie://192.168.167.33:8088/mi-tracker-web/download.html">Open Application</a>  
</body>
</html>
這樣,當點擊Open Application是就會跳轉到註冊了intent-filter的界面。

有時候我們在跳轉到app時還需要傳過來一些數據,那麼只要把url改成一下的即可:

howie://192.168.167.33:8088/mi-tracker-web/download.html?data=123456&data2=987654
跳轉到app後接收數據:

Intent intent = getIntent();
		String action = intent.getAction();
		if(Intent.ACTION_VIEW.equals(action)){
			Uri uri = intent.getData();
			if(uri != null){
				String data = uri.getQueryParameter("data");
				String data2 = uri.getQueryParameter("data2");
			}
		}





發佈了120 篇原創文章 · 獲贊 14 · 訪問量 48萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章