栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Android onMediaButtonEvent中收听“ ACTION_DOWN”(按键)事件,以测量时间?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Android onMediaButtonEvent中收听“ ACTION_DOWN”(按键)事件,以测量时间?

我自己解决了这个问题。诀窍是使用

event.getDownTime()
以下示例对其进行解释:

    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.getEventTime():0.421

ACTION_DOWN:0.47

ACTION_UP:0.471

因此,现在我得到了用户按下按键的瞬间。

特别感谢Balkrishna Rawool



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/419190.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号