Reputation: 199
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:
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:
toast
like this: You copy this: ...
Upvotes: 19
Views: 20838
Reputation: 2340
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
Upvotes: 26
Reputation: 1350
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
and manifest define is in the below
<service
android:name=".service.ClipboardMonitorService"
android:label="Clipboard Monitor"
android:exported="false"/>
Upvotes: 6
Reputation: 9
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>
Upvotes: -5