从浏览器拦截的链接,打开我的Andr​​oid应用

问题描述:

我希望能够促使我的应用程序,当用户点击允许浏览器打开它在给定模式的URL,而不是打开一个链接。这可能是当用户在网页上的浏览器或电子邮件客户端或在一个新鲜出炉的应用程序中的web视图内

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.action.VIEW android.intent.category.BROWSABLE.

Use an android.intent.action.VIEW of category android.intent.category.BROWSABLE.

从罗曼盖伊的照片流应用程序的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,你需要寻找的动作,然后做一些与你一直流传的网址。该 Intent.getData()方法为您提供了一个开放的。

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.