1

How can I checked if play/pause button in my headset is clicked? After that can I implement some method that could change typical action (play/pause) for my own action (shut down etc.) ?

PatLas
  • 179
  • 1
  • 5
  • 16

3 Answers3

5

Right now I am using below code headset button click event it's working for me. try this code

// Receiver
public class MediaButtonEventReceiver extends BroadcastReceiver {

    private static final String TAG = "MediaButtonIntentReceiv";
    public MediaButtonIntentReceiver() {
        super();
    }

    @Override
    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();
        if (action == KeyEvent.ACTION_DOWN) {
            // do something
            Log.e(TAG, "onReceive: " );
            Toast.makeText(context, "BUTTON PRESSED!", Toast.LENGTH_SHORT).show();
        }


        abortBroadcast();
    }
}
// Write this code in onCreateview in your activity 

IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
MediaButtonIntentReceiver r = new MediaButtonIntentReceiver();
filter.setPriority(1000); //this line sets receiver priority
registerReceiver(r, filter);
//  manifest.xml 
 <receiver android:name=".MediaButtonIntentReceiver">
            <intent-filter >
                <action android:name="android.intent.action.MEDIA_BUTTON"/>
            </intent-filter>
 </receiver>
nmu
  • 1,442
  • 2
  • 20
  • 40
Jaydeep Chauhan
  • 146
  • 2
  • 6
3

If you are trying to listen for this from an activity in the foreground, use onKeyDown() and watch for KEYCODE_MEDIA_PLAY_PAUSE.

Use a BroadcastReceiver for ACTION_MEDIA_BUTTON if you are trying to listen for this event from the background (e.g., a service playing music).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • It's my broadcast receiver but it doesn't work, what's wrong? public final BroadcastReceiver hReceiver = new BroadcastReceiver(){ public void onReceive(Context context, Intent intent) { String action = intent.getAction(); Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); if(mediaButtonIntent.equals(action)) { adapter.add("click"); } } }; – PatLas Apr 12 '12 at 16:48
  • @user1320033: You do not need to test your action string, if you only use this `BroadcastReceiver` once with the right `IntentFilter`. Besides, you are testing it incorrectly. – CommonsWare Apr 12 '12 at 19:34
  • @user1320033: `Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())`. – CommonsWare Apr 13 '12 at 14:29
  • It steal doesn't work :( I write in onReceive: `if(Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {...}` and in onCreate() `registerReceiver(hReceiver, new IntentFilter());` but after clicking nothing has happened. – PatLas Apr 13 '12 at 15:12
0

You can use ACTION_MEDIA_BUTTON intent Read more above link Hope this help

  • It's my broadcast receiver but it doesn't work, what's wrong? public final BroadcastReceiver hReceiver = new BroadcastReceiver(){ public void onReceive(Context context, Intent intent) { String action = intent.getAction(); Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); if(mediaButtonIntent.equals(action)) { adapter.add("click"); } } }; – PatLas Apr 12 '12 at 16:47