Javascript 从网站重定向到Android应用程序?

Javascript 从网站重定向到Android应用程序?,javascript,android,html,redirect,Javascript,Android,Html,Redirect,当用户打开网站时,我希望发生以下两种情况之一: 如果安装了应用程序mycolapp,请使用自定义url方案mycolapp将用户重定向到该应用程序:// 如果未安装应用程序,请保持在当前页面,而不发布或重定向到未知页面或错误弹出窗口 案例1很简单,您可以使用以下代码: window.location = "mycoolapp://"; 如果设备上安装了应用程序,则此功能将正常工作。未安装应用程序时,它会将用户重定向到未知的空白页 对于案例2,我有一个使用iFrame的解决方案,它在iOS和本

当用户打开网站时,我希望发生以下两种情况之一:

  • 如果安装了应用程序mycolapp,请使用自定义url方案mycolapp将用户重定向到该应用程序://
  • 如果未安装应用程序,请保持在当前页面,而不发布或重定向到未知页面或错误弹出窗口
  • 案例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浏览器上运行,但在Chrome for Android上没有重定向。页面上没有任何内容

    当应用程序未安装时,如何在不重定向到空白页面的情况下重定向到在Chrome for Android上运行的应用程序?

    编辑:我知道你可以使用意图

    window.location = "intent://#Intent;package=com.mycoolapp;scheme=mycoolapp;launchFlags=268435456;end;";
    
    这不是我想要的,因为如果没有安装应用程序,它会在google play上启动应用程序页面。有没有一种方法可以使它不重定向到google play?

    您不需要使用自定义协议启动应用程序。它适用于任何地址,例如在您的
    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>
    

    如果安装了应用程序,Android将提示“使用打开”。如果没有,用户将被带到浏览器中的/Android页面。

    在Android上不要使用“特殊”方案。。。在意向筛选器中使用http方案。。。浏览器将要求用户使用浏览器或您的应用程序(如果已安装)完成操作@Selvin您能为您的想法发布一个答案吗?这与RGraham的回答相同。我使用自定义url,因为我在iOs上使用相同的url方案。这使事情变得更简单。@confile快速浏览一下。使用iOS和以上版本的Android应用程序。如果未安装应用程序,请使用window.location=“/Android”;将在浏览器中打开空白页。这不是我想要的。如果未安装应用程序,用户应停留在当前页面。
     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>
    
     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>
    
    <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>
    
    <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
            }
        }