Chrome 自定义标签。设置多个工具栏项
Chrome Custom Tabs. Setting multiple toolbar items
我正在我们的 Android 应用程序上实施 Chrome 自定义选项卡(使用最新版本 23.3.0)。 chrome 选项卡的最新版本允许您使用 "builder.addToolbarItem()" 方法(根据此 的其他可自定义内容)在底部工具栏上添加按钮。现在我在添加操作时遇到问题我的底部工具栏按钮的意图。我为我添加的每个工具栏项目设置了两个不同的操作意图。但是当打开 chrome 自定义选项卡并单击我添加的任何工具栏项目时,相同的意图已启动。启动的意向始终对应于为添加的第一个工具栏项设置的意向。
这是我在单击回收站视图的项目时用来构建自定义选项卡的代码。
protected void openCustomTab(Listing listing, int position) {
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
builder.setToolbarColor(ContextCompat.getColor(this, R.color.primary));
builder.setStartAnimations(this, R.anim.slide_in_right, R.anim.slide_out_left);
builder.setExitAnimations(this, R.anim.slide_in_left, R.anim.slide_out_right);
builder.setCloseButtonIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_arrow_back));
addShareAction(listing, builder);
setBottomToolbar(listing, position, builder);
CustomTabActivityHelper.openCustomTab(
this, builder.build(), Uri.parse(listing.getListingURL()));
}
private void setBottomToolbar(Listing listing, int position, CustomTabsIntent.Builder builder) {
builder.setSecondaryToolbarColor(ContextCompat.getColor(this, R.color.color_bottom_toolbar));
addFavoriteButton(listing, position, builder);
addReportButton(position, builder);
}
private void addShareAction(Listing listing, CustomTabsIntent.Builder builder) {
Bitmap iconShare = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_share_custom_tab);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, getResources().getString(R.string.share_item_message));
shareIntent.putExtra(Intent.EXTRA_TEXT, listing.getShareURL());
PendingIntent pi = PendingIntent.getActivity(this, 0, shareIntent, 0);
builder.setActionButton(iconShare, getString(R.string.share_item_message), pi, true);
}
private void addReportButton(int position, CustomTabsIntent.Builder builder) {
Bitmap reportIcon = BitmapFactory.decodeResource(getResources(),
R.drawable.detail_bottom_hide);
Bundle menuBundle = ActivityOptions.makeCustomAnimation(this, R.anim.slide_in_left,
R.anim.slide_out_right).toBundle();
Intent reportIntent = createDetailActionIntent(position, ViewsConstants.REPORT);
PendingIntent pi = PendingIntent.getActivity(this, ACTION_REPORT, reportIntent, 0, menuBundle);
builder.addToolbarItem(2, reportIcon, getString(R.string.report_listing_dialog_title), pi);
}
private void addFavoriteButton(Listing listing, int position, CustomTabsIntent.Builder builder) {
Bitmap favIcon;
if (listing.getIsFavorite()) {
favIcon = BitmapFactory.decodeResource(getResources(),
R.drawable.detailbottom_fav);
} else {
favIcon = BitmapFactory.decodeResource(getResources(),
R.drawable.detailbottom_nofav);
}
Bundle menuBundle = ActivityOptions.makeCustomAnimation(this, android.R.anim.slide_in_left,
android.R.anim.slide_out_right).toBundle();
Intent favIntent = createDetailActionIntent(position, ViewsConstants.FAVORITE);
PendingIntent pi = PendingIntent.getActivity(this, 0,favIntent, 0, menuBundle);
builder.addToolbarItem(1, favIcon, getString(R.string.add_favorite), pi);
}
private Intent createDetailActionIntent(int position, String action) {
Intent actionIntent = new Intent(getApplicationContext(), ListingActivity.class);
actionIntent.putExtra(ViewsConstants.SEARCH_PARAMETERS, getListingPresenter().getSearchParameters());
actionIntent.putExtra(ViewsConstants.POSITION, position);
actionIntent.putExtra(ViewsConstants.DETAIL_ACTION, action);
return actionIntent;
}
因此,在 运行 这段代码之后,将获得一个 chrome 自定义选项卡,其中包含一个底部工具栏和两个按钮收藏夹和报告。单击任何按钮将始终启动最喜欢的操作。我已经确保通过多次调试代码将值正确传递给意图。
我不知道我在这里错过了什么。我开始认为它可能是 Chrome 自定义选项卡上的一个错误,但可能是我遗漏了什么。任何帮助将不胜感激。提前致谢。
已编辑
我已经根据给出的建议编辑了代码库,现在它可以正常工作,可以针对每个按钮执行的操作。比你!
但我还有一个问题,关于职位。我正在设置在 intent 上选择的项目的位置,但是当打开 chrome 选项卡时,单击任何操作按钮并返回到 activity,intent 仅设置了操作但设置了位置丢失了。我不明白为什么?我在上面代码中显示的方法 createDetailActionIntent() 中设置所有意图值。
关于从 chrome 自定义选项卡返回到 activity 并检索 intent extras 时位置丢失的任何想法???
这个 post 帮助我解决了我遇到的最后一个问题。
感谢所有为解决这个问题做出贡献的人!
启动了相同的意图,因为您使用相同的 Intent
操作和相同的请求代码创建了两个 PendingIntent
。
要解决您的问题,请更改 addFavoriteButton
中 PendingIntent.getActivity
的请求代码:
private void addFavoriteButton(Listing listing, int position, CustomTabsIntent.Builder builder) {
Bitmap favIcon;
if (listing.getIsFavorite()) {
favIcon = BitmapFactory.decodeResource(getResources(),
R.drawable.detailbottom_fav);
} else {
favIcon = BitmapFactory.decodeResource(getResources(),
R.drawable.detailbottom_nofav);
}
Bundle menuBundle = ActivityOptions.makeCustomAnimation(this, android.R.anim.slide_in_left,
android.R.anim.slide_out_right).toBundle();
Intent favIntent = createDetailActionIntent(position, ViewsConstants.FAVORITE);
PendingIntent pi = PendingIntent.getActivity(this, 1,favIntent, 0, menuBundle);
builder.addToolbarItem(1, favIcon, getString(R.string.add_favorite), pi);
}
如果您想了解更多关于 PendingIntent
的工作原理,您可以阅读这篇文章 StackOveflow answer。
您需要将不同的 requestCode 传递给 PendingIntent.getActivity
调用,否则 PendingIntent 会被覆盖。查看 PendingIntent docs 了解更多详情。
自定义选项卡的 Github 演示 code 以正确的方式实现了这一点。
您的代码应如下所示:
private static final int ACTION_CODE_REPORT = 1;
private static final int ACTION_CODE_SHARE = 2;
private void addShareAction(Listing listing, CustomTabsIntent.Builder builder) {
Bitmap iconShare = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_share_custom_tab);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, getResources().getString(R.string.share_item_message));
shareIntent.putExtra(Intent.EXTRA_TEXT, listing.getShareURL());
PendingIntent pi = PendingIntent.getActivity(this, ACTION_CODE_SHARE, shareIntent, 0);
builder.setActionButton(iconShare, getString(R.string.share_item_message), pi, true);
}
private void addReportButton(int position, CustomTabsIntent.Builder builder) {
Bitmap reportIcon = BitmapFactory.decodeResource(getResources(),
R.drawable.detail_bottom_hide);
Bundle menuBundle = ActivityOptions.makeCustomAnimation(this, R.anim.slide_in_left,
R.anim.slide_out_right).toBundle();
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), ACTION_CODE_REPORT,
createDetailActionIntent(position, ViewsConstants.REPORT), 0, menuBundle);
builder.addToolbarItem(2, reportIcon, getString(R.string.report_listing_dialog_title), pi);
}
我正在我们的 Android 应用程序上实施 Chrome 自定义选项卡(使用最新版本 23.3.0)。 chrome 选项卡的最新版本允许您使用 "builder.addToolbarItem()" 方法(根据此
这是我在单击回收站视图的项目时用来构建自定义选项卡的代码。
protected void openCustomTab(Listing listing, int position) {
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
builder.setToolbarColor(ContextCompat.getColor(this, R.color.primary));
builder.setStartAnimations(this, R.anim.slide_in_right, R.anim.slide_out_left);
builder.setExitAnimations(this, R.anim.slide_in_left, R.anim.slide_out_right);
builder.setCloseButtonIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_arrow_back));
addShareAction(listing, builder);
setBottomToolbar(listing, position, builder);
CustomTabActivityHelper.openCustomTab(
this, builder.build(), Uri.parse(listing.getListingURL()));
}
private void setBottomToolbar(Listing listing, int position, CustomTabsIntent.Builder builder) {
builder.setSecondaryToolbarColor(ContextCompat.getColor(this, R.color.color_bottom_toolbar));
addFavoriteButton(listing, position, builder);
addReportButton(position, builder);
}
private void addShareAction(Listing listing, CustomTabsIntent.Builder builder) {
Bitmap iconShare = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_share_custom_tab);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, getResources().getString(R.string.share_item_message));
shareIntent.putExtra(Intent.EXTRA_TEXT, listing.getShareURL());
PendingIntent pi = PendingIntent.getActivity(this, 0, shareIntent, 0);
builder.setActionButton(iconShare, getString(R.string.share_item_message), pi, true);
}
private void addReportButton(int position, CustomTabsIntent.Builder builder) {
Bitmap reportIcon = BitmapFactory.decodeResource(getResources(),
R.drawable.detail_bottom_hide);
Bundle menuBundle = ActivityOptions.makeCustomAnimation(this, R.anim.slide_in_left,
R.anim.slide_out_right).toBundle();
Intent reportIntent = createDetailActionIntent(position, ViewsConstants.REPORT);
PendingIntent pi = PendingIntent.getActivity(this, ACTION_REPORT, reportIntent, 0, menuBundle);
builder.addToolbarItem(2, reportIcon, getString(R.string.report_listing_dialog_title), pi);
}
private void addFavoriteButton(Listing listing, int position, CustomTabsIntent.Builder builder) {
Bitmap favIcon;
if (listing.getIsFavorite()) {
favIcon = BitmapFactory.decodeResource(getResources(),
R.drawable.detailbottom_fav);
} else {
favIcon = BitmapFactory.decodeResource(getResources(),
R.drawable.detailbottom_nofav);
}
Bundle menuBundle = ActivityOptions.makeCustomAnimation(this, android.R.anim.slide_in_left,
android.R.anim.slide_out_right).toBundle();
Intent favIntent = createDetailActionIntent(position, ViewsConstants.FAVORITE);
PendingIntent pi = PendingIntent.getActivity(this, 0,favIntent, 0, menuBundle);
builder.addToolbarItem(1, favIcon, getString(R.string.add_favorite), pi);
}
private Intent createDetailActionIntent(int position, String action) {
Intent actionIntent = new Intent(getApplicationContext(), ListingActivity.class);
actionIntent.putExtra(ViewsConstants.SEARCH_PARAMETERS, getListingPresenter().getSearchParameters());
actionIntent.putExtra(ViewsConstants.POSITION, position);
actionIntent.putExtra(ViewsConstants.DETAIL_ACTION, action);
return actionIntent;
}
因此,在 运行 这段代码之后,将获得一个 chrome 自定义选项卡,其中包含一个底部工具栏和两个按钮收藏夹和报告。单击任何按钮将始终启动最喜欢的操作。我已经确保通过多次调试代码将值正确传递给意图。 我不知道我在这里错过了什么。我开始认为它可能是 Chrome 自定义选项卡上的一个错误,但可能是我遗漏了什么。任何帮助将不胜感激。提前致谢。
已编辑
我已经根据给出的建议编辑了代码库,现在它可以正常工作,可以针对每个按钮执行的操作。比你! 但我还有一个问题,关于职位。我正在设置在 intent 上选择的项目的位置,但是当打开 chrome 选项卡时,单击任何操作按钮并返回到 activity,intent 仅设置了操作但设置了位置丢失了。我不明白为什么?我在上面代码中显示的方法 createDetailActionIntent() 中设置所有意图值。
关于从 chrome 自定义选项卡返回到 activity 并检索 intent extras 时位置丢失的任何想法???
这个 post 帮助我解决了我遇到的最后一个问题。
感谢所有为解决这个问题做出贡献的人!
启动了相同的意图,因为您使用相同的 Intent
操作和相同的请求代码创建了两个 PendingIntent
。
要解决您的问题,请更改 addFavoriteButton
中 PendingIntent.getActivity
的请求代码:
private void addFavoriteButton(Listing listing, int position, CustomTabsIntent.Builder builder) {
Bitmap favIcon;
if (listing.getIsFavorite()) {
favIcon = BitmapFactory.decodeResource(getResources(),
R.drawable.detailbottom_fav);
} else {
favIcon = BitmapFactory.decodeResource(getResources(),
R.drawable.detailbottom_nofav);
}
Bundle menuBundle = ActivityOptions.makeCustomAnimation(this, android.R.anim.slide_in_left,
android.R.anim.slide_out_right).toBundle();
Intent favIntent = createDetailActionIntent(position, ViewsConstants.FAVORITE);
PendingIntent pi = PendingIntent.getActivity(this, 1,favIntent, 0, menuBundle);
builder.addToolbarItem(1, favIcon, getString(R.string.add_favorite), pi);
}
如果您想了解更多关于 PendingIntent
的工作原理,您可以阅读这篇文章 StackOveflow answer。
您需要将不同的 requestCode 传递给 PendingIntent.getActivity
调用,否则 PendingIntent 会被覆盖。查看 PendingIntent docs 了解更多详情。
自定义选项卡的 Github 演示 code 以正确的方式实现了这一点。
您的代码应如下所示:
private static final int ACTION_CODE_REPORT = 1;
private static final int ACTION_CODE_SHARE = 2;
private void addShareAction(Listing listing, CustomTabsIntent.Builder builder) {
Bitmap iconShare = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_share_custom_tab);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, getResources().getString(R.string.share_item_message));
shareIntent.putExtra(Intent.EXTRA_TEXT, listing.getShareURL());
PendingIntent pi = PendingIntent.getActivity(this, ACTION_CODE_SHARE, shareIntent, 0);
builder.setActionButton(iconShare, getString(R.string.share_item_message), pi, true);
}
private void addReportButton(int position, CustomTabsIntent.Builder builder) {
Bitmap reportIcon = BitmapFactory.decodeResource(getResources(),
R.drawable.detail_bottom_hide);
Bundle menuBundle = ActivityOptions.makeCustomAnimation(this, R.anim.slide_in_left,
R.anim.slide_out_right).toBundle();
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), ACTION_CODE_REPORT,
createDetailActionIntent(position, ViewsConstants.REPORT), 0, menuBundle);
builder.addToolbarItem(2, reportIcon, getString(R.string.report_listing_dialog_title), pi);
}