19

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

user2977452
  • 199
  • 1
  • 1
  • 6

3 Answers3

26

the way i did it was:

final ClipboardManager clipboard = (ClipboardManager) this.getSystemService(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

Googlian
  • 6,077
  • 3
  • 38
  • 44
Carlos Carrizales
  • 2,340
  • 3
  • 18
  • 24
  • How can I remove the `addPrimaryClipChangedListener()` @carlos-carrizales – Sp4Rx Nov 12 '16 at 17:55
  • 1
    @Sp4Rx You can use `clipboard.removePrimaryClipChangedListener(listener)`, see https://developer.android.com/reference/android/content/ClipboardManager.html#removePrimaryClipChangedListener(android.content.ClipboardManager.OnPrimaryClipChangedListener) – lucidbrot Dec 30 '17 at 19:37
  • 2
    This solution does not meet the following request: `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: ...` – Aydinozkan Feb 12 '19 at 15:32
  • For me this only works when my app is in the foreground. Once I switch away to another app, the monitoring no longer works until I bring my app back to the foreground. – jho Feb 21 '23 at 07:03
6

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"/>
Mahdi Azadbar
  • 1,330
  • 3
  • 17
  • 24
-5

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>
Matt
  • 74,352
  • 26
  • 153
  • 180