Android Chrome Custom Tabs add Copy Link to options menu

十年热恋 提交于 2020-01-11 09:03:10

问题


How to add 'Copy link' option to Chrome Custom Tabs options menu in Android. Adding custom menu items in CustomTabs is like this.

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

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

I want to add Copy Link option just like Twitter do in his app browser. I am not sure how I can copy link to Clipboard in CustomTabs.


回答1:


Create a 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
    }
}

Register it in the AndroidManifest.xml:

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

Use this method to launch the Custom Tab:

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"));
}


来源:https://stackoverflow.com/questions/35649468/android-chrome-custom-tabs-add-copy-link-to-options-menu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!