简体   繁体   English

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

[英]Chrome custom tabs and intent-filter

The application has Activity to show the content of the specific website eg "example.com".该应用程序具有显示特定网站内容的活动,例如“example.com”。 So to show the content of "example.com/product/123" inside the app I have intent filter:所以为了在应用程序中显示“example.com/product/123”的内容,我有意图过滤器:

<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>

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.但在某些情况下,我必须在浏览器中显示此内容,因此我决定使用 chrome 自定义选项卡只是为了使其更快并使用户留在应用程序中。 I tried to use intent builder to show url in custom tabs with:我尝试使用意图构建器在自定义选项卡中显示 url:

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

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,它会按预期工作。

So the question is how create explicit Intent for the Chrome Custom Tabs?所以问题是如何为 Chrome 自定义标签创建显式 Intent?

You can use setPackage :您可以使用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);

I have used @Mattia Maestrini's Answer, but i think some might be we have to change我使用了@Mattia Maestrini 的回答,但我认为有些可能是我们必须改变

I have already installed latest Chrome (Updated August 8, 2016) in my Nexus 5 (6.0.1)我已经在我的 Nexus 5 (6.0.1) 中安装了最新的 Chrome(2016 年 8 月 8 日更新)

but I am not getting Chrome Package "com.android.chrome" into resolveInfoList, I have to do changes, then it is working fine on my phone但我没有将 Chrome 包“com.android.chrome”放入 resolveInfoList,我必须进行更改,然后它在我的手机上运行良好

packageManager.queryIntentActivities(customTabsIntent.intent, PackageManager.MATCH_DEFAULT_ONLY);

above query does not giving me package name of Chrome上面的查询没有给我 Chrome 的包名

       String PACKAGE_NAME = "com.android.chrome";

        CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder().build();
        customTabsIntent.intent.setData(Uri.parse(url));

        PackageManager packageManager = getPackageManager();
        List<ApplicationInfo> resolveInfoList = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);

        for (ApplicationInfo applicationInfo : resolveInfoList) {
            String packageName = applicationInfo.packageName;
            if (TextUtils.equals(packageName, PACKAGE_NAME)) {
                customTabsIntent.intent.setPackage(PACKAGE_NAME);
                break;
            }
        }

        customTabsIntent.launchUrl(this, Uri.parse(url));

I have tested above code and it is working fine on my phone我已经测试了上面的代码,它在我的手机上运行良好

在接受的答案中,您还可以检查是否启用了 chrome。

if (TextUtils.equals(packageName, PACKAGE_NAME) && applicationInfo.enabled)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM