2014-09-03 49 views
5

當用戶打開一個網站,我想以下兩種情形之一將發生:從網站重定向到Android應用程序?

  1. 如果應用mycoolapp安裝將用戶重定向到使用自定義URL方案mycoolapp應用://
  2. 如果應用程序未安裝,則保留在當前頁面上,而不發佈或重定向到未知頁面或錯誤彈出窗口。

案例1很容易,你可以使用下面的代碼:

window.location = "mycoolapp://"; 

如果安裝在設備上的應用程序這將工作。當應用程序未安裝時,它將用戶重定向到未知的空白頁面。

對於情況2我有一個使用iFrame的解決方案,該解決方案在iOS和原生Android瀏覽器上效果很好。它不適用於Android版Chrome。

var redirect = function (location) { 
    var iframe = document.createElement('iframe'); 
    iframe.setAttribute('src', location); 
    iframe.setAttribute('width', '1px'); 
    iframe.setAttribute('height', '1px'); 
    iframe.setAttribute('position', 'absolute'); 
    iframe.setAttribute('top', '0'); 
    iframe.setAttribute('left', '0'); 
    document.documentElement.appendChild(iframe); 
    iframe.parentNode.removeChild(iframe); 
    iframe = null; 
};  

redirect('mycoolapp://'); 

安裝應用程序時,它正在使用原生Android瀏覽器,但在Android版Chrome瀏覽器中沒有重定向。頁面上沒有任何反應。

我該如何重定向到在Android版Chrome上工作的應用程序,而不會在應用程序未安裝時重定向到空白頁?

編輯:我知道,你可以使用一個Intent

window.location = "intent://#Intent;package=com.mycoolapp;scheme=mycoolapp;launchFlags=268435456;end;"; 

這不是我想要的,因爲它啓動玩谷歌的應用程序頁面,如果沒有安裝的應用程序。有沒有一種方式,它不會重定向到谷歌播放?

+0

Android上不使用「特殊」的計劃...使用意圖過濾HTTP方案......瀏覽器會問用戶使用瀏覽器或與您的應用程序(如果已安裝)完成操作 – Selvin 2014-09-03 11:39:59

+0

@Selvin您能否爲您的想法發佈一個答案? – confile 2014-09-03 12:42:09

+0

它與RGraham的回答相同 – Selvin 2014-09-03 12:42:39

回答

4

你不需要需要在自定義協議上啓動你的應用程序。它將適用於任何地址,例如。在AndroidManifest.xml

<intent-filter> 
    <category android:name="android.intent.category.DEFAULT" /> 
    <category android:name="android.intent.category.BROWSABLE" /> 
    <data android:host="www.mycoolapp.com" /> 
    <data android:scheme="http" /> 
    <data android:pathPattern="/Android" /> 
</intent-filter> 

這意味着您可以使用直接的用戶:

window.location = "/Android"; 

如果安裝了應用程序,Android將提示「打開方式」。如果沒有,用戶將被帶到瀏覽器中的/ Android頁面。

+0

我使用自定義url,因爲我對iOs使用相同的url方案,這使得事情變得更容易。 – confile 2014-09-03 12:41:27

+0

@confile做一個快速的瀏覽器嗅探。使用你的iOS和Android以上版本。如果應用程序未安裝,則爲 – CodingIntrigue 2014-09-03 12:47:19

+0

window.location =「/ Android」;將在瀏覽器中打開一個空白頁面。這不是我想要的。如果應用程序未安裝,用戶應該停留在當前頁面。 – confile 2014-09-03 12:57:52

0
Add this in manifest file,note the scheme. 

     <intent-filter> 
      <data android:scheme="startci" /> 
      <action android:name="android.intent.action.VIEW" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
      <category android:name="android.intent.category.BROWSABLE" /> 
     </intent-filter> 

在html中給出代碼,方案與清單文件中的方案相同。

<a href="intent:#Intent;scheme=startci;package=gramener.star;end">click here</a> 

,如果你想參數添加到您的應用程序使用此

<a href="intent:#Intent;scheme=startci://open?url_param=hi santhosh;package=gramener.star;end">click here</a> 

如果未安裝應用程序,所以如果你想重定向至其他網頁添加後備網址這樣。該URL必須被編碼

<a href="intent:#Intent;scheme=startci://open?url_param=hi santhosh;package=gramener.star;S.browser_fallback_url=http%3A%2F%2Fzxing.org;end">click here</a> 

得到參數添加以下代碼

Uri data = this.getIntent().getData(); 
    if (data != null && data.isHierarchical()) { 
     if (data.getQueryParameter("url_param") != null) { 
      String param = data.getQueryParameter("url_param");    
      Log.d("the param is",param); 
       //do something here 
     } 
    } 
+0

這是工作按鈕,它從第一個啓動應用程序... – 2017-09-01 11:16:08