微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我试着获得时间System.currentTimeMillis(),而不是用户按下媒体按钮.但是当媒体按钮再次上升时执行回调动作== KeyEvent.ACTION_UP.

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); 

日志输出

time stopped down: 1473420286380
time stopped up: 1473420286383

在我看来,这些价值之间的差异太小了.

解决方法:

我自己解决了这个问题.诀窍是使用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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐