【问题标题】:Redirect to Android App from a Website?从网站重定向到 Android 应用程序?
【发布时间】:2014-10-27 21:24:55
【问题描述】:

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

  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 上没有重定向。页面上什么也没有发生。

如何在未安装应用时重定向到我在 Chrome for Android 上运行的应用而不重定向到空白页面?

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

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

这不是我想要的,因为如果未安装应用程序,它会在 google play 上启动应用程序页面。有没有办法不重定向到google play?

【问题讨论】:

  • 在 android 上不要使用“特殊”方案...在 Intent 过滤器中使用 http 方案...浏览器会要求用户使用浏览器或您的应用程序(如果已安装)完成操作跨度>
  • @Selvin 你能回答你的想法吗?
  • 和RGraham的回答一样

标签: javascript android html redirect


【解决方案1】:
 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中给出代码,scheme与manifest文件中的相同。

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 我使用自定义 url,因为我对 iO 使用相同的 url 方案,这让事情变得更容易。
    • @confile 快速浏览浏览器。使用您在 iOS 和 Android 上所拥有的。
    • 如果应用没有安装 window.location = "/Android";将在浏览器中打开一个空白页面。这不是我想要的。如果未安装应用程序,用户应留在当前页面。
    猜你喜欢
    • 2020-05-17
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 2012-11-11
    • 1970-01-01
    • 2018-12-11
    • 2020-04-22
    • 1970-01-01
    相关资源
    最近更新 更多