在手機的瀏覽器上通過串連開啟App

來源:互聯網
上載者:User

標籤:

Android系統中實現

 

1、在系統系統內建的瀏覽器中

首先做成HTML的頁面,頁面內容格式如下:

<a href="[scheme]://[host]/[path]?[query]">啟動應用程式</a> 

這一句就可以了。

各個項目含義如下所示:

scheme:判別啟動的App。 ※詳細後述

host:適當記述

path:傳值時必須的key     ※沒有也可以

query:擷取值的Key和Value  ※沒有也可以

 

作為測試好好寫了一下,如下:

<a href="myapp://jp.app/openwith?name=zhangsan&age=26">啟動應用程式</a>  

 

接下來是Android端。
首先在AndroidManifest.xml的MAIN Activity下追加以下內容。(啟動Activity時給予)

※必須添加項

<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="myapp" android:host="jp.app" android:pathPrefix="/openwith"/>  </intent-filter>

HTML記述的內容加入<data …/>。
其中必須的內容僅scheme,沒有其他內容app也能啟動。

※注意事項:intent-filter的內容【android.intent.action.MAIN】和 【android.intent.category.LAUNCHER】這2個,不能與這次追加的內容混合。
                 所以,如果加入了同一個Activity,請按以下這樣做,否則會導致應用表徵圖在案頭消失等問題。

<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="myapp" android:host="jp.app" android:pathPrefix="/openwith"/>  </intent-filter> 

這樣的話,沒有問題。

 

接下來在Activity中需要取值的地方添加以下代碼,我是直接寫在OnCreate函數裡的:

 Intent i_getvalue = getIntent();  
String action = i_getvalue.getAction();  
  
if(Intent.ACTION_VIEW.equals(action)){  
    Uri uri = i_getvalue.getData();  
    if(uri != null){  
        String name = uri.getQueryParameter("name");  
        String age= uri.getQueryParameter("age");  
    }  
}

這樣就能擷取到URL傳遞過來的值了。

2、在第三方的瀏覽器中

把一個http服務宿主在本地應用中,本地的服務地址為127.0.0.1:8765中,宿主用於監控服務資料,並開啟自身。

3、在中開啟

在開放平台登記應用之後,可以獲得appid,通過這個appid就可以跳轉到你的app。
iOS平台格式如下:appid://openwebview/?ret=0,appid要替換成實際的,後面可以帶參數,在你的app可以接收到。
例如:location.href = wx234ad233ae222://openwebview/?ret=0

IOS系統中實現(沒有蘋果裝置,只能拿mac類比)

 

1、在系統內建的瀏覽器
  // To avoid the "protocol not supported" alert, fail must open another app.              var appstore = "itms://itunes.apple.com/us/app/facebook/id284882215?mt=8&uo=6";              function applink(fail){                  return function(){                      var clickedAt = +new Date;                      // During tests on 3g/3gs this timeout fires immediately if less than 500ms.                      setTimeout(function(){                                // To avoid failing on return to MobileSafari, ensure freshness!                                if (+new Date - clickedAt < 2000){                                window.location = fail;                                }                                }, 500);                      };              }              document.getElementById("applink1").onclick = applink(appstore);              document.getElementById("applink2").onclick = applink(appstore); 
 

其原理就是為HTML頁面中的超連結點擊事件增加一個setTimeout方法.

如果在iPhone上面500ms內,本機有應用程式能解析這個協議並開啟程式,則這個回調方法失效;如果本機沒有應用程式能解析該協議或者500ms內沒有開啟個程式,則執行setTimeout裡面的function,就是跳轉到apple的itunes。

2、在第三方的瀏覽器中

網易雲音樂的地址,直接就能開啟雲音樂

https://itunes.apple.com/app/id590338362

3、在中開啟

在開放平台登記應用之後,可以獲得appid,通過這個appid就可以跳轉到你的app。
iOS平台格式如下:appid://openwebview/?ret=0,appid要替換成實際的,後面可以帶參數,在你的app可以接收到。
例如:location.href = wx234ad233ae222://openwebview/?ret=0

在手機的瀏覽器上通過串連開啟App

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.