Search code examples
androidserviceclipboardmanager

Copy and Paste the text (Android App)


I am building ClipboardManager app which is used for copying the text and pasting to the app. I used OnPrimaryClipChangedListener to listen the text that is copied to clipboard and I run OnPrimaryClipChangedListener on app service and that will paste the text automatically to my app. But the problem is that when I open ClipboardManager app and copy the text from own app, it again trigger OnPrimaryClipChangedListener and paste the data again. How to filter the text that is copied from ClipboardManager app or any other app? If that copied from our own app then discard else save the data to ClipboardManager database.


Solution

  • private OnPrimaryClipChangedListener listener = new OnPrimaryClipChangedListener() {
        public void onPrimaryClipChanged() {
            performClipboardCheck();
        }
    };
    
    private void performClipboardCheck {
        ClipboardManager cb = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
            if (cb.hasPrimaryClip()) {
                ClipData cd = cb.getPrimaryClip();
                cd.getItemAt(0).getText()
            }
    };
    

    from Permanently listen to Clipboard changes

    That is probably what you want, that also means this is a duplicate...