3

I am trying to do a Notification Action (for Android 4.1+) that will copy some text to the clipboard. I read the reference about the Copy-Paste feature and now I have this:

        ClipboardManager clipboard = (ClipboardManager)
            mContext.getSystemService(Context.CLIPBOARD_SERVICE);
    ClipData clip = ClipData.newPlainText("simple text","Hello, World!");

I don't know how to put it into an Intent that will be put into a PendingIntent.

If you can give me some example and explain it - it will be great! I'm a new with android developing. Thank you in advance.

Edit: I found this: https://stackoverflow.com/a/12601766/1866009 but I don't understand it correctly.

2 Answers 2

8

With android developer's help the problem was solved by this code:

        BroadcastReceiver brCopy = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            ClipboardManager clipboard = (ClipboardManager)
                    mContext.getSystemService(Context.CLIPBOARD_SERVICE);
            ClipData clip = ClipData.newPlainText("label", "text");
            clipboard.setPrimaryClip(clip);
            Toast.makeText(mContext, "Copied!", Toast.LENGTH_SHORT).show();

        }
    };
    IntentFilter intentFilter = new IntentFilter("com.example.ACTION_COPY");
    mContext.registerReceiver(brCopy, intentFilter);

    Intent copy = new Intent("com.example.ACTION_COPY");
    PendingIntent piCopy = PendingIntent.getBroadcast(mContext, 0, copy, PendingIntent.FLAG_CANCEL_CURRENT);

    mBuilder.addAction(android.R.drawable.ic_menu_copy, "Copy", piCopy);
2
  • you could have put it on the question itself , with a horizontal divider . :) Jan 12, 2013 at 15:43
  • 1
    Aside: users here are encouraged to add their own answers, and generally discouraged from editing solutions retrospectively into their question. For future readers, it is much better to read a question how it was originally asked (with appropriate improvements) and then finding answers below. This improves the quality of the data carried by the API too. +1 to Dennis.
    – halfer
    Feb 2, 2013 at 15:12
4

check this tutorial about android notifications actions :

http://www.vogella.com/articles/AndroidNotifications/article.html

when creating the pendingIntent , give it a custom intent that will be received by your broadcastReceiver (defined in your manifest, example here ) , and then do whatever you wish with it (for example copy to the clipboard) .

1
  • Thank you very much! I read these tutorials and solved my problem!
    – Dennis
    Jan 12, 2013 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.