永久收听到剪贴板变化 [英] Permanently listen to Clipboard changes

查看:204
本文介绍了永久收听到剪贴板变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建,这将能够推出一项服务来听剪贴板变化的应用程序。

I'm building an application that will launch a service capable to listen to clipboard changes.

我真正想要的是记录(在存储写入)剪贴板中的每一个微小变动永久,所以当我启动我的应用程序,我可以读取写入由该服务存储的文件。这意味着,没有必要为我的应用程序和服务,而且也没有必要使用wakelocks以保持设备最多(因为剪贴板上几乎没有变化,而该设备处于睡眠状态)之间的直接通信。

What i really want is to record (and write it in storage) every single change in the clipboard permanently, so when i launch my app i can read the stored files written by that service. This means, there's no need for direct communication between my app and the service and there's no need to use wakelocks to keep the device up (since the clipboard hardly changes while the device is asleep).

我使用一个处理程序来反复地检查剪贴板,我想知道我怎么能实现clipboardListener检查这些变化。

I'm using a handler to recurrently check the clipboard, I want to know how can I implement the clipboardListener to check for those changes.

推荐答案

找到了!

我已经做到了这一点,它完美AFAIK,这个过程仅在内存消耗3MB。 我张贴这种万一有人可能需要类似的东西。

I have done this, it works flawlessly afaik, and the process in memory only consumes 3mb. I'm posting this in case someone might need something similar.

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

If there are any errors, please point them out :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();}  
            } 
        }}
}

这篇关于永久收听到剪贴板变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆