【问题标题】:React native power button long press listener when app is running in background当应用程序在后台运行时反应本机电源按钮长按监听器
【发布时间】:2020-01-20 13:27:20
【问题描述】:

我想在用户长按电源按钮时调用 SOS 功能。 如果没有库可以做到这一点,是否有任何本地方法可以做到这一点?

【问题讨论】:

标签: javascript java android ios react-native


【解决方案1】:

您可以使用此库在按下主页按钮时运行代码

react-native-home-pressed

您可以更改键码以创建repository 的分支

并且,用计时器检查长按。

更新:

您可以使用此library 来获取按键向上和按键向下事件的键码事件。

例子:

componentDidMount() {
    KeyEvent.onKeyDownListener((keyEvent) => {
        if (keyEvent.keyCode === 'Keycode of the power button'){
            this.timeout = setTimeout(() => {
                //Your SOS Function here
            }, 1000)
        }
    });

    KeyEvent.onKeyUpListener((keyEvent) => {
        if (keyEvent.keyCode === 'Keycode of the power button'){
            clearTimeout(this.timeout)
        }
    })
}

componentWillUnmount() {
    KeyEvent.removeKeyDownListener();
    KeyEvent.removeKeyUpListener();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-19
    • 1970-01-01
    • 2016-06-26
    • 1970-01-01
    • 2020-07-26
    相关资源
    最近更新 更多