拦截来自浏览器的链接以打开我的 Android 应用 [英] Intercepting links from the browser to open my Android app

查看:48
本文介绍了拦截来自浏览器的链接以打开我的 Android 应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在用户点击给定模式的 URL 时提示我的应用打开一个链接,而不是允许浏览器打开它.这可能是当用户在浏览器或电子邮件客户端中的网页上或在新开发的应用程序中的 WebView 中时.

I'd like to be able to prompt my app to open a link when user clicks on an URL of a given pattern instead of allowing the browser to open it. This could be when the user is on a web page in the browser or in an email client or within a WebView in a freshly-minted app.

例如,从手机的任意位置点击 YouTube 链接,您就有机会打开 YouTube 应用.

For example, click on a YouTube link from anywhere in the phone and you'll be given the chance to open the YouTube app.

如何为我自己的应用实现这一目标?

How do I achieve this for my own app?

推荐答案

使用类别 android.intent.category.BROWSABLE.

来自 Romain Guy 的 Photostream 应用的 AndroidManifest.xml,

From Romain Guy's Photostream app's AndroidManifest.xml,

    <activity
        android:name=".PhotostreamActivity"
        android:label="@string/application_name">

        <!-- ... -->            

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

一旦进入,您就会进入 activity,你需要寻找动作,然后用你得到的 URL 做一些事情.Intent.getData() 方法给你一个 Uri.

Once inside you're in the activity, you need to look for the action, and then do something with the URL you've been handed. The Intent.getData() method gives you a Uri.

    final Intent intent = getIntent();
    final String action = intent.getAction();

    if (Intent.ACTION_VIEW.equals(action)) {
        final List<String> segments = intent.getData().getPathSegments();
        if (segments.size() > 1) {
            mUsername = segments.get(1);
        }
    }

但是,应该注意的是,这个应用程序有点过时了(1.2),所以你可能会发现有更好的方法来实现这一点.

It should be noted, however, that this app is getting a little bit out of date (1.2), so you may find there are better ways of achieving this.

这篇关于拦截来自浏览器的链接以打开我的 Android 应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆