0

How do I monitor the clipboard in Flutter?

I want send a notification to user when the user copies an Instagram link in the Instagram app or other apps so the user can download the link in my application ..

For this I need to monitor the clipboard

in android (java) we can use "WatcherService" or use like this:

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();
                }
            });

How do I use the services or above code in Flutter?

note: I want it work if the user closes the application

note: The target is more on the Android side

AhmadReza
  • 421
  • 6
  • 20
  • Does this answer your question? [How do I monitor the clipboard in Flutter?](https://stackoverflow.com/questions/50647727/how-do-i-monitor-the-clipboard-in-flutter) – Akif Oct 05 '20 at 07:08
  • @Akif It will not work when the user closes the application – AhmadReza Oct 05 '20 at 07:10
  • What about this one? Does this answer your question? https://stackoverflow.com/questions/55018442/how-to-create-copy-to-clipboard-event-on-flutter – Akif Oct 05 '20 at 07:31
  • @Akif No, its just show how copy to clipboard! I want monitor the clipboard – AhmadReza Oct 05 '20 at 07:33
  • You can't, at least in Android 10 and above. – Ryan M Oct 06 '20 at 06:21

2 Answers2

3

I've created a flutter plugin for this purpose. It works on Android and iOS.

https://pub.dev/packages/clipboard_monitor

import 'package:clipboard_monitor/clipboard_monitor.dart';

void startClipboardMonitor()  {
    ClipboardMonitor.registerCallback(onClipboardText);
}

void stopClipboardMonitor()  {
    ClipboardMonitor.unregisterCallback(onClipboardText);
}

void onClipboardText(String text) {
    print("clipboard changed: $text");
}
mix1009
  • 179
  • 1
  • 7
0

For the last one, as far as I understand, you want to monitor the clipboard. So, there is a package for this in Flutter. You can check this:

https://pub.dev/packages/clipboard_manager

Akif
  • 7,098
  • 7
  • 27
  • 53