Android Chrome自定义标签将“复制链接”添加到选项菜单

时间:2016-02-26 10:37:55

标签: android chrome-custom-tabs

如何添加'复制链接' Android中Chrome浏览器自定义标签选项菜单的选项。在CustomTabs中添加自定义菜单项是这样的。

CustomTabsIntent.Builder customTabsIntent = new CustomTabsIntent.Builder();

String menuItemTitle = App.s(R.string.share);
PendingIntent menuItemPendingIntent = createPendingIntentShare(url);
customTabsIntent.addMenuItem(menuItemTitle, menuItemPendingIntent);

我想在他的应用浏览器中添加类似Twitter的复制链接选项。我不确定如何在CustomTabs中复制链接到剪贴板。

enter image description here

1 个答案:

答案 0 :(得分:7)

创建BroadcastReceiver

public class CustomTabsBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String url = intent.getDataString();

        Toast.makeText(context, "Copy link pressed. URL = " + url, Toast.LENGTH_SHORT).show();

        //Here you can copy the URL to the clipboard
    }
}

AndroidManifest.xml

中注册
<receiver
    android:name=".CustomTabsBroadcastReceiver"
    android:enabled="true">
</receiver>

使用此方法启动自定义标签:

private void launchCustomTab() {
    Intent intent = new Intent(this, CustomTabsBroadcastReceiver.class);

    String label = "Copy link";
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder()
            .addMenuItem(label, pendingIntent)
            .build();

    customTabsIntent.launchUrl(this, Uri.parse("http://www.google.it"));
}