8

On my Samsung Tab 3 running Android 4.1.2 multiple copies to clipboard produce a clipboard containing each copy. This is seen through a button on the tight bottom of the slide-up keyboard.

I'd like to delete all these copies programmatically, however, the ClipboardManager doesn't appear to offer methods to do this. How can delete everything that has been copied to the clipboard?

Thanks,

Chris

GoRoS
  • 5,183
  • 2
  • 43
  • 66
user3594510
  • 99
  • 1
  • 1
  • 4

5 Answers5

15

ClipboardManager clipService = (ClipboardManager)activity.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clipData = ClipData.newPlainText("", "");
clipService.setPrimaryClip(clipData);

Darpan
  • 5,623
  • 3
  • 48
  • 80
Mergu.
  • 151
  • 1
  • 5
4

I don't think you can. It sounds like Samsung has written a clipboard extender that monitors clipboard events and retains a copy of everything.

There is no way to access the history without (the user) interacting with the history list via the UI.

One possible work-around: If the history list is of the limited/recycling variety (i.e. limited to 10 with new items overwriting old items), then you may be able to effectively erase it by repeatedly sending blank strings (or harmless non-duplicate strings such as 'empty1', 'empty2', etc..) Whatever you do, you'll end up overwriting something that the user has deemed to be important, and THE USER WILL HATE YOU.

Chris Thornton
  • 15,620
  • 5
  • 37
  • 62
3

From API 28

ClipboardManager mCbm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
mCbm.clearPrimaryClip()
Matt
  • 399
  • 3
  • 6
0

Using the manager you mentioned, you can set an empty string to it with setText to clear the clipboard.

ClipboardManager mClipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
mClipboardManager.setText("");
GoRoS
  • 5,183
  • 2
  • 43
  • 66
  • 4
    This adds a blank clip to the stack. The sensitive data previously copied remains available for all to see. That's what I'd like to delete. – user3594510 May 02 '14 at 23:03
  • And what about adding the empty String as a Clip? `ClipData data = mClipboardManager.newPlainText("", ""); mClipboardManager.setPrimaryClip(data);` – GoRoS May 03 '14 at 09:59
0

To actually delete the entries (instead of exchanging them with an empty string via setPrimaryClip()), with API level 28+:

ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
while (clipboardManager.hasPrimaryClip()) {
    clipboardManager.clearPrimaryClip()
}
barnabas
  • 129
  • 2
  • 4