androidandroid-broadcastlong-click

Google now won't let my app detect headset button long click


I want my app to do some specific actions when user long clicks (press and hold) headset button. I use a BroadcastReceiver for MEDIA_BUTTON and it works fine in phones who doesn't run Google now app for sound actions for long press. But in the phones that automatically run Google now my app is just being ignored. How can I disable Google now and detect headset button long click. here is onReceive method in my BroadcastReceiver class.

public void onReceive(Context context, Intent intent) {
String intentAction = intent.getAction();
    if (!Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
         return;
    }
    KeyEvent event = (KeyEvent) intent
            .getParcelableExtra(Intent.EXTRA_KEY_EVENT);
    if (event == null) {
        return;
    }
    int action = event.getAction();

    switch (event.getKeyCode()) {
    case KeyEvent.KEYCODE_HEADSETHOOK:
             /**
             * for long click
             */
            if (action == KeyEvent.ACTION_DOWN) {

                if (!isDown) {
                    isDown = true;
                    handler.postDelayed(l, LONG_CLICK_DELAY);
                }

            }

            if (action == KeyEvent.ACTION_UP) {

                                     /**
                 * for long click
                 */
                if (isDown)
                    isDown = false;
             }
            break;
    }
    abortBroadcast();
}

Solution

  • I believe that your efforts may be in vain due to this Android bug.

    I assume that will override anything you attempt, or at best, start your app as well as Google Now....

    I've only managed to get around this on rooted devices whilst I wait for Google to fix it sigh