Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome custom tabs and intent-filter

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?

like image 625
Viktor Yakunin Avatar asked Sep 18 '15 15:09

Viktor Yakunin


People also ask

What is custom tab intent?

Chrome custom tabs give apps more control over their web experience, and make transitions between native and web content more seamless without having to resort to a WebView. Chrome custom tabs allow an app to customize how Chrome looks and feels. An app can change things like: Toolbar color. Enter and exit animations.

What is intent filter and permission?

An intent filter is an expression in an app's manifest file that specifies the type of intents that the component would like to receive. For instance, by declaring an intent filter for an activity, you make it possible for other apps to directly start your activity with a certain kind of intent.

What is the difference between intent and intent filters?

An intent is an object that can hold the os or other app activity and its data in uri form.It is started using startActivity(intent-obj).. \n whereas IntentFilter can fetch activity information on os or other app activities.

How to create a custom tabs intent in Android?

There are multiple ways for creating a custom tabs intent. You can use the builder available in androidX by adding the library to the build dependencies: A Custom Tabs connection is used for setting up a CustomTabsSession between the app and the Chrome tab. We need the session to verify that the app and web app belong to the same origin.

What are custom chrome tabs in Android?

Many apps use this feature of custom chrome tabs for redirecting their users from their application to any webpage via custom chrome tabs. So in this article, we will take a look at the implementation of Custom Chrome tabs in Android.

What are intent filters in Android 7?

Android 7.0 Device administration Android Developers Docs Guides Intents and Intent Filters An Intentis a messaging object you can use to request an action from another app component. Although intents facilitate communication between components in several ways, there are three fundamental use cases:

What are custom tabs and how do they work?

Note: For information on sharing the content in Custom Tabs, see the blog post, Better content sharing with Custom Tabs. Custom Tabs allow an app to customize how the browser looks and feels. An app can change things like:


2 Answers

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);
like image 123
Mattia Maestrini Avatar answered Oct 15 '22 14:10

Mattia Maestrini


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

like image 37
Parmar Subhash Avatar answered Oct 15 '22 15:10

Parmar Subhash