在前台监控Flutter中的剪贴板?

huangapple go评论85阅读模式
英文:

Monitor the clipboard in Foreground flutter?

问题

import 'package:flutter/services.dart';

void main() {
  // Initialize the clipboard monitoring
  ClipboardMonitor().startMonitoring();
}

class ClipboardMonitor {
  final Clipboard _clipboard = Clipboard();

  void startMonitoring() {
    _clipboard.clear(); // Clear clipboard initially
    _clipboard.listen((event) {
      String copiedText = event.text;
      if (copiedText.contains('instagram.com')) {
        // Trigger your notification or download process here
        // You can use plugins like 'flutter_local_notifications' for notifications
        // Handle the download process in your application
      }
    });
  }
}
英文:

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

答案1

得分: 3

我已经为此创建了一个Flutter插件。它在Android和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("剪贴板已更改:$text");
}
英文:

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

答案2

得分: 0

最后一个问题,据我理解,您想要监控剪贴板。因此,在Flutter中有一个适用于此的软件包。您可以查看此链接:

https://pub.dev/packages/clipboard_manager

英文:

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

huangapple
  • 本文由 发表于 2020年10月5日 15:06:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64203859.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定