如何在后台捕获事件按以获取解锁/锁定按钮(物理)-Google Flutter

时间:2019-04-15 08:54:06

标签: dart flutter background-process

我正在编写一款具有解锁手机计数器功能的应用,我想在后台捕获事件按下按钮。当尝试事件按下时,计数器增加。

1 个答案:

答案 0 :(得分:0)

您可以使用HARDWARE_BUTTONS flutter软件包来完成

https://pub.dev/packages/hardware_buttons#-readme-tab-看到此

您喜欢实现 导入'dart:async';

import 'package:hardware_buttons/hardware_buttons.dart' as HardwareButtons;

两倍= 0;

StreamSubscription<HardwareButtons.LockButtonEvent> _lockButtonSubscription;



_homeButtonSubscription = HardwareButtons.homeButtonEvents.listen((event) {
  setState(() {
    _latestHardwareButtonEvent = 'HOME_BUTTON';
    times=times+1;
  });
});

_lockButtonSubscription = HardwareButtons.lockButtonEvents.listen((event) {
  setState(() {
    _latestHardwareButtonEvent = 'LOCK_BUTTON';
  });
});


@override
void dispose() {
 super.dispose();
 _lockButtonSubscription?.cancel();
}