How to listen for a copy in Android

后端 未结 3 1636
-上瘾入骨i
-上瘾入骨i 2021-01-11 18:30

To get myself a little more familiar with content providers in Android, I\'m making a small clipboard manager app. Its core functionality is to simply add whatever you copy

3条回答
  •  鱼传尺愫
    2021-01-11 18:54

    Put inside your class:

    ClipboardManager.OnPrimaryClipChangedListener mPrimaryChangeListener = new ClipboardManager.OnPrimaryClipChangedListener() {
            public void onPrimaryClipChanged() {
    
                // this will be called whenever you copy something to the clipboard
            }
        };
    

    Put this inside onCreate method:

    ClipboardManager clipboard = (ClipboardManager) this.getSystemService(Context.CLIPBOARD_SERVICE);
    clipboard.addPrimaryClipChangedListener(mPrimaryChangeListener);
    

    That's all. I hope I helped you.

提交回复
热议问题