【问题标题】:Permanently listen to Clipboard changes永久收听剪贴板更改
【发布时间】:2016-05-02 02:36:29
【问题描述】:

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

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

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

【问题讨论】:

    标签: android android-service clipboard clipboardmanager


    【解决方案1】:

    找到了!

    我已经这样做了,它完美无瑕地运行,内存中的进程只消耗 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();
                    }  
                }
            }
        }
    }
    

    【讨论】:

    • 为此 +1。但是,我不知道如何在奥利奥中解决这个问题。我使用了 JobIntentService,但是 android 杀死了该服务,因此它不再是永久的。
    • 我们必须使用向后兼容的workManager,但我需要解决问题才能对workManager做同样的事情
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-21
    • 1970-01-01
    相关资源
    最近更新 更多