3

当我将文本复制到剪贴板时,onPrimaryClipChanged 方法被调用了两次。任何想法为什么?

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    final ClipboardManager cliboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);

    cliboardManager
            .addPrimaryClipChangedListener(new OnPrimaryClipChangedListener() {

                @Override
                public void onPrimaryClipChanged() {
                    ClipData clipData = cliboardManager.getPrimaryClip();
                    System.out
                            .println("********** clip changed, clipData: "
                                    + clipData.getItemAt(0));
                }
            });
    return true;
}

测试用例:从 BBC 网站复制文本“继续”将产生以下输出:

继续

继续

但是,如果我调试程序,我可以看到 clipData 对象具有值:

ClipData { text/plain {T:continue } }

第一次调用 onPrimaryClipChanged() 并且

ClipData { text/plain "BBC - 主页" {T:continue } }

下一次 onPrimaryClipChanged() 被调用。

所以基本上第一次 ClipDescription 是 { text/plain } 而第二次是 ClipDescription { text/plain "BBC - Homepage" } (即包括网页的标题)

4

4 回答 4

9

我假设您没有注册多个听众,我不能说这是错误,您仍然可以解决它。尝试这样的事情:

   String mPreviousText = "";

   cliboardManager
                .addPrimaryClipChangedListener(new OnPrimaryClipChangedListener() {

                    @Override
                    public void onPrimaryClipChanged() {
                        ClipData clipData = cliboardManager.getPrimaryClip();
                        System.out
                                .println("********** clip changed, clipData: "
                                        + clipData.getItemAt(0));
                         ClipData.Item item = clipData.getItemAt(0);
                         if(mPreviousText.equals(item.getText().toString())) return;
                         else{
                            /// do something
                            mPrevousText = item.getText().toString();
                         }
                    }
                });
于 2013-08-22T23:10:33.257 回答
6

我也遇到这个问题,终于知道为什么会调用多次了!!!

通常我们addPrimaryClipChangedListener(),但我们没有removePrimaryClipChangedListener()

请参阅:http: //developer.android.com/reference/android/content/Context.html#CLIPBOARD_SERVICE

与 getSystemService(String) 一起使用来检索 ClipboardManager 以访问和修改全局剪贴板的内容。

这意味着我们应该removePrimaryClipChangedListener()手动!

我的解决方案代码:

ClipboardManager myClipBoard ;
static boolean bHasClipChangedListener = false;

ClipboardManager.OnPrimaryClipChangedListener mPrimaryClipChangedListener = new ClipboardManager.OnPrimaryClipChangedListener() {
    public void onPrimaryClipChanged() {
        ClipData clipData = myClipBoard.getPrimaryClip();
        Log.d("henrytest", "********** clip changed, clipData: " + clipData.getItemAt(0));
    }
};

private void RegPrimaryClipChanged(){
    if(!bHasClipChangedListener){
        myClipBoard.addPrimaryClipChangedListener(mPrimaryClipChangedListener);
        bHasClipChangedListener = true;
    }
}
private void UnRegPrimaryClipChanged(){
    if(bHasClipChangedListener){
        myClipBoard.removePrimaryClipChangedListener(mPrimaryClipChangedListener);
        bHasClipChangedListener = false;
    }
}

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

@Override
protected void onResume() {
    super.onResume();
    RegPrimaryClipChanged();
}

@Override
protected void onPause() {
    super.onPause();
    UnRegPrimaryClipChanged();
}

@Override
protected void onStop() {
    super.onStop();
    //UnRegPrimaryClipChanged();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    //UnRegPrimaryClipChanged();
}
于 2015-01-30T07:04:46.273 回答
2

这是我防止 ClipboardManager OnPrimaryClipChangedListener 为每个副本调用两次的解决方法。

long lastCopiedTime = 0;
ClipboardManager.OnPrimaryClipChangedListener onPrimaryClipChangedListener = new ClipboardManager.OnPrimaryClipChangedListener() {
    @Override
    public void onPrimaryClipChanged() {
                ClipboardManager clipBoard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                String pasteData = "";
                ClipData.Item item = clipBoard.getPrimaryClip().getItemAt(0);
                if (System.currentTimeMillis() - lastCopiedTime > TimeUnit.SECONDS.toMillis(1)) {
                    if (StringUtils.isNotBlank(item.getText())) {
                        String s = item.getText().toString();
                        if (StringUtils.isNotBlank(s)) {
                            pasteData = s.trim();
                            if (StringUtils.isNotBlank(pasteData)) {
                                  /// do something here
                            }
                        }
                    }
                }
                lastCopiedTime = System.currentTimeMillis();
            }
        }
    }
};
于 2018-03-19T11:30:28.490 回答
0

// 防止 OnPrimaryClipChangedListener 的重复动作。

private ExecutorService mThreadPool = Executors.newSingleThreadExecutor();
static boolean isExpireData = true;

class hasExpireData implements Runnable {

    @Override
    public void run() {
        // TODO Auto-generated method stub

        isExpireData = true;
    }

}

private ClipboardManager.OnPrimaryClipChangedListener mOnPrimaryClipChangedListener =
        new ClipboardManager.OnPrimaryClipChangedListener() {
    @Override
    public void onPrimaryClipChanged() {
        Log.d(TAG, "onPrimaryClipChanged");
        ClipData clip = mClipboardManager.getPrimaryClip();

        Thread expireData = new Thread(new hasExpireData());
        new Handler().postDelayed(expireData, 500); // 0.5 seconds wait...

        if(isExpireData) {
            isExpireData = false;
            mThreadPool.execute(new TextRunnable(
                    clip.getItemAt(0).getText()));
        }

    }
};

private class TextRunnable implements Runnable {
    public TextRunnable(CharSequence text) {
        // text store to this class
    }

    @Override
    public void run() {
        // text other process
    }
}
于 2016-05-16T23:47:50.243 回答