0

I want to use the HEADSETHOOK button (keycode 79) on my Samsung earphones but cannot get it to work. I am, essentially, following the answer given in this SO question. My phone is a Samsung Galaxy S7 running Android v8.

The relevant part of my manifest is:

    <receiver android:name=".MainActivity$HeadsetBtnReceiver" android:enabled="true" >
        <intent-filter android:priority="10000000" >
            <action android:name="android.intent.action.MEDIA_BUTTON" />
        </intent-filter>
    </receiver>
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

My Activity (which includes the BroadcastReceiver) is:

public class MainActivity extends AppCompatActivity {

static AudioManager mAudioManager;
static ComponentName mReceiverComponent;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    mReceiverComponent = new ComponentName(this, HeadsetBtnReceiver.class);
    mAudioManager.registerMediaButtonEventReceiver(mReceiverComponent);

}
@Override
public void onResume() {
    super.onResume();
}

@Override
public void onPause() {
    super.onPause();
}

@Override
public void onBackPressed() {
    Log.e("Udp", "Backpress MainActivity");
    finish();
}

@Override
public void onDestroy() {
    Log.e("UDP svc", "Destroying MainActivity");
    mAudioManager.unregisterMediaButtonEventReceiver(mReceiverComponent);
    finish();
    super.onDestroy();
}

public static class HeadsetBtnReceiver extends BroadcastReceiver {

    // Constructor is mandatory

    public HeadsetBtnReceiver()
    {
        super ();
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        String intentAction = intent.getAction();
        Log.e("Udp svc", intentAction.toString() + " happened");
        if (!Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
            Log.e("Udp svc", "no media button information");
            return;
        }
        KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
        if (event == null) {
            Log.e("Udp svc", "no keypress");
            return;
        }

        KeyEvent key = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
        Log.e("UDP svc", "KeyEvent : ");

        if(key.getAction() == KeyEvent.ACTION_UP) {
            int keycode = key.getKeyCode();
            if(keycode == KeyEvent.KEYCODE_MEDIA_NEXT) {
                Log.e("Udp svc", "Next Pressed");
            } else if(keycode == KeyEvent.KEYCODE_MEDIA_PREVIOUS) {
                Log.e("Udp svc", "Previous pressed");
            } else if(keycode == KeyEvent.KEYCODE_HEADSETHOOK) {
                Log.e("Udp svc", "Head Set Hook pressed");
            }
        }
    }
}

}

When I run the app I get no Logcat messages, all that happens is that I get the default actions for both the volume buttons and the other, single, button.

I believe that the BroadcastReceiver has been deprecated in favour of MediaSession but as far as I can make out that only recognises the volume buttons.

What am I doing wrong?

Doug Conran
  • 437
  • 1
  • 5
  • 17
  • Try registering for it dynamically as a test, using `registerReceiver()`. If that works, then your problem is that [most implicit broadcasts are banned on Android 8.0+](https://commonsware.com/blog/2017/04/11/android-o-implicit-broadcast-ban.html). If `registerReceiver()` also fails, then either you have the wrong `Intent` action or the device simply is not broadcasting it when you think it should be. – CommonsWare Aug 11 '18 at 23:26
  • Thanks for the reply. However I still can't get it working and am clearly doing something wrong. See my comment below @Pawel's answer for more detail – Doug Conran Aug 12 '18 at 15:03

1 Answers1

0

Move your receiver from manifest into a regular receiver object You will register at runtime. Broadcasts receivers declared in manifest no longer work from android O (few exceptions apply).

As part of the Android 8.0 (API level 26) Background Execution Limits, apps that target the API level 26 or higher can no longer register broadcast receivers for implicit broadcasts in their manifest.

More details: https://developer.android.com/about/versions/oreo/background#broadcasts

Pawel
  • 15,548
  • 3
  • 36
  • 36
  • 1
    Thank you for your reply. I've now done this (as far as I'm aware) but am still not getting any responses when I press the headset button, I have removed the entry from the manifest and created a broadcastreceiver instance called headsetBtnReceiver (based on the public static class HeadsetBtnReceiver declared within the MainActivity) and registered this with the intentfilter ACTION_MEDIA_BUTTON all of which is immediately before the AudioManager statements. But still it isn't working. What am I doing wrong or missing? – Doug Conran Aug 12 '18 at 15:28