3

I would like to detect the event of "copy to clipboard", so when a user, after having selected a string or url, tap on copy to clipboard.

Do you have any idea how to check this in an Android environment?

salvob
  • 1,300
  • 3
  • 21
  • 41
  • 1
    For Android: Check this http://stackoverflow.com/questions/6931359/how-to-listen-for-a-copy-in-android – hilzj May 11 '16 at 12:42
  • There is a tutorial on Google code - https://code.google.com/archive/p/my-clips/ Check out, you will like it. It's for clipboard – Jimit Patel May 11 '16 at 12:44
  • Thank you guys, I am gonna make this question just for Android. For a java application for computer I could create a new question. – salvob May 11 '16 at 12:59
  • Does it mean that we now can't copy a link to clipboard without being tracked? – Totoro May 14 '18 at 00:43

2 Answers2

6

You are looking for http://developer.android.com/reference/android/content/ClipboardManager.html

ClipboardManager
.addPrimaryClipChangedListener(new ClipboardManager.OnPrimaryClipChangedListener() {
  @Override
  protected void onPrimaryClipChanged() {
    Log.i("clipboard", "changed to:" + ClipboardManager.getText());
  }
});
Ábrahám Endre
  • 649
  • 6
  • 12
1

ClipboardManager.addPrimaryClipChangedListener and ClipboardManager.getText() are deprecated. New solution:

val clipboardManager = context.getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
clipboardManager.addPrimaryClipChangedListener {
    val clipboardAsText = clipboardManager.primaryClip?.getItemAt(0)?.text
    if (clipboardAsText != null) {
        Toast.makeText(context, "Text in clipboard: $clipboardAsText", Toast.LENGTH_SHORT).show()
    }
}
Cristan
  • 12,083
  • 7
  • 65
  • 69