如何防止剪贴板监视器复制剪贴板数据两次?

时间:2018-02-22 01:47:40

标签: java android clipboard clipboarddata clipboardmanager

我一直在研究一个简单的剪贴板监视器,当剪贴板发生变化时,它会将剪贴板的内容保存在文件文本中。一切正常,但我注意到每当我复制一些东西时,文本会连续复制两次(在图像中,数据库单词是复制的单词)。

You can see here that the text is copied twice  好像这个函数快速连续调用了两次,我似乎无法理解为什么?

public class ClipboardMonitorService extends Service {
private static final String TAG = "ClipboardManager";
private static final String FILENAME = "clipboard-history.txt";

private File mHistoryFile;
private ExecutorService mThreadPool = Executors.newSingleThreadExecutor();

private ClipboardManager mClipboardManager;

@Override
public void onCreate() {
    super.onCreate();


    mHistoryFile = new File(getExternalFilesDir(null),FILENAME);

    mClipboardManager =
            (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
    mClipboardManager.addPrimaryClipChangedListener(mOnPrimaryClipChangedListener);

}



@Override
public void onDestroy() {
    super.onDestroy();

    if (mClipboardManager != null) {
        mClipboardManager.removePrimaryClipChangedListener(
                mOnPrimaryClipChangedListener);
    }
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

private boolean isExternalStorageWritable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
    }
    return false;
}

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

                mThreadPool.execute(new WriteHistoryRunnable(
                        clip.getItemAt(0).getText()));

            }
        };

private class WriteHistoryRunnable implements Runnable {
    private final Date mNow;
    private final CharSequence mTextToWrite;

    public WriteHistoryRunnable(CharSequence text) {
        mNow = new Date(System.currentTimeMillis());
        mTextToWrite = text;
    }

    @Override
    public void run() {
        if (TextUtils.isEmpty(mTextToWrite)) {
            // Don't write empty text to the file
            /*
            TextUtils.isEmpty(string) will return a boolean value so if true nothing is return
            i.e it does nothing
             */
            return;
        }

        if (isExternalStorageWritable()) {
            try {
                Log.i(TAG, "Writing new clip to history:");
                Log.i(TAG, mTextToWrite.toString());
                BufferedWriter writer =
                        new BufferedWriter(new FileWriter(mHistoryFile,true));
                writer.write(String.format("[%s]: ", mNow.toString()));
                writer.write(mTextToWrite.toString());
                writer.newLine();
                writer.close();
            } catch (IOException e) {
                Log.w(TAG, String.format("Failed to open file %s for writing!",
                        mHistoryFile.getAbsoluteFile()));
            }
        } else {
            Log.w(TAG, "External storage is not writable!");
        }
    }
} 
}

1 个答案:

答案 0 :(得分:1)

查看复制数据FROM的位置是否重要。即尝试普通的旧Windows记事本。如果它在那里工作正常,但不是来自其他应用程序,那么问题可能在于您正在复制的应用程序。并非所有应用都遵守规则。有时,当用户仅执行一次操作时,应用程序将多次更新剪贴板,例如每种数据格式更新一次 复制图形时,Excel用于更新剪贴板24次以上!