创建一个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"));
}