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?
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.
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.
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.
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.
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.
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:
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:
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);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With