Android Chrome Custom Tabs add Copy Link to options menu

前端 未结 1 1935
醉话见心
醉话见心 2021-01-18 10:04

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         


        
相关标签:
1条回答
  • 2021-01-18 10:39

    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"));
    }
    
    0 讨论(0)
提交回复
热议问题