我想通过一个头部按钮控制我的壁球得分计数器应用程序。这意味着我想检测单击或双击并根据点击次数为第一或第二位玩家添加分数。
我无法使用长按,而不是双击,因为长按会激活Google即时。
答案 0 :(得分:1)
这是我在音乐播放器中用来处理单击和双击耳机控制的内容。
static final long CLICK_DELAY = 500;
static long lastClick = 0; // oldValue
static long currentClick = System.currentTimeMillis();
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_MEDIA_BUTTON)) {
KeyEvent keyEvent = (KeyEvent) intent.getExtras().get(Intent.EXTRA_KEY_EVENT);
if (keyEvent.getAction() != KeyEvent.ACTION_DOWN)return;
lastClick = currentClick ;
currentClick = System.currentTimeMillis();
if(currentClick - lastClick < CLICK_DELAY ){
//This is double click
} else {
//This is single click
}
}
答案 1 :(得分:0)
为时已晚,但也许有人会发现它很有用,例如Google音乐,Spotify等三次单击。
const val DOUBLE_CLICK_TIMEOUT = 430L
private var mHeadsetHookClickCounter = 0
override fun onMediaButtonEvent(mediaButtonEvent: Intent?): Boolean {
if (Intent.ACTION_MEDIA_BUTTON == mediaButtonEvent?.action) {
val ke = mediaButtonEvent.getParcelableExtra<KeyEvent>(Intent.EXTRA_KEY_EVENT)
if (ke != null && ke.keyCode == KeyEvent.KEYCODE_HEADSETHOOK) {
if (ke.action == KeyEvent.ACTION_UP) {
mHeadsetHookClickCounter = min(mHeadsetHookClickCounter+1, 3)
if (mHeadsetHookClickCounter == 3) {
handlingHeadsetClick()
} else {
Handler().postDelayed({
handlingHeadsetClick()
}, DOUBLE_CLICK_TIMEOUT)
}
}
return true
}
}
return super.onMediaButtonEvent(mediaButtonEvent)
}
private fun handlingHeadsetClick() {
logd("MediaSessionSupportFeature Handling headset click")
when(mHeadsetHookClickCounter) {
1 -> { service.get()?.togglePlayPause() }
2 -> { service.get()?.playNext() }
3 -> { service.get()?.playPrevious() }
}
// Reset Counter
mHeadsetHookClickCounter = 0
return
}