如何收听“ACTION_DOWN” (关键按下)事件在Android onMediaButtonEvent中,为了衡量时间? [英] How to listen to "ACTION_DOWN" (key pressed) event in Android onMediaButtonEvent, in order to measure the time?
问题描述
我试图获得时间 System.currentTimeMillis()
,而不是用户按下媒体按钮。但是当媒体按钮再次上升时执行回调 action == KeyEvent.ACTION_UP
。
I try to get the time System.currentTimeMillis()
, than the user presses the media button. But the callback is executed at the time when the media button goes up again action == KeyEvent.ACTION_UP
.
我不想在我的解决方案中使用BroadcastReceiver。
I don't want to use a BroadcastReceiver for my solution.
这是我的代码:
MediaSession audioSession = new MediaSession(getApplicationContext(), "TAG");
audioSession.setCallback(new MediaSession.Callback() {
@Override
public boolean onMediaButtonEvent(final Intent mediaButtonIntent) {
String intentAction = mediaButtonIntent.getAction();
if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction))
{
KeyEvent event = (KeyEvent)mediaButtonIntent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (event != null)
{
int action = event.getAction();
if (action == KeyEvent.ACTION_DOWN) {
long stopTimeOfGame_millis = System.currentTimeMillis();
UtilsRG.info("time stopped: " +stopTimeOfGame_millis);
}
if (action == KeyEvent.ACTION_UP) {
long test = System.currentTimeMillis();
UtilsRG.info("time stopped up: " +test);
}
}
}
return super.onMediaButtonEvent(mediaButtonIntent);
}
});
PlaybackState state = new PlaybackState.Builder()
.setActions(PlaybackState.ACTION_PLAY_PAUSE)
.setState(PlaybackState.STATE_PLAYING, 0, 0, 0)
.build();
audioSession.setPlaybackState(state);
audioSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS | MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
audioSession.setActive(true);
日志输出:
时间停止:1473420286380
时间停止:1473420286383
time stopped down: 1473420286380
time stopped up: 1473420286383
在我看来差异在这些值之间太小了。
In my opinion the difference is too small between these values.
推荐答案
我自己解决了这个问题。诀窍是使用 event.getDownTime()
以下示例解释:
I fixed the issue by my self. The trick is to use event.getDownTime()
Following example explains it:
audioSession = new MediaSession(getApplicationContext(), "TAG");
audioSession.setCallback(new MediaSession.Callback() {
@Override
public boolean onMediaButtonEvent(final Intent mediaButtonIntent) {
String intentAction = mediaButtonIntent.getAction();
if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
KeyEvent event = mediaButtonIntent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (event != null) {
stopTimeOfGame_millis = event.getDownTime();
double usersReactionTime = (event.getDownTime() - startTimeOfGame_millis) / 1000.0;
UtilsRG.info("event.getDownTime(): " + usersReactionTime);
double getEventTime = (event.getEventTime() - startTimeOfGame_millis) / 1000.0;
UtilsRG.info("event.getEventTime(): " + getEventTime);
int action = event.getAction();
if (action == KeyEvent.ACTION_DOWN) {
long action_down = android.os.SystemClock.uptimeMillis();
double actionDown = (action_down - startTimeOfGame_millis) / 1000.0;
UtilsRG.info("ACTION_DOWN: " + actionDown);
}
if (action == KeyEvent.ACTION_UP) {
long action_up = android.os.SystemClock.uptimeMillis();
double actionUp = (action_up - startTimeOfGame_millis) / 1000.0;
UtilsRG.info("ACTION_UP: " + actionUp);
}
}
}
return true;
}
});
PlaybackState state = new PlaybackState.Builder()
.setActions(PlaybackState.ACTION_PLAY_PAUSE)
.setState(PlaybackState.STATE_PLAYING, 0, 0, 0)
.build();
audioSession.setPlaybackState(state);
audioSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS | MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
audioSession.setActive(true);
我收到了以下日志:
event.getDownTime():0.281
event.getDownTime(): 0.281
event.getEventTime():0.421
event.getEventTime(): 0.421
ACTION_DOWN:0.47
ACTION_DOWN: 0.47
ACTION_UP:0.471
ACTION_UP: 0.471
因此现在我得到了片刻当用户按下键时。
Thus now I got the moment when the user presses the key down.
特别感谢Balkrishna Rawool
Special Thanks goes to Balkrishna Rawool
这篇关于如何收听“ACTION_DOWN” (关键按下)事件在Android onMediaButtonEvent中,为了衡量时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!