service - android clipboard listener

2019-03-11 06:27发布

I need a simple service (which will run in the background), when user copies anything from the browser or sms etc., there will be a toast showing that text.

example: enter image description here

this service must be run on android 2.1 and later.

Today (from 10:35 AM to now[11:11 PM]) I've been searching the internet and tested several codes, but so far I have not come to a conclusion.

Some users in response to questions like this suggested that the use of the (my-clips) project. I get this, you can download this. But this project is complex and I am confused.

can anyone show me a very simple example please? thank you


Edit:

this is simple app run on background andoird OS. When the user does not open this app and copies any text from the browser or sms etc., this app will be active and show a toast like this: You copy this: ...

4条回答
混吃等死
2楼-- · 2019-03-11 06:53

Here is what works for me.

First, the Broadcast:

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
        ComponentName service = context.startService(
                new Intent(context, ClipboardMonitor.class));
        if (service == null) {
            Log.e("TAG", "Can't start service");
        }
    } else {
        Log.e("TAG", "Recieved unexpected intent " + intent.toString());
    }
}

and then this is the service

private MonitorTask mTask = new MonitorTask();
private ClipboardManager mCM;

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

@Override
public void onCreate() {
    mCM = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
    mTask.start();
}


@Override
public void onDestroy() {
    mTask.cancel();
}

@Override
public void onStart(Intent intent, int startId) {
}

/**
 * Monitor task: monitor new text clips in global system clipboard and
 * new image clips in browser download directory
 */
private class MonitorTask extends Thread {

    private volatile boolean mKeepRunning = false;
    private String mOldClip = null;


    public MonitorTask() {
        super("ClipboardMonitor");
    }

    /** Cancel task */
    public void cancel() {
        mKeepRunning = false;
        interrupt();
    }

    @Override
    public void run() {
        mKeepRunning = true;

        while (true) {
            doTask();

            if (!mKeepRunning) {
                break;
            }
        }

    }

    private void doTask() {
        if (mCM.hasText()) {
            String newClip = mCM.getText().toString();
            if (!newClip.equals(mOldClip)) {

                mOldClip = newClip;
               // Toast.makeText(getApplicationContext(), "" +  newClip.toString(), Toast.LENGTH_SHORT).show();
                Log.i("TAG", "new text clip inserted: " + newClip.toString());
            }
        }
    }

Also, the permissions:

<uses-permission android:name="android.permission.GET_CLIPS" />
<uses-permission android:name="android.permission.READ_CLIPS" />
<uses-permission android:name="android.permission.WRITE_CLIPS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<service android:name=".ClipboardMonitor" />
<receiver android:name=".ClipboardMonitorStarter">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>
查看更多
狗以群分
3楼-- · 2019-03-11 06:55

the way i did it was:

final ClipboardManager clipboard = (ClipboardManager) this.getSystemService(Context.CLIPBOARD_SERVICE);
            clipboard.addPrimaryClipChangedListener( new ClipboardManager.OnPrimaryClipChangedListener() {
                public void onPrimaryClipChanged() {
                    String a = clipboard.getText().toString();
                    Toast.makeText(getBaseContext(),"Copy:\n"+a,Toast.LENGTH_LONG).show();
                }
            });

do it this way without service, add to manifest or anything, just open your app first then close it, and copy the text from anywhere to copy and show up in your app

查看更多
我想做一个坏孩纸
4楼-- · 2019-03-11 06:58

First, You need to add these permissions to AndroidManifest:

<uses-permission android:name="android.permission.GET_CLIPS" />
<uses-permission android:name="android.permission.READ_CLIPS" />
<uses-permission android:name="android.permission.WRITE_CLIPS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

Then, you need to add a service like this:

public class Clipboard extends Service {
private ClipboardManager mCM;
IBinder mBinder;
int mStartMode;

 @Override
 public int onStartCommand(Intent intent, int flags, int startId) {
    mCM = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
    mCM.addPrimaryClipChangedListener(new OnPrimaryClipChangedListener() {

        @Override
        public void onPrimaryClipChanged() {
            String newClip = mCM.getText().toString();
            Toast.makeText(getApplicationContext(), newClip.toString(),  Toast.LENGTH_LONG).show();
            Log.i("LOG", newClip.toString() + "");


        }
    });
    return mStartMode;
 }


@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
  }
 }

Add this service in AndroidManifest:

    <service android:name=".Clipboard" />

start service at MainActivity

    startService(new Intent(this, Clipboard.class));
查看更多
别忘想泡老子
5楼-- · 2019-03-11 07:08

for monitor Clipboard in android you need a service for monitoring clipboard and this service should be define in manifest. your clip board service is here

https://github.com/twaddington/Android-Clipboard-Monitor/blob/master/src/com/example/clipboardmonitor/service/ClipboardMonitorService.java

and manifest define is in the below

<service
        android:name=".service.ClipboardMonitorService"
        android:label="Clipboard Monitor"
        android:exported="false"/>
查看更多
登录 后发表回答