2016-02-26 1 views
3

Как добавить ссылку «Скопировать ссылку» в меню настроек пользовательских талонов Chrome в Android. Добавление пользовательских пунктов меню в 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

Heres ссылка на реализацию BroadcastReceiver, которая копирует ссылку в буфер обмена: https://gist.github.com/andreban/ccccade793f63ace207b – andreban

+0

Спасибо, это работает. –

Смежные вопросы