重定向至“从网站访问Android应用程序”诊断树

Redirect to Android App from a Website?

本文关键字:应用程序 Android 诊断 从网站访问 访问 网站 重定向      更新时间:2023-09-26

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

  1. 如果安装了应用程序mycolapp,则使用自定义url方案mycolapp将用户重定向到该应用程序://
  2. 如果未安装应用程序,请留在当前页面,而不发布或重定向到未知页面或弹出错误

情况1很容易你可以使用以下代码:

window.location = "mycoolapp://"; 

如果在设备上安装了应用程序,这将起作用。当应用程序未安装时,它会将用户重定向到未知的空白页面。

对于案例2,我有一个使用iFrame的解决方案,它在iOS和原生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上运行的应用程序

编辑:我知道您可以使用Intent

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

这不是我想要的,因为如果没有安装应用程序,它会在谷歌播放上启动应用程序页面。有没有一种方法不会重定向到谷歌播放?

 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。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
        }
    }

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