永久监听剪贴板变化

24

我正在构建一个应用程序,它将启动一个能够监听剪贴板更改的服务。

我的真正目标是永久记录(并将其写入存储器)剪贴板中的每一次更改,因此当我启动我的应用程序时,我可以读取该服务编写的存储文件。这意味着,我的应用程序和服务之间不需要直接通信,也不需要使用唤醒锁来保持设备运行(因为当设备休眠时,剪贴板几乎不会发生变化)。

我正在使用处理程序定期检查剪贴板,我想知道如何实现clipboardListener以检查这些更改。

1个回答

54

找到它了!

我已经完成了这个,据我所知它完美地工作,而且在内存中的进程仅消耗3MB。我发布这篇文章是为了提供类似需求的人参考。

如果有任何错误,请指出 :D

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;

import android.app.Service;
import android.content.ClipData;
import android.content.ClipDescription;
import android.content.ClipboardManager;
import android.content.ClipboardManager.OnPrimaryClipChangedListener;
import android.content.Intent;
import android.os.IBinder;

public class CBWatcherService extends Service {

    private final String tag = "[[ClipboardWatcherService]] ";  
    private OnPrimaryClipChangedListener listener = new OnPrimaryClipChangedListener() {
        public void onPrimaryClipChanged() {
            performClipboardCheck();
        }
    };

    @Override 
    public void onCreate() {
        ((ClipboardManager) getSystemService(CLIPBOARD_SERVICE)).addPrimaryClipChangedListener(listener);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        File folder = new File(ClipboardCacheFolderPath);
        // ClipboardCacheFolderPath is a predefined constant with the path
        // where the clipboard contents will be written

        if (!folder.exists()) { folder.mkdir(); }
        return START_STICKY;
    }

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

    private void performClipboardCheck() {
        ClipboardManager cb = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        if (cb.hasPrimaryClip()) {
            ClipData cd = cb.getPrimaryClip();
            if (cd.getDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
                try {
                    File folder = new File(ClipboardCacheFolderPath);
                    if (!folder.exists()) { folder.mkdir(); }
                    Calendar cal = Calendar.getInstance();
                    String newCachedClip = 
                        cal.get(Calendar.YEAR) + "-" +
                        cal.get(Calendar.MONTH) + "-" +
                        cal.get(Calendar.DAY_OF_MONTH) + "-" +
                        cal.get(Calendar.HOUR_OF_DAY) + "-" +
                        cal.get(Calendar.MINUTE) + "-" +
                        cal.get(Calendar.SECOND);

                    // The name of the file acts as the timestamp (ingenious, uh?)
                    File file = new File(ClipboardCacheFolderPath + newCachedClip);
                    file.createNewFile();
                    BufferedWriter bWriter = new BufferedWriter(new FileWriter(file));
                    bWriter.write((cd.getItemAt(0).getText()).toString());
                    bWriter.close();
                }
                catch (IOException e) {
                    e.printStackTrace();
                }  
            }
        }
    }
}

2
+1。然而,我不知道如何在Oreo中处理这个问题。我已经使用了JobIntentService,但是安卓会杀死服务,因此它不再是永久的。 - Ikelie Cyril
我们必须使用向后兼容的workManager,但我需要一个解决方法来使用workManager完成同样的工作。 - Phaneendra Charyulu Kanduri

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接