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

Par*_*odi 403 android intentfilter android-intent

任何人都可以指导我如何从Android浏览器启动我的Android应用程序?

Fel*_*lix 605

使用<intent-filter><data>元素的元素.例如,要处理所有链接到twitter.com,你把这个里面你<activity>在你的AndroidManifest.xml:

<intent-filter>
    <data android:scheme="http" android:host="twitter.com"/>
    <action android:name="android.intent.action.VIEW" />
</intent-filter>
Run Code Online (Sandbox Code Playgroud)

然后,当用户在浏览器中单击指向twitter的链接时,将询问他们使用哪个应用程序来完成操作:浏览器或您的应用程序.

当然,如果您想在您的网站和应用程序之间提供紧密集成,您可以定义自己的方案:

<intent-filter>
    <data android:scheme="my.special.scheme" />
    <action android:name="android.intent.action.VIEW" />
</intent-filter>
Run Code Online (Sandbox Code Playgroud)

然后,在您的网络应用程序中,您可以添加以下链接:

<a href="my.special.scheme://other/parameters/here">
Run Code Online (Sandbox Code Playgroud)

当用户点击它时,您的应用程序将自动启动(因为它可能是唯一可以处理my.special.scheme://uris类型的应用程序).唯一的缺点是如果用户没有安装应用程序,他们将得到一个讨厌的错误.而且我不确定有没有办法检查.


编辑:要回答您的问题,您可以使用getIntent().getData()哪个返回一个Uri对象.然后,您可以使用Uri.*方法提取所需的数据.例如,假设用户点击了以下链接http://twitter.com/status/1234:

Uri data = getIntent().getData();
String scheme = data.getScheme(); // "http"
String host = data.getHost(); // "twitter.com"
List<String> params = data.getPathSegments();
String first = params.get(0); // "status"
String second = params.get(1); // "1234"
Run Code Online (Sandbox Code Playgroud)

你可以在你的任何地方完成上述任务Activity,但你可能会想要这样做onCreate().您还可以使用params.size()获取路径段的数量Uri.查看javadoc或android开发人员网站,了解Uri可用于提取特定部分的其他方法.

  • 任何像我一样无法使这个方案工作的人需要在清单中将`android:exported ="true"`添加到他们的`Activity`中.请查看此答案http://stackoverflow.com/a/13044911/1276636 (13认同)
  • 如果没有安装目标Android应用程序怎么办?怎么处理这个. (8认同)
  • 非常感谢您的快速回复.但是,请您告诉我如何在"my.special.scheme://"之后访问参数. (6认同)
  • 我编辑了我的答案,包括如何从Uri中提取数据的说明.如果您通过单击旁边的复选标记进行考虑,请将此答案标记为已接受(仅限). (4认同)
  • 不确定这对整个世界是如何起作用的.只是不能在chrome上工作,它总是在浏览器中打开链接,直到你放置"android:pathPrefix"元素.答案无论如何都没有文档中提到的类别值.如果它仍然不适合某人,请参考:http://stackoverflow.com/a/21727055/2695276 PS:在这方面挣扎了好几天. (4认同)
  • 应该注意的是,自 Chrome 版本 25 以来功能发生了变化。有关新方法的详细信息,请参阅[此页面](https://developer.chrome.com/multidevice/android/intents)。 (2认同)

And*_*cko 68

CHROME截至2014年1月28日,以上所有答案均不适用于我

我的应用程序从http://example.com/someresource/正确启动了应用程序,例如环聊,gmail等,但不是来自Chrome浏览器.

要解决这个问题,以便从CHROME正确启动你必须像这样设置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:host="example.com"
                android:pathPrefix="/someresource/"
                android:scheme="http" />
            <data
                android:host="www.example.com"
                android:pathPrefix="/someresource/"
                android:scheme="http" />
        </intent-filter>
Run Code Online (Sandbox Code Playgroud)

注意pathPrefix元素

只要用户通过点击谷歌搜索结果或任何其他网站上的链接从Chrome浏览器请求http://example.com/someresource/模式,您的应用就会显示在活动选择器中

  • 如何让这个工作为Android棒棒糖? (3认同)

hac*_*bod 66

请在此处查看我的评论:在Android浏览器中创建一个链接启动我的应用程序?

我们强烈反对人们不使用他们自己的计划,除非他们正在定义一个新的全球互联网计划.

  • 我不能对iOS对开发人员施加的限制负责.;) (23认同)
  • 谁负责?没有.但是你可以考虑它,因为无论你喜欢与否,这些限制都会强加给你的许多开发人员 (11认同)
  • Hackbod是Android平台背后的Google工程师之一.遵循这个建议可能是个好主意.事实上,似乎像Honeycomb这样的自定义方案支持. (10认同)
  • hackbod正在回答专门针对Android的问题.没有提到iOS,所以不需要考虑它. (6认同)
  • @hackbod会命名一个方案(即href ="com.ourdomain.myapp // whatever")一个好的做法,假设这样的链接可以在不同的网站上找到? (4认同)
  • 我相信,如果您希望 URL 也能在 iPhone 上使用(例如,从 QR 码),您必须制定自己的方案。 (2认同)

Zak*_*aki 48

在我的情况下,我必须设置两个类别<intent-filter>,然后它工作:

<intent-filter>
<data android:scheme="my.special.scheme" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>
Run Code Online (Sandbox Code Playgroud)

  • 同样在这里,需要添加其他类别. (2认同)

Ден*_*еня 21

例如,你有接下来的事情:

打开应用的链接:http://example.com

您应用的软件包名称:com.example.mypackage

然后你需要做下一步:

1)为您的活动添加一个意图过滤器(可以是您想要的任何活动.有关更多信息,请查看文档).

        <activity
        android:name=".MainActivity">

        <intent-filter android:label="@string/filter_title_view_app_from_web">
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <!-- Accepts URIs that begin with "http://example.com" -->

            <data
                android:host="example.com"
                android:scheme="http" />
        </intent-filter>

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity> 
Run Code Online (Sandbox Code Playgroud)

2)创建HTML文件以测试链接或使用此方法.

<a href="intent://example.com#Intent;scheme=http;package=com.example.mypackage;end">Open your Activity directly (just open your Activity, without a choosing dialog). </a>

<a href="http://example.com">Open this link with browser or your programm (by choosing dialog).</a>
Run Code Online (Sandbox Code Playgroud)

3)使用Mobile Chrome进行测试

4)就是这样.

并没有必要在市场上发布应用程序以测试深层链接=)

此外,有关更多信息,请查看文档和有用的演示文稿.

  • 我必须创建 html 文件并上传它才能使其正常工作。我想知道为什么,尤其是第二个,不能直接从浏览器(chrome)工作。 (2认同)

geo*_*gij 18

还应<category android:name="android.intent.category.BROWSABLE"/>添加到intent过滤器,以便从链接中正确识别活动.

  • 作为参考,添加CATEGORY_BROWSABLE意味着"浏览器可以安全地调用目标活动以显示链接引用的数据 - 例如,图像或电子邮件消息." (4认同)
  • 不仅仅是BROWSABLE,还有android.intent.category.DEFAULT - 所以当在其他应用程序中启动时可以打开链接,比如电子邮件(不仅仅是浏览器) (2认同)

Var*_*tia 8

以下链接提供有关直接从浏览器启动应用程序(如果已安装)的信息.否则它会直接在Play商店中打开应用程序,以便用户可以无缝下载.

https://developer.chrome.com/multidevice/android/intents


Zol*_*tan 7

请注意,当您实现此功能时,如果您的图标从Android启动器中消失,则必须拆分intent-filter.

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </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:scheme="your-own-uri" />
        </intent-filter>
    </activity>
Run Code Online (Sandbox Code Playgroud)

  • 如何让这个工作为Android棒棒糖? (2认同)

RRK*_*RRK 6

是的,Chrome搜索而不是寻找方案.如果要通过URI方案启动应用程序,请在Play商店中使用此酷实用程序App.它节省了我的一天:) https://play.google.com/store/apps/details?id=com.naosim.urlschemesender


Meh*_*ani 5

Xamarin 费利克斯港的答案

在您的中MainActivity,添加以下内容(docs:Android.App.IntentFilterAttribute Class):

....
[IntentFilter(new[] { 
    Intent.ActionView }, 
    Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable }, 
    DataScheme = "my.special.scheme")
]
public class MainActivity : Activity
{
    ....
Run Code Online (Sandbox Code Playgroud)

Xamarin将为您添加以下内容AndroidManifest.xml

<activity android:label="Something" android:screenOrientation="portrait" android:theme="@style/MyTheme" android:name="blahblahblah.MainActivity">
  <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="my.special.scheme" />
  </intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)

为了获取参数(我在MainActivity的OnCreate中进行了测试):

var data = Intent.Data;
if (data != null)
{
    var scheme = data.Scheme;
    var host = data.Host;
    var args = data.PathSegments;

    if (args.Count > 0)
    {
        var first = args[0];
        var second = args[1];
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

据我所知,以上内容可以添加到任何活动中,而不仅仅是MainActivity

笔记:

  1. 当用户单击链接时,Android OS重新启动您的应用程序(杀死前一个实例,如果有的话,然后运行新实例),这意味着该OnCreate应用程序的事件MainLauncher Activity将再次被触发。
  2. 通过此链接:<a href="my.special.scheme://host/arg1/arg2">,在以上最后一个代码中,代码段值将为:
scheme: my.special.scheme
host: host
args: ["arg1", "arg2"]
first: arg1
second: arg2
Run Code Online (Sandbox Code Playgroud)