0

What i want is when user copies data from my application a string get added with the copied data.Or there is another way to do that.

I tried this:

ClipboardManager.OnPrimaryClipChangedListener mPrimaryChangeListener = new ClipboardManager.OnPrimaryClipChangedListener() {
    public void onPrimaryClipChanged() {

        ClipboardManager clipBoard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        String cb = (String) clipBoard.getText();
        String tex = "hi i am here";
        String con = cb.concat(tex);
        clipBoard.setText(con);

        clipBoard.addPrimaryClipChangedListener(mPrimaryChangeListener);

    }
};

I put this code in oncreate.But it is not working I am new in android world.How to implement this.

Puneet Kushwah
  • 1,495
  • 2
  • 17
  • 35

1 Answers1

1
ClipboardManager myClipBoard ;

@Override
protected void onCreate(Bundle savedInstanceState)
{                   
    myClipBoard = (ClipboardManager) Clipboard.this.getSystemService(android.content.Context.CLIPBOARD_SERVICE);
    myClipBoard.addPrimaryClipChangedListener(mPrimaryClipChangedListener);

}

ClipboardManager.OnPrimaryClipChangedListener mPrimaryClipChangedListener = new ClipboardManager.OnPrimaryClipChangedListener() {
        public void onPrimaryClipChanged() {                
            ClipData clipData = myClipBoard.getPrimaryClip();

             ClipData.Item item = clipData.getItemAt(0);                                                 
             String tex = ", hi i am here";
             String con = item.getText().toString().concat(tex);

             myClipBoard.removePrimaryClipChangedListener(mPrimaryClipChangedListener);
             myClipBoard.setText(con);
             myClipBoard.addPrimaryClipChangedListener(mPrimaryClipChangedListener);

        }
};
HenryChuang
  • 1,449
  • 17
  • 28
  • it is working fine for the first time, but after that it is adding "hi i am there " many times....i can able to find why? – Puneet Kushwah Jan 27 '15 at 09:48
  • you have to check "if(mPreviousText.equals(item.getText())) return;", or it will copy multi times. But why it call onPrimaryClipChanged() multi times, i don't know :) – HenryChuang Jan 27 '15 at 10:05
  • i know why call multi times, because your code "myClipBoard.setText(con)" will call "onPrimaryClipChanged()", i modify answer, cheers – HenryChuang Jan 28 '15 at 03:44
  • in this line myClipBoard = (ClipboardManager) Clipboard.this.getSystemService(android.content.Context.CLIPBOARD_SERVICE); it is giving error at Clipboard.this....... – Puneet Kushwah Jan 28 '15 at 05:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/69713/discussion-between-puneet-kushwah-and-henrychuang). – Puneet Kushwah Jan 28 '15 at 05:59
  • change Clipboard.this to YourActivity.this – HenryChuang Jan 28 '15 at 05:59
  • I tried a lot of things, i added the toast n the last addprimaryClipChangeListener is making the text to concat 3 times....can you help me – Puneet Kushwah Jan 28 '15 at 09:18
  • i know why it call multi tmies, see my answer : http://stackoverflow.com/questions/18391701/clipboardmanager-onprimaryclipchangedlistener-is-called-twice-for-every-copy/28230843#28230843 – HenryChuang Jan 30 '15 at 07:06
  • @HenryChaung i tried to modify this code with reference to the above post, but i cant able to do so so can you please edit it....Thanks... – Puneet Kushwah Feb 12 '15 at 12:44