how to add clipboard option to action_send intent

2019-09-02 18:38发布

问题:

Somehow I've seen an application that can add Clipboard option to share options.

Something like this screenshot

How can I do that?

I tried to add extra intent, but the additional intent didn't show up

Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_TEXT, "Extra Text");
    intent.setType("text/plain");

    Intent clipboardIntent = new Intent();
    clipboardIntent.putExtra(Intent.EXTRA_TEXT, "Extra Text");
    clipboardIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");

    Intent chooserIntent = Intent.createChooser(intent, "Chooser Title");
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { clipboardIntent });
    startActivity(chooserIntent);

Thank you :D

回答1:

ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
clipboard.setText("Text to copy");

Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "Extra Text"+clipboard.getText());
intent.setType("text/plain");



Intent chooserIntent = Intent.createChooser(intent, "Chooser Title");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { clipboardIntent });
startActivity(chooserIntent);