Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClipBoardManager.OnPrimaryClipChangedListener Not Working

I want to get notified if the user copies something to the clipboard.

I created a service and started it in the HomeActivity, also I write a timer to check if my service is working. I checked and my service is working Log cat shows that. But my listener method is not working what I'm missing here?

ClipBoardWatcherService:

public class ClipBoardWatcherService extends Service {
private final String tag = "FameThings";

private ClipboardManager.OnPrimaryClipChangedListener listener = new ClipboardManager.OnPrimaryClipChangedListener() {
    @Override
    public void onPrimaryClipChanged() {
        performClipBoardCheck();
    }
};

public static void start(Context context) {
    Intent i = new Intent(context, ClipBoardWatcherService.class);
    context.startService(i);
}

@Override
public void onCreate() {
    ((ClipboardManager) getSystemService(CLIPBOARD_SERVICE)).addPrimaryClipChangedListener(this.listener);
    new Timer().scheduleAtFixedRate(new TimerTask(){
        @Override
        public void run(){
            Log.i("tag", "A Kiss every 5 seconds");
        }
    },0,5000);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
   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_URILIST)) {
            Log.i(tag, cd.getDescription().toString());
        }
    }
}
}

HomeActivity:

public class HomeActivity extends AppCompatActivity {

//...
@Override
protected void onCreate(Bundle savedInstanceState) {
    //...
    ClipBoardWatcherService.start(this);
    //...
  }
//...
}

Manifest:

    <service
        android:label="ClipBoard Listener"
        android:exported="false"
        android:name=".service.ClipBoardWatcherService" />

I launched my app and opened the Youtube App and I opened up a video and then I clicked on the share button and then clicked on the "Copy" button but nothing logged on LogCat, only my timer is working. LogCat:

  //...
    2019-10-12 00:11:32.925 9314-9424/com.famethings.android I/tag: A Kiss every 5 seconds
    2019-10-12 00:11:37.963 9314-9424/com.famethings.android I/tag: A Kiss every 5 seconds
    2019-10-12 00:11:42.963 9314-9424/com.famethings.android I/tag: A Kiss every 5 seconds
    2019-10-12 00:11:47.961 9314-9424/com.famethings.android I/tag: A Kiss every 5 seconds
    2019-10-12 00:11:52.940 9314-9424/com.famethings.android I/tag: A Kiss every 5 seconds
like image 282
Muhammed Ozdogan Avatar asked Oct 11 '19 21:10

Muhammed Ozdogan


1 Answers

Android Q: If the application is not the default IME or does not have input focus this return null. the same as OnPrimaryClipChangedListener.

https://developer.android.com/about/versions/10/privacy/changes#clipboard-data

https://shoewann0402.github.io/2019/03/27/android-q-beta-access-clipboard-data/

like image 76
qinmiao Avatar answered Oct 20 '22 11:10

qinmiao