Chrome custom tabs and intent-filter

做~自己de王妃 提交于 2019-12-05 17:01:24

问题


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>

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

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.

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


回答1:


You can use 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);



回答2:


I have used @Mattia Maestrini's Answer, but i think some might be we have to change

I have already installed latest Chrome (Updated August 8, 2016) in my Nexus 5 (6.0.1)

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

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

above query does not giving me package name of 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



来源:https://stackoverflow.com/questions/32655190/chrome-custom-tabs-and-intent-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!