23

クリップボードの変更をリッスンできるサービスを起動するアプリケーションを構築しています。

私が本当に望んでいるのは、クリップボードのすべての変更を永続的に記録 (およびストレージに書き込む) ことです。そのため、アプリを起動すると、そのサービスによって書き込まれた保存ファイルを読み取ることができます。これは、アプリとサービスの間で直接通信する必要がなく、デバイスを維持するためにウェイクロックを使用する必要がないことを意味します (デバイスがスリープしている間はクリップボードがほとんど変更されないため)。

ハンドラーを使用してクリップボードを繰り返しチェックしています。それらの変更をチェックするために clipboardListener を実装する方法を知りたいです。

4

1 に答える 1

53

それを見つけた!

私はこれを行いました。これは問題なく動作し、メモリ内のプロセスは 3 MB しか消費しません。誰かが同様のものが必要な場合に備えて、これを投稿しています。

間違いがあれば指摘してください :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();
                }  
            }
        }
    }
}
于 2014-03-09T19:45:41.040 に答える