从 Android 浏览器 / Chrome 启动自定义 Android 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21663001/
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 04:53:47  来源:igfitidea点击:

Launching custom Android application from Android browser / Chrome

androidandroid-intentintentfilter

提问by Vlad Schnakovszki

First of all, my question is extremely similar to this, thisand this. The Android documentation for what I'm trying to achieve is here. I couldn't get this to work using these resources so please don't mark this as duplicate as it is not.

首先,我的问题与thisthisthis极其相似。我试图实现的 Android 文档在这里。我无法使用这些资源使其正常工作,因此请不要将其标记为重复,因为它不是。

I have a website and an Android application. The user will be able to scan QR codes that contain links like http://mywebsite.com/map/. When the user tries to open this link, I want Android to show him a chooser dialog where he can choose to open that link using my application. If my application is not installed, it should proceed to the specified website.

我有一个网站和一个 Android 应用程序。用户将能够扫描包含http://mywebsite.com/map/ 等链接的二维码。当用户尝试打开此链接时,我希望 Android 向他显示一个选择器对话框,他可以在其中选择使用我的应用程序打开该链接。如果我的应用程序没有安装,它应该继续到指定的网站。

I know Chrome allows this by opening the chooser dialog when the user navigates to that address. For example, try downloading the Stack Exchange appand going to this question in Chrome. It will show this:

我知道 Chrome 通过在用户导航到该地址时打开选择器对话框来允许此操作。例如,尝试下载Stack Exchange 应用程序并在 Chrome 中访问此问题。它会显示这个:

Screenshot of chooser dialog.

Screenshot of chooser dialog.

I have added the following code in AndroidManifest.xml after following the suggestion in the above-mentioned answers:

按照上述答案中的建议,我在 AndroidManifest.xml 中添加了以下代码:

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <action android:name="android.intent.action.SEND" />

    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />

    <data
        android:host="mywebsite.com"
        android:path="/map"
        android:scheme="http" />
    <data
        android:host="mywebsite.com"
        android:path="/animals"
        android:scheme="http" />
    <data
        android:host="mywebsite.com"
        android:path="/articles"
        android:scheme="http" />
</intent-filter>

Also, I have tried adding android:mimeType="text/plain" to data but it didn't help.

另外,我尝试将 android:mimeType="text/plain" 添加到数据中,但没有帮助。

The problem is that when I go to http://mywebsite.com/mapor http://mywebsite.com/map/Chrome just opens the webpage without showing the chooser dialog.

问题是,当我访问http://mywebsite.com/maphttp://mywebsite.com/map/ 时,Chrome 只会打开网页而不显示选择器对话框。

I would like to mention:

我想提一下:

  • following the Android documentation, I have added this code inside one of the activity structures in AndroidManifest.xml. As I am not sure this is the perfect place to add it, I have also tried adding it outside the application structure and directly inside the application structure but it didn't work
  • this is the only code I have implemented for this to work. If something else is needed please let me know. From what I understand, adding a href to the webpage is only needed when using custom schemas
  • I do not want to use a custom schema in order to achieve this
  • I am developing on a Nexus 4, running Android 4.4.2 (latest)
  • 按照 Android 文档,我在 AndroidManifest.xml 的活动结构之一中添加了此代码。由于我不确定这是添加它的最佳位置,我也尝试将它添加到应用程序结构之外并直接添加到应用程序结构内,但它没有用
  • 这是我为使其工作而实现的唯一代码。如果需要其他东西,请告诉我。据我了解,只有在使用自定义模式时才需要向网页添加 href
  • 我不想使用自定义架构来实现这一点
  • 我正在 Nexus 4 上开发,运行 Android 4.4.2(最新)

回答by AndroidGecko

You need to set it up like this :

你需要这样设置:

<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:host="example.com"
            android:pathPrefix="/someresource/"
            android:scheme="http" />
        <data
            android:host="www.example.com"
            android:pathPrefix="/someresource/"
            android:scheme="http" />
    </intent-filter>

Notice that in your case, you would need to use android:pathPrefix instead of android:path.

请注意,在您的情况下,您需要使用 android:pathPrefix 而不是 android:path。

回答by Eric Woodruff

Just to be sure, you should reset the preferences for your app in case you have accidentally set it to always open the link in chrome rather than show the chooser dialog. Once "Always" is used to open the matching uri, it will never show the chooser.

可以肯定的是,您应该重置应用程序的首选项,以防您不小心将其设置为始终在 chrome 中打开链接而不是显示选择器对话框。一旦使用“始终”打开匹配的 uri,它将永远不会显示选择器。

Second, you can have as many data elements in the intent filter as your want, but it is not necessary to repeat information. You can do the same thing like this:

其次,您可以根据需要在意图过滤器中拥有任意数量的数据元素,但没有必要重复信息。你可以做同样的事情:

<data android:host="mywebsite.com"/>
<data android:scheme="http"/>
<data android:path="/map"/>
<data android:path="/animals"/>
<data android:path="/articles"/>

But note that for the path, you can just use a wildcard

但请注意,对于路径,您可以只使用通配符

<data android:path="/.*"/>

Consider adding an additional

考虑添加额外的

<data android:host="www.mywebsite.com"/>

And finally you may not want to show the chooser dialog but open a specific app intent/activity directly. On your website, if you detect the android user agent, you can create a link url this way:

最后,您可能不想显示选择器对话框,而是直接打开特定的应用程序意图/活动。在您的网站上,如果您检测到 android 用户代理,则可以通过以下方式创建链接 url:

<a href="intent://mywebsite.com/articles#Intent;package=com.myapp;scheme=http;end;"/>

See here for more details How do I open any app from my web browser (Chrome) in Android? What do I have to do with the A Href link?

有关更多详细信息,请参阅此处如何在 Android 中从我的网络浏览器 (Chrome) 打开任何应用程序?我与 A Href 链接有什么关系?

Note that with this method if the app is not installed the user will be taken to the Google Play store for the specified app package.

请注意,使用此方法,如果未安装应用程序,用户将被带到指定应用程序包的 Google Play 商店。

If you are still having problems, check your intent filter priority. http://developer.android.com/guide/topics/manifest/intent-filter-element.html

如果您仍然遇到问题,请检查您的意图过滤器优先级。http://developer.android.com/guide/topics/manifest/intent-filter-element.html

回答by Dhruv Raval

In my case site URL is: http://www.example.org/mobile/

就我而言,站点 URL 是:http: //www.example.org/mobile/

so putting these code into AndroidManifest.xmlinside activity

所以把这些代码放到AndroidManifest.xml中活动

            <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:host="www.example.org"
                    android:pathPrefix="/mobile/"
                    android:scheme="http" />
            </intent-filter>

Here,

这里,

scheme-> Protocol of particular site

方案-> 特定站点的协议

host-> Exact site url with WWW

主机-> 带有WWW 的确切站点 url

pathprefix- > Your site's sub path if available

pathprefix- > 您站点的子路径(如果可用)

Now,

现在,

You can search with chrome / etc android browser'search box like examplethen open chosen dialog ..!!

您可以使用 chrome / etc android 浏览器的搜索框进行搜索,例如示例,然后打开选择的对话框 ..!!