android - 从浏览器拦截链接以打开我的 Android 应用程序

标签 android android-intent intentfilter url-interception

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

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

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

最佳答案

使用类别 android.intent.category.BROWSABLE 的 android.intent.action.VIEW .

来自 Romain Guy 的 Photostream应用程序的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 ,你需要寻找 Action ,然后对你得到的 URL 做一些事情。 Intent.getData() 方法为您提供了一个 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),所以你可能会发现有更好的方法来实现这一点。

关于android - 从浏览器拦截链接以打开我的 Android 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1609573/

相关文章:

android - ACTION_CHOOSER 和 ACTION_PICK_ACTIVITY 之间的 Intent 解析和区别

android - 父 Activity 完成后,子 Activity 被杀死

android - (New) GCM消息收到,但如何解析?

android - 图库问题

Android MediaStore.ACTION_IMAGE_CAPTURE 和 Intent.ACTION_PICK

android - 未从 onActivityCreated 中的 Intent 调用 Activity

android - 从 Android 应用程序转换 m3u8 文件

android - 如何从 R.raw 资源获取 java.io.File

Android Inbuild(ActionImageCapture) Intent 返回空 Intent 。无法传递结果 {who= null}