Custom Chrome Tabs Asks For Multiple Browser To Choose
Solution 1:
Check this stackoverflow answer: if you set the CustomTabIntent package with the package of your desired browser before launch the url, you can skip the Android dialog browser choice; for me it worked:
/**
* Opens the URL on a Custom Tab
*
* @param activity The host activity.
* @param uri the Uri to be opened.
*/publicstaticvoidopenCustomTab(Activity activity,
Uri uri) {
// Here is a method that returns the chrome package name StringpackageName= CustomTabsHelper.getPackageNameToUse(activity, mUrl);
CustomTabsIntent.Builderbuilder=newCustomTabsIntent.Builder();
mCustomTabsIntent = builder
.setShowTitle(true)
.build();
builder.setToolbarColor(ContextCompat.getColor(activity, R.color.colorPrimary));
if ( packageName != null ) {
mCustomTabsIntent.intent.setPackage(packageName);
}
mCustomTabsIntent.launchUrl(activity, uri);
}
Solution 2:
Supporting multiple browsers
The Custom Tabs protocol is open, meaning that other browsers can support it. In fact, the Samsung Internet Browser already supports it (albeit with broken implementation) and Firefox has added an initial, very experimental support to it's nightly builds.
So, best practice would be querying the Android system on which of the installed browsers support the Custom Tabs protocol:
privatestaticfinalStringACTION_CUSTOM_TABS_CONNECTION="android.support.customtabs.action.CustomTabsService";
publicstatic ArrayList<ResolveInfo> getCustomTabsPackages(Context context) {
PackageManagerpm= context.getPackageManager();
// Get default VIEW intent handler.IntentactivityIntent=newIntent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
// Get all apps that can handle VIEW intents.
List<ResolveInfo> resolvedActivityList = pm.queryIntentActivities(activityIntent, 0);
ArrayList<ResolveInfo> packagesSupportingCustomTabs = newArrayList<>();
for (ResolveInfo info : resolvedActivityList) {
IntentserviceIntent=newIntent();
serviceIntent.setAction(ACTION_CUSTOM_TABS_CONNECTION);
serviceIntent.setPackage(info.activityInfo.packageName);
if (pm.resolveService(serviceIntent, 0) != null) {
packagesSupportingCustomTabs.add(info);
}
}
return packagesSupportingCustomTabs;
}
You may want to check ResolveInfo#preferredOrder
to check if the user has preference on one of the apps over the others. Also, if there's no preferred app (or two apps have the same major preference level), you may want to ask the user to choose one, or go with reasonable defaults
Thinking about Native apps
It is also important to check if the give Url has a native application that handles it installed. If so, it may make sense for your app to start the native one instead of opening it in a Custom Tab:
publicstatic List<ResolveInfo> getNativeApp(Context context, Uri uri) {
PackageManagerpm= context.getPackageManager();
//Get all Apps that resolve a random urlIntentbrowserActivityIntent=newIntent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
List<ResolveInfo> resolvedBrowserList = pm.queryIntentActivities(browserActivityIntent, 0);
IntentspecializedActivityIntent=newIntent(Intent.ACTION_VIEW, uri);
List<ResolveInfo> resolvedSpecializedList = pm.queryIntentActivities(specializedActivityIntent, 0);
resolvedSpecializedList.removeAll(resolvedBrowserList);
return resolvedBrowserList;
}
Opening the Custom Tab with an specific package
Once decided on which handler to use to open the Custom Tab, use the mCustomTabsIntent.intent.setPackage(packageName);
to tell Custom Tab which application to open it.
Solution 3:
Might be helpful.
CustomTabsIntent.Builderbuilder=newCustomTabsIntent.Builder();
CustomTabsIntentcustomTabsIntent= builder.build();
customTabsIntent.intent.setPackage("com.android.chrome");
builder.setToolbarColor(ContextCompat.getColor(context,R.color.colorPrimary));
builder.setShowTitle(true);
builder.addDefaultShareMenuItem();
builder.build().launchUrl((Activity) context, Uri.parse(http_link));
Solution 4:
you can avoid this problem by specifying the package name like this intentCustomTabs.intent.setPackage("com.android.chrome");
full code :
Stringurl= link;
CustomTabsIntent.BuilderbuilderCustomTabs=newCustomTabsIntent.Builder();
CustomTabsIntentintentCustomTabs= builderCustomTabs.build();
intentCustomTabs.intent.setPackage("com.android.chrome");
intentCustomTabs.intent.addFlags(newInteger("67108864"));
builderCustomTabs.setShowTitle(true);
builderCustomTabs.setToolbarColor(ContextCompat.getColor(this, R.color.colorPrimary));
builderCustomTabs.enableUrlBarHiding();
intentCustomTabs.launchUrl(this, Uri.parse(url));
Post a Comment for "Custom Chrome Tabs Asks For Multiple Browser To Choose"