0

I am trying to use the clipboard in android as described here: https://www.tutorialspoint.com/android/android_clipboard.htm

ClipboardManager myClipboard;
            myClipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);


            ClipData clip = ClipData.newPlainText("code", eventCode);
            myClipboard.setPrimaryClip(clip);

However, getSystemService requires 2 arguments instead of just 1, but I can't find anything about it.

Thanks to Usama Altaf's comment, I was able to solve this issue. This is my now working code:

ClipboardManager myClipboard;
            ClipboardManager clipboard = context.getApplicationContext().getSystemService(ClipboardManager.class);

            ClipData clip = ClipData.newPlainText("code", eventCode);
            clipboard.setPrimaryClip(clip);
J.B.
  • 51
  • 1
  • 2
  • 10
  • "However, getSystemService requires 2 arguments instead of just 1" -- where does this code reside? The tutorial that you are reading appears to assume that the code resides in an activity or other `Context` subclass, and there, `getSystemService()` takes a single parameter. – CommonsWare Jul 01 '21 at 11:30
  • `ClipboardManager clipboard = (ClipboardManager) getApplicationContext().getSystemService(CLIPBOARD_SERVICE);` – Usama Altaf Jul 01 '21 at 11:33
  • @CommonsWare This code resides in an adapter class. – J.B. Jul 01 '21 at 11:35
  • You will need to call `getSystemService()` on your `Activity`, pass the `ClipboardManager` into the adapter, or move your "copy stuff to the clipboard" logic out of the adapter. – CommonsWare Jul 01 '21 at 11:52

0 Answers0