I have implemented Background Service to listen to clipboard events system wide..
Service Implementation:
public class ClipService extends Service {
ClipboardManager cm;
@Override
public void onCreate() {
Log.d("FRAG","onCreate called...");
super.onCreate();
cm = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
cm.addPrimaryClipChangedListener(new ClipboardListener());
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
cm.removePrimaryClipChangedListener(new ClipboardListener());
}
class ClipboardListener implements ClipboardManager.OnPrimaryClipChangedListener{
@Override
public void onPrimaryClipChanged() {
Log.d("FRAG","onPrimaryClipChanged called..");
if(cm!=null) {
String s1 = cm.getPrimaryClip().getItemAt(0).coerceToText(ClipService.this).toString();
ContentValues cv = new ContentValues();
cv.put(DatabaseHelper.CONTENT,s1);
getContentResolver().insert(DataProvider.CONTENT_URI, cv);//Insert using Content provider
}
}
}
}
Now my concern is that for every text copied to clipboard three entries are getting inserted for single copy event...i.e. onPrimaryClipChanged is getting called three times..
i am using `Log.d("FRAG","onPrimaryClipChanged called.."); and it is getting logged 3 times so problem seems to be with function being called 3 times for each clipboard change event and not insert part of the code.
What might be the cause of it getting called 3 times?