Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

android - Permanently listen to Clipboard changes

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

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).

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

FOUND IT!

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.

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();
                }  
            }
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...