Chrome自定义标签和意图过滤器

Chrome自定义标签和意图过滤器

本文介绍了Chrome自定义标签和意图过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应用程序具有活动"以显示特定网站的内容,例如"example.com".因此,为了在应用程序中显示"example.com/product/123"的内容,我有一个意图过滤器:

The application has Activity to show the content of the specific website e.g. "example.com". So to show the content of "example.com/product/123" inside the app I have intent filter:

<activity
    android:name=".LinkInterceptorActivity">
    <intent-filter android:priority="999">
         <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.example.com"
              android:scheme="http" />
    </intent-filter>
</activity>

但是在某些情况下,我必须在浏览器中显示此内容,因此我决定使用chrome自定义标签只是为了使其更快并保持用户在应用程序中.我尝试使用Intent Builder在自定义标签中显示网址,

But in some cases I have to show this content in browser, so I decided to use chrome custom tabs just to make it faster and keep user inside the app.I tried to use intent builder to show url in custom tabs with:

new CustomTabsIntent.Builder().build().launchUrl(activity, url);

但是它显示了使用...完成操作"对话框,在该对话框中可以看到我的应用程序和设备上的所有浏览器.如果选择我的应用程序,它将跳入循环并再次显示对话框,但是如果选择chrome,它将按预期工作.

But it shows me 'Complete action using...' dialog where I see my app and all the browsers on device. If I select my app it jumps into the loop and shows dialog again, but if I select chrome it works as expected.

所以问题是如何为Chrome自定义标签创建显式的Intent?

So the question is how create explicit Intent for the Chrome Custom Tabs?

推荐答案

您可以使用setPackage:

String PACKAGE_NAME = "com.android.chrome";

CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder().build();
customTabsIntent.intent.setData(uri);

PackageManager packageManager = getPackageManager();
List<ResolveInfo> resolveInfoList = packageManager.queryIntentActivities(customTabsIntent.intent, PackageManager.MATCH_DEFAULT_ONLY);

for (ResolveInfo resolveInfo : resolveInfoList) {
    String packageName = resolveInfo.activityInfo.packageName;
    if (TextUtils.equals(packageName, PACKAGE_NAME))
        customTabsIntent.intent.setPackage(PACKAGE_NAME);
}

customTabsIntent.launchUrl(this, uri);

这篇关于Chrome自定义标签和意图过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 13:07