Search code examples
androidkeyevent

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.


Solution

  • A deleted answer linked to this article: How to Stop Google Assis­tant from Pop­ping up Randomly. The article seems to have been written for Android 9, and some of the options have already changed in Android 10, so reproducing the content of the article wouldn't be very valuable. The takeaway from the article is that turning off the Google Assistant malware doesn't actually turn it off. You have to opt out in multiple places, and Google keeps adding more. Anyone who finds this Q&A in the future should browse the settings for more new switches.

    As I mentioned in the question, Google Assistant was already "turned off" on my device (procedure 1, step 4 in the article). But there's a new section of Assistant Settings titled Your Apps. Each app listed there has a switch labeled Let your Assistant learn from this app.

    My device had been running for a couple weeks and Google Assistant was messing with my key events. I turned off the "learn" switch for the app in question and the problem went away without a reboot.

    Edit: approximately three weeks after turning off "learning" seemed to solve the problem, it came back. I have now resorted to force closing and disabling the Google app.