简体   繁体   English

Intent-filter无法在浏览器中运行

[英]Intent-filter not working from browser

I'm using an Activity with an intent filter similar to the one described here to be able to intercept clicks in the browser and give the user the option to open the my app instead. 我正在使用具有类似于此处描述的意图过滤器的Activity,以便能够拦截浏览器中的点击并为用户提供打开我的应用程序的选项。 Here's the code from my AndroidManifest.xml : 这是我的AndroidManifest.xml的代码:

<activity android:name="com.scompt.ScomptIntentFilter">
    <intent-filter>
        <data android:scheme="http" android:host="www.scompt.com" />
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
    </intent-filter>
</activity>

This isn't working if I enter http://www.scompt.com into the browser. 如果我在浏览器中输入http://www.scompt.com则无效。 The page is loaded, just like normal. 正常加载页面。

If I enter either of the following commands on the command line, then I'm given the standard chooser between my app and the browser, as I would expect. 如果我在命令行中输入以下任一命令,那么我会在我的应用程序和浏览器之间给出标准选择器,正如我所料。

adb -d shell am start -d http://www.scompt.com -a android.intent.action.VIEW
adb -d shell am start -d http://www.scompt.com

Is there any other place I should look to get this to work? 是否还有其他地方可以让它发挥作用? I've verified what I'm doing with the open-source hubroid and I seem to be doing the same thing. 我已经验证了我正在使用开源的hubroid做什么,我似乎在做同样的事情。

With regards to intents there is a distinct difference between a user typing a URL into their browser and following a link in a web page, email etc. 关于意图,用户在浏览器中键入URL并跟随网页中的链接,电子邮件等之间存在明显差异。

An intent WILL be fired as you expect if you follow a link on a browser web page or a link in an email. 如果您关注浏览器网页上的链接或电子邮件中的链接,则会按预期触发意图。

However, when the user types a URL into the address bar of their browser Android believes that the user specifically wants that URL to open in the browser and therefore does not fire an intent. 但是,当用户在其浏览器的地址栏中键入URL时,Android认为用户特别希望该URL在浏览器中打开,因此不会触发意图。

So if you want to test URLs and intent filters then you need to setup a static web page with hyperlinks on it or send an email to your phone. 因此,如果您要测试URL和目标过滤器,则需要设置一个带有超链接的静态网页,或者向您的手机发送电子邮件。

When you think about it this system works in a similar fashion to the default browser behaviour in Windows. 当您考虑它时,此系统的工作方式与Windows中的默认浏览器行为类似。 Imagine your default browser is Chrome but you also have Firefox installed. 想象一下,您的默认浏览器是Chrome,但您也安装了Firefox。 If you click a link in an email it will open in Chrome. 如果您点击电子邮件中的链接,它将在Chrome中打开。 If however you open Firefox and type a URL into the address bar and hit go it DOES NOT open in Chrome because clearly you want the URL to open in Firefox. 但是,如果您打开Firefox并在地址栏中输入一个URL并点击它就不能在Chrome中打开,因为很明显您希望在Firefox中打开该URL。

http://example.com isn't http://www.example.com . http://example.com不是http://www.example.com

Try typing http://www.example.com into the browser and see if that works. 尝试在浏览器中键入http://www.example.com ,看看是否有效。

This is the manifest code that works for me: 这是适用于我的清单代码:

<activity
    android:name="MY_APP"
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <data
            android:host="MY_HOST"
            android:scheme="MY_SCHEME" />
    </intent-filter>
</activity>

I had exactly the same problem as of 28 Jan 2014 with chrome browser. 我和2014年1月28日的Chrome浏览器有完全相同的问题。 check my answer here 在这里查看我的答案

Launch custom android application from android browser 从Android浏览器启动自定义Android应用程序

add pathPatterns to data tag: 将pathPatterns添加到数据标记:

            <data
                android:host="www.example.com"
                android:scheme="http"
                android:pathPattern=".*">
            </data>

            <data
                android:host="example.com"
                android:scheme="http"
                android:pathPattern=".*">
            </data>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM