我想在用户长按电源按钮时调用 SOS 功能。 如果没有库可以做到这一点,是否有任何 native 方法可以做到这一点?
最佳答案
您可以使用此库在按下主页按钮时运行代码
您可以更改创建 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();
}
关于javascript - 当应用程序在后台运行时,响应 native 电源按钮长按监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59824310/