javascript - 通过 Chrome a href 打开 Android 应用程序 - 单击事件

标签 javascript android google-chrome android-intent intentfilter

我正在开发 Android 应用程序,并尝试使用浏览器中的 a href 来打开我的应用程序。我尝试了几次并发现了一些有趣的东西(或者只是我不够聪明)。

当我检测到用户使用Android Mobile时,我将触发href的点击事件,然后我的应用程序应该打开。 Firefox 工作正常,Chrome 确实触发了点击事件,但什么也不做。当我自己(用手指)单击 a href 时,它起作用了。所以现在我不知道 chrome 是否不能让我通过代码打开我的应用程序,或者只是我使用了错误的方式。

此外,当我将 href 更改为“https://www.google.com ”时,Firefox 和 Chrome 都可以工作。

<a id="goToMyApp" href="intent://myHost/#Intent;package=myPackage;scheme=https;end;">
click me</a>
<script type="text/javascript">
    var goToMyApp = document.getElementById("goToMyApp");
    if (navigator.userAgent.match(/Android/)) {
        setTimeout("goToMyApp.click()",1000);
    }
</script>

这是我在 AndroidManifest.xml 中的 Intent 过滤器,但我不认为存在问题。

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT"/>
    <category android:name="android.intent.category.BROWSABLE"/>
    <data android:scheme="https" android:host="myHost" android:path="/"/>
</intent-filter>

有人对此有任何想法吗?

编辑:

我在这里再次阅读了有关 Android Intents with Chrome 的文档:https://developer.chrome.com/multidevice/android/intents终于看到了页面底部的评论。

Chrome doesn’t launch an external app for a given Intent URI in the following cases.

  • When the Intent URI is redirected from a typed in URL.
  • When the Intent URI is initiated without user gesture. (This one is what I trying to to do)

我认为这是因为安全政策,Chrome不会让页面通过代码启动 Intent 。这就解释了为什么我可以通过手势打开我的应用程序,但不能通过代码打开我的应用程序。

我会留下这个问题,希望它可以帮助其他尝试做同样事情的人。

最佳答案

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

答案在这里有更详细的解释:https://appstudio.ca/blog/index.php/2017/09/11/open-the-android-app-using-the-link/

关于javascript - 通过 Chrome a href 打开 Android 应用程序 - 单击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46155522/

相关文章:

java - 使用 .java 的复选框首选项

java - 禁用 Chrome 通知 (Selenium)

javascript - 如何告诉您网站的 chrome 语言(作为翻译栏的提示)

Javascript 变量意外未定义

javascript - asp.net 中 css 和 js 的 GZIP 压缩

android - Glide 不加载真实图像并卡在占位符上

java - Android 主页按钮、计时器问题

google-maps - 地理位置在 Chrome 中不起作用

javascript - 如果选择标签上没有选定的数据,则显示消息

javascript - 如何保持响应式移动菜单处于事件状态?