Viktor Yakunin Viktor Yakunin - 1 year ago 182
Android Question

Chrome custom tabs and intent-filter

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

<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" />
android:scheme="http" />

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?

Answer Source

You can use setPackage:

String PACKAGE_NAME = "";

CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder().build();

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.launchUrl(this, uri);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download