I have an activity that loads an external url into a webview within my app. I'd like to use Chrome Custom tabs when it's available but I support devices that might not have a version of Chrome that supports them.
In the case of CustomTabs not being supported I'd like to use my old code but use the CustomTabsIntent.Builder() when they are. The old code loads the content in a WebView contained in an Activity where I can still manage the ActionBar.
I'd like to write a helper method that will tell me if it's supported but I'm not sure how. The info on the developer page is pretty slim: https://developer.chrome.com/multidevice/android/customtabs
It says if you bind succeeds the custom tabs can be safely used. Is there an easy way to bind to test this?
Like this I assume:
Intent serviceIntent = new Intent("android.support.customtabs.action.CustomTabsService");
serviceIntent.setPackage("com.android.chrome");
boolean customTabsSupported = bindService(serviceIntent, new CustomTabsServiceConnection() {
@Override
public void onCustomTabsServiceConnected(final ComponentName componentName, final CustomTabsClient customTabsClient) {}
@Override
public void onServiceDisconnected(final ComponentName name) {}
},
Context.BIND_AUTO_CREATE | Context.BIND_WAIVE_PRIORITY);
if (customTabsSupported) {
// is supported
}
Instead of binding and unbinding the service, you can use the PackageManager to check if Custom Tabs is supported.
Be aware that other browsers may support Custom Tabs in the future, so you may want to modify that to support this case.
I ended up writing a static method in my Utils class so I can check and handle the case where it isn't supported:
From developer site of Chrome, I found the followings -
So, I checked whether Chrome supports Chrome Custom Tab by version.
Check my code:
I don't know how efficient it is, just want to share.
You can try following code to figure out if you have a browser that supports custom tab:
When calling, you can do something like this:
I solved this problem by handling
ActivityNotFound
exception in catch block.The trick is to check if the browser activity of chrome can be started or not, if it can't be started or throws an exception then simply open the link through
Intent.ACTION_VIEW
.Here is all the relevant code ....
What is
openCustomTab(url)
you say : Here is the relevant code for it.My style of answer may seem cocky but before clicking downvote let me know if you have run into any unexpected bug or any other problem that this approach may have cause. Do give your feedback, we are a community afterall.