Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Registering a headset button click with BroadcastReceiver in Android

Tags:

java

android

I have a headset with single button and want to do a simple Toast when the button is pressed.

Right now I have the following code:

public class MediaButtonIntentReceiver extends BroadcastReceiver {  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         Toast.makeText(context, "BUTTON PRESSED!", Toast.LENGTH_SHORT).show();      }     abortBroadcast(); } } 

And my main activity is the following:

public class mainActivity extends Activity {  /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.main);      IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);     MediaButtonIntentReceiver r = new MediaButtonIntentReceiver();     registerReceiver(r, filter);   } } 

Nothing happens though when I push the button though.

I'm pretty sure something is wrong with my permissions/xml in the manifest. Here's the receiver XML so far:

    <receiver android:name=".MediaButtonIntentReceiver">         <intent-filter>             <action android:name="android.intent.action.MEDIA_BUTTON" />         </intent-filter>     </receiver> 

....

and:

<uses-permission android:name="android.permission.BLUETOOTH" />  

I notice in LogCat that when I press the button I get an error from "BluetoothIntentReceiver" saying "onReceive() Action : android.intent.action.MEDIA_BUTTON"

like image 748
JDS Avatar asked Jun 09 '11 01:06

JDS


People also ask

Where we use BroadcastReceiver in Android?

Broadcast in android is the system-wide events that can occur when the device starts, when a message is received on the device or when incoming calls are received, or when a device goes to airplane mode, etc. Broadcast Receivers are used to respond to these system-wide events.

How to create BroadcastReceiver in Android?

Creating a BroadcastReceiver The onReceiver() method is first called on the registered Broadcast Receivers when any event occurs. The intent object is passed with all the additional data. A Context object is also available and is used to start an activity or service using context. startActivity(myIntent); or context.

What is a media button?

Media buttons are hardware buttons found on Android devices and other peripheral devices, for example, the pause/play button on a Bluetooth headset.


2 Answers

Just wanted to answer my own question in case others come across similar issues.

The code does work, just I wasn't seeing the Toast because I had another headset button controller app installed (and running in the background), so I guess it took priority over mine. However when I put

    IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);//"android.intent.action.MEDIA_BUTTON"     MediaButtonIntentReceiver r = new MediaButtonIntentReceiver();     filter.setPriority(1000); //this line sets receiver priority     registerReceiver(r, filter); 

It was able to work even with the other app installed. Also, you don't need both the above AND the XML, one or the other is fine as ways of registering the intent receiver.

like image 120
JDS Avatar answered Oct 01 '22 06:10

JDS


Here's what I've got that's working in Android 4.2.2

In my manifest.xml I do this:

<receiver android:name=".MediaButtonIntentReceiver"> <intent-filter>     <action android:name="android.intent.action.MEDIA_BUTTON" /> </intent-filter> 

NB: this is instead of calling registerReceiver.

In my Main Activity's onCreate I need to call the AudioManager:

((AudioManager)getSystemService(AUDIO_SERVICE)).registerMediaButtonEventReceiver(     new ComponentName(          getPackageName(),           MediaButtonIntentReceiever.class.getName())); 

I have found it will work without the AudioManager call, but not for long!

like image 21
noelicus Avatar answered Oct 01 '22 08:10

noelicus