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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25643272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:36:25  来源:igfitidea点击:

Redirect to Android App from a Website?

javascriptandroidhtmlredirect

提问by confile

When a user opens a website I want one of the following two cases to be happen:

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

  1. If the app mycoolapp is installed redirect the user to the app using the custom url scheme mycoolapp://
  2. If the app is not installed stay on the current page without posting or redirecting to an unknown page or an error popup.
  1. 如果安装了应用程序 mycoolapp,则使用自定义 url 方案 mycoolapp:// 将用户重定向到应用程序
  2. 如果未安装该应用程序,请留在当前页面上,而不会发布或重定向到未知页面或错误弹出窗口。

Case 1 is easy you can use the following code:

情况 1 很简单,您可以使用以下代码:

window.location = "mycoolapp://"; 

This will work if the app is installed on the device. When the app is not installed it redirects the user to a blank page which is unknown.

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

For case 2 I have a solution using an iFrame which works great on iOS and on the native Android browser. It does not work on Chrome for Android.

对于案例 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://');

When the app is installed it is working on the native Android browser, but on Chrome for Android there is not redirect. Nothing happens on the page.

安装该应用程序后,它可以在本机 Android 浏览器上运行,但在 Android 版 Chrome 上没有重定向。页面上没有任何反应。

How can I make redirect to my app working on Chrome for Android without redirecting to a blank page when the app is not installed?

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

Edit:I know that you can use an Intent

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

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

This is not what I want because it launches the app page on google play if the app is not installed. Is there a way that it will not redirect to google play?

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

回答by CodingIntrigue

You don't needto launch your app on a custom protocol. It will work for anyaddress, e.g. in your AndroidManifest.xml:

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

This means you can direct the user using:

这意味着您可以使用以下命令指导用户:

window.location = "/Android";

If the app is installed, Android will prompt to "Open With". If not, the user will just be taken to the /Android page in their browser.

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

回答by santhosh rb

 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>

in html give the code, the scheme is same as in the manifest file.

在html中给出代码,方案与清单文件中的相同。

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

if you want to add parameter to your app use this

如果您想向您的应用程序添加参数,请使用它

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

if the app is not installed so if you want to redirect to some other page add the fallback url like this. the url must be encoded

如果该应用程序未安装,所以如果您想重定向到其他页面,请添加这样的回退 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>

to get the parameter add below code

获取参数添加下面的代码

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