android读取剪切板选中内容,Android 10 Flutter resume状态下读取剪切板内容

本文介绍在Flutter应用中从后台切换到前台时如何正确读取剪切板内容的方法,包括利用FocusNode和通过Android插件实现的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这样处理很多时候依旧获取不到,debug模式下可以,但release情况下很多时候无法获取,暂未知

刚用Flutter练手做了个小项目,有个需求就是当app由后台切回前台的时候,获取剪切板的内容,如果内容是一个网址,就进行解析

监听切回前台很简单,如下:

class _MyHomePageState extends State with WidgetsBindingObserver {

@override

void didChangeAppLifecycleState(AppLifecycleState state) {

if (state == AppLifecycleState.resumed) {

}

}

@override

void initState() {

WidgetsBinding.instance.addObserver(this);

super.initState();

}

@override

void dispose() {

WidgetsBinding.instance.removeObserver(this);

super.dispose();

}

}

如果在切回时去读取剪切板数据,例如:

@override

void didChangeAppLifecycleState(AppLifecycleState state) async {

super.didChangeAppLifecycleState(state);

switch (state) {

case AppLifecycleState.resumed:

ClipboardData data = await Clipboard.getData(Clipboard.kTextPlain);

print("onResumed __${data}");

break;

}

}

这个时候往往读取到的数据为空,因为在android 10中,我们读取剪切板数据往往在 onWindowFocusChanged 方法下读取,也就是说只有在窗体获取到焦点后才能读取剪切板内容,所以可以创建一个FocusNode,用于获取焦点

class _MyHomePageState extends State with WidgetsBindingObserver {

var _focusNode = FocusNode();

@override

void didChangeAppLifecycleState(AppLifecycleState state) {

if (state == AppLifecycleState.resumed) {

FocusScope.of(context).requestFocus(_focusNode);

}

}

@override

void initState() {

WidgetsBinding.instance.addObserver(this);

_focusNode.addListener(() async {

if (_focusNode.hasFocus) {

FocusScope.of(context).unfocus();

ClipboardData data = await Clipboard.getData(Clipboard.kTextPlain);

if (data != null) {

//处理剪切板内容

}

}

});

super.initState();

}

@override

void dispose() {

WidgetsBinding.instance.removeObserver(this);

super.dispose();

}

}

2021/03/11

上面的方法发现效果并不好,最后的还是通过安卓插件来实现,具体如下:

首先将项目下面的android文件夹下的MainActivity (java或者kt,如果是kt,改成java,kt不会。。。) 改成如下:

public class MainActivity extends FlutterActivity implements MethodChannel.MethodCallHandler{

private static final String CHANNEL = "plugins.flutter.io/windowFocusChangedListener";

private MethodChannel windowFocusChangedListenChannel;

@Override

public void onWindowFocusChanged(boolean hasFocus){

super.onWindowFocusChanged(hasFocus);

if (windowFocusChangedListenChannel != null)

windowFocusChangedListenChannel.invokeMethod("onWindowFocusChanged", hasFocus);

}

@Override

public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine){

super.configureFlutterEngine(flutterEngine);

windowFocusChangedListenChannel = new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL);

windowFocusChangedListenChannel.setMethodCallHandler(this);

}

@Override

public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result){

switch (call.method) {

case "onWindowFocusChanged":

result.success(call.arguments);

break;

default:

result.notImplemented();

}

}

}

回到flutter项目,添加代码如下:

if (defaultTargetPlatform == TargetPlatform.android) {

const MethodChannel windowsFocusChangedChannel =

const MethodChannel('plugins.flutter.io/windowFocusChangedListener');

windowsFocusChangedChannel.setMethodCallHandler(_onMethodCall);

}

Future _onMethodCall(MethodCall call) async {

switch (call.method) {

case 'onWindowFocusChanged':

bool focused = call.arguments;

if (focused) {

readFromClipboard();

}

return true;

}

throw MissingPluginException(

'${call.method} was invoked but has no handler',

);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值