Android 在 Intent 中响应 URL

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

Android Respond To URL in Intent

androidurlandroid-intentintentfilterlaunch

提问by Isaac Waller

I want my intent to be launched when the user goes to a certain url: for example, the android market does this with http://market.android.com/urls. so does youtube. I want mine to do that too.

我希望在用户访问某个 url 时启动我的意图:例如,android 市场使用http://market.android.com/urls执行此操作。优酷也是。我也希望我的也这样做。

回答by Isaac Waller

I did it! Using <intent-filter>. Put the following into your manifest file:

我做到了!使用<intent-filter>. 将以下内容放入您的清单文件中:

<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.youtube.com" android:scheme="http" />
</intent-filter>

This works perfectly!

这工作得很好!

回答by Jordan Réjaud

You might need to add different permutations to your intent filter to get it to work in different cases (http/ https/ ect).

您可能需要向意图过滤器添加不同的排列,以使其在不同情况下(http/https/ 等)工作。

For example, I had to do the following for an app which would open when the user opened a link to google drive forms, www.docs.google.com/forms

例如,我必须为一个应用程序执行以下操作,该应用程序将在用户打开指向 google 驱动器表单的链接时打开, www.docs.google.com/forms

Note that path prefix is optional.

请注意,路径前缀是可选的。

        <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="http"
                android:host="docs.google.com"
                android:pathPrefix="/forms"/>
            <data
                android:scheme="http"
                android:host="www.docs.google.com"
                android:pathPrefix="/forms" />

            <data
                android:scheme="https"
                android:host="www.docs.google.com"
                android:pathPrefix="/forms" />

            <data
                android:scheme="https"
                android:host="docs.google.com"
                android:pathPrefix="/forms" />
        </intent-filter>