Friday 6 August 2021

KEYCODE_MEDIA_PLAY_PAUSE intercepted by Google Assistant

I wrote an Android app with a button that raises KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE to control third party media player apps. This works perfectly for several days or weeks, but then the key event starts being intercepted by Google Assistant, which is ostensibly turned off. Sometimes it pops up a notification inviting me to turn on various Google Assistant features. Sometimes it speaks the time. And sometimes it just makes the Google Assistant sound but does nothing else. In any case, sometimes the key event is also received by third party media players, sometimes not. Rebooting the device resolves the issue for another week or two.

The app is extremely simple. Here's the related code:

In onCreate:

    findViewById(R.id.play_button).setOnClickListener(view -> {
        sendKey(KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);
    });

And the definition of sendKey:

private void sendKey(int keyCode) {
    KeyEvent down = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
    mAudioManager.dispatchMediaKeyEvent(down);
    KeyEvent up = new KeyEvent(KeyEvent.ACTION_UP, keyCode);
    mAudioManager.dispatchMediaKeyEvent(up);
}

I know this kind of bizarre behavior is often device- or carrier-specific, so I don't know how to further investigate or report it unless someone else has encountered the same issue. Have you seen this, and is there a solution? The device I've experienced this on is a fully updated Motorola Moto G7 Supra from cricKet, running Android 10.



from KEYCODE_MEDIA_PLAY_PAUSE intercepted by Google Assistant

No comments:

Post a Comment