2016-02-26 42 views
3

如何將「複製鏈接」選項添加到Android中的Chrome自定義標籤選項菜單。在CustomTabs中添加自定義菜單項就像這樣。Android Chrome自定義標籤添加複製鏈接到選項菜單

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

回答

6

創建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")); 
} 
+2

繼承人鏈接到BroadcastReceiver實施,將鏈接複製到剪貼板:https://gist.github.com/andreban/ccccade793f63ace207b – andreban

+0

謝謝,它的工作原理。 –

相關問題