Android Question How to listen (monitor) Clipboard changes

somed3v3loper

Well-Known Member
Licensed User
Longtime User
Hi all ,

I am trying to make an application that listen to clipboard changes but do not know how exactly to do it .
I think B4A does not have any built-in function or way to do so .

So I am trying to get ClipboardManager as I read that it has listening ability .

This is one of my many attempts


B4X:
    Dim myClipboard As JavaObject
    myClipboard.InitializeStatic("android.content.Context")
      myClipboard.RunMethod("getSystemService",Array As String("CLIPBOARD_SERVICE"))

This code raises the error

B4X:
java.lang.IllegalArgumentException: expected receiver of type android.content.Context, but got java.lang.Class<android.content.Context>

Please help me I am still novice especially in Java and Android APIs and if there is some intent for clipboard that would be better

Thanks in advance
 

somed3v3loper

Well-Known Member
Licensed User
Longtime User
There's a clipboard library, search the forums.

It is just for seting,getting clipboard text and checking if there is a text .

I need to monitor clipboard so when the user copies any text my application (background service) gets notified.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
If you are using B4a 3.8 + you can use JavaObject to set the listener, see the attached example which uses the clipboard library to get the clipboard contents.
 

Attachments

  • ClipListener.zip
    7 KB · Views: 687
Upvote 0

somed3v3loper

Well-Known Member
Licensed User
Longtime User
Thank you very much Stevel
I added it to service to monitor clipboard even if activity is closed but can we know how long will it be listening to clipboard changes ? Will OS kill it after some time ?
I want it to be always listening.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Unfortunately there doesn't appear to have been a listener exposed for the clipboard prior to Api 11.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
What is Babylon? It may use native libraries, which I'm afraid is beyond me.
 
Upvote 0

somed3v3loper

Well-Known Member
Licensed User
Longtime User
Upvote 0

stevel05

Expert
Licensed User
Longtime User
As it is using notifications, it is possible that the clipboard is being monitored by code in a service. You cold poll the clipboard to see if the text has changed. I'm not sure what effect it would have on the battery life if it was on all the time though.
 
Upvote 0

somed3v3loper

Well-Known Member
Licensed User
Longtime User
As it is using notifications, it is possible that the clipboard is being monitored by code in a service. You cold poll the clipboard to see if the text has changed. I'm not sure what effect it would have on the battery life if it was on all the time though.

Thanks a lot Stevel
I think I will go with checking Clipboard every some seconds but I hope we can get better solution.
 
Upvote 0
Top