How to perform an action whenever someone copies some text in the keyboard of Android? And would you have to use a service to let it run all the time?
How to perform an action whenever someone copies some text in the keyboard of Android? And would you have to use a service to let it run all the time?
You are looking for method addPrimaryClipChangedListener
of ClipboardManager
. It receives as parameter ClipboardManager.OnPrimaryClipChangedListener
.
Assuming you're familiar with Listeners, the code looks something like this:
final ClipboardManager gerenciadorDeAreaDeTransferencia =
(ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
gerenciadorDeAreaDeTransferencia.
addPrimaryClipChangedListener(new OnPrimaryClipChangedListener() {
@Override
public void onPrimaryClipChanged() {
// seu código
}
}
);
Note that this only works from the API 11.
About running in the background, you'll probably depend on a service, yes, but the details of that are subject to another question.