gpt4 book ai didi

android - 音量键长按事件

转载 作者:行者123 更新时间:2023-11-29 01:10:16 25 4
gpt4 key购买 nike

我想在特定时间按下音量增大按钮时发出 toast 消息。

public class MainActivity extends Activity {
@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
if(keyCode==KeyEvent.KEYCODE_VOLUME_UP){
event.startTracking();
return true;
}
return super.onKeyDown(keyCode,event);
}
@Override
public boolean onKeyLongPress(int keyCode,KeyEvent event){
if(keyCode==KeyEvent.KEYCODE_VOLUME_UP){
Toast.makeText(this, "Volume Up Pressed", Toast.LENGTH_SHORT).show();
return true;
}
return onKeyLongPress(keyCode,event);
} }

这是在长按时发出 toast 消息但我想在按下按钮 5 秒时 toast ,我如何为此指定一个特定的持续时间!

最佳答案

声明全局计数变量:int count = 0int key, keyEvent;

在你的按键方法中添加这个

public class MainActivity extends Activity {
@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
if(keyCode==KeyEvent.KEYCODE_VOLUME_UP){
key = keyCode;
keyEvent = KeyEvent.KEYCODE_VOLUME_UP;
new CountDownTimer(5000, 1000) {

public void onTick(long millisUntilFinished) {
if(key==keyEvent){
count++;
}
else{
count = 0;
}
}
public void onFinish() {
if(count = 5){
//Notify Them it's been 5 seconds
}
count = 0;
}
}.start();
return true;
}
return super.onKeyDown(keyCode,event);
}

关于android - 音量键长按事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43681209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com