V-r*_*hit 11 android chrome-custom-tabs
我正在尝试实现自定义Chrome标签.我正在使用Google的默认库customtabs.
我引用本教程来实现自定义chrome选项卡.现在按照教程中的建议,我做了类似的编码.
mCustomTabsServiceConnection = new CustomTabsServiceConnection() {
@Override
public void onCustomTabsServiceConnected(
ComponentName componentName,
CustomTabsClient customTabsClient) {
mCustomTabsClient = customTabsClient;
mCustomTabsClient.warmup(0L);
mCustomTabsSession = mCustomTabsClient.newSession(null);
}
@Override
public void onServiceDisconnected(ComponentName name) {
mCustomTabsClient = null;
}
};
CustomTabsClient.bindCustomTabsService(this, "com.android.chrome",
mCustomTabsServiceConnection);
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(
mCustomTabsSession).setShowTitle(true);
builder.setToolbarColor(getResources().getColor(R.color.purple_main));
builder.setStartAnimations(getApplicationContext(), R.anim.fadein,
R.anim.fadeout);
builder.setExitAnimations(getApplicationContext(), R.anim.fadein,
R.anim.fadeout);
mCustomTabsIntent = builder.build();
Run Code Online (Sandbox Code Playgroud)
并启动自定义标签.
mCustomTabsIntent.launchUrl(TampleDetails.this,
Uri.parse(temple_website));
Run Code Online (Sandbox Code Playgroud)
现在,如您所见,我已将自定义选项卡与chrome的包名绑定,但它仍然要求在Chrome和UC浏览器之间进行选择.很明显UC浏览器不支持自定义标签.
所以我的问题是如何限制自定义标签仅与Chrome浏览器绑定?
任何帮助将不胜感激.
谢谢.
Val*_* S. 17
检查此stackoverflow答案:如果在启动url之前将CustomTabIntent软件包与所需浏览器的软件包一起设置,则可以跳过Android对话框浏览器选项; 对我来说它有效:
/**
* Opens the URL on a Custom Tab
*
* @param activity The host activity.
* @param uri the Uri to be opened.
*/
public static void openCustomTab(Activity activity,
Uri uri) {
// Here is a method that returns the chrome package name
String packageName = CustomTabsHelper.getPackageNameToUse(activity, mUrl);
CustomTabsIntent.Builder builder = new CustomTabsIntent.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);
}
Run Code Online (Sandbox Code Playgroud)
自定义选项卡协议已打开,这意味着其他浏览器可以支持它.事实上,三星互联网浏览器已经支持它(尽管实施方式已经破损),Firefox已经为它的夜间构建添加了初步的,非常实验性的支持.
因此,最佳做法是查询Android系统安装的浏览器支持自定义选项卡协议:
private static final String ACTION_CUSTOM_TABS_CONNECTION =
"android.support.customtabs.action.CustomTabsService";
public static ArrayList<ResolveInfo> getCustomTabsPackages(Context context) {
PackageManager pm = context.getPackageManager();
// Get default VIEW intent handler.
Intent activityIntent = new Intent(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 = new ArrayList<>();
for (ResolveInfo info : resolvedActivityList) {
Intent serviceIntent = new Intent();
serviceIntent.setAction(ACTION_CUSTOM_TABS_CONNECTION);
serviceIntent.setPackage(info.activityInfo.packageName);
if (pm.resolveService(serviceIntent, 0) != null) {
packagesSupportingCustomTabs.add(info);
}
}
return packagesSupportingCustomTabs;
}
Run Code Online (Sandbox Code Playgroud)
您可能需要检查ResolveInfo#preferredOrder以检查用户是否对其中一个应用程序优先于其他应用程序.此外,如果没有首选应用程序(或两个应用程序具有相同的主要首选项级别),您可能想要让用户选择一个,或者使用合理的默认值
检查给定Url是否具有处理它的本机应用程序也很重要.如果是这样,您的应用程序启动本机应用程序而不是在自定义选项卡中打开它可能是有意义的:
public static List<ResolveInfo> getNativeApp(Context context, Uri uri) {
PackageManager pm = context.getPackageManager();
//Get all Apps that resolve a random url
Intent browserActivityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
List<ResolveInfo> resolvedBrowserList = pm.queryIntentActivities(browserActivityIntent, 0);
Intent specializedActivityIntent = new Intent(Intent.ACTION_VIEW, uri);
List<ResolveInfo> resolvedSpecializedList = pm.queryIntentActivities(specializedActivityIntent, 0);
resolvedSpecializedList.removeAll(resolvedBrowserList);
return resolvedBrowserList;
}
Run Code Online (Sandbox Code Playgroud)
一旦决定使用哪个处理程序打开自定义选项卡,使用mCustomTabsIntent.intent.setPackage(packageName);告诉自定义选项卡打开它的应用程序.
| 归档时间: |
|
| 查看次数: |
7390 次 |
| 最近记录: |