The HeadsetObserver class is responsible for broadcasting Intent.ACTION_HEADSET_PLUG
when a headset in plugged in/out. Here's some of the code from HeadsetObserver.sendIntent()
// Pack up the values and broadcast them to everyone
Intent intent = new Intent(Intent.ACTION_HEADSET_PLUG);
intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
int state = 0;
int microphone = 0;
if ((headset & HEADSETS_WITH_MIC) != 0) {
microphone = 1;
}
if ((headsetState & headset) != 0) {
state = 1;
}
intent.putExtra("state", state);
intent.putExtra("name", headsetName);
intent.putExtra("microphone", microphone);
if (LOG) Slog.v(TAG, "Intent.ACTION_HEADSET_PLUG: state: "+state+" name: "+headsetName+" mic: "+microphone);
// TODO: Should we require a permission?
ActivityManagerNative.broadcastStickyIntent(intent, null);
As you can see, the intent is flagged with Intent.FLAG_RECEIVER_REGISTERED_ONLY. This means that the broadcast cannot be received by a BroadcastReceiver
defined in your AndroidManifest.xml.
You have to have some component of you app running, ie. a Service
or an Activity
, and register the receiver with registerReceiver(BroadcastReceiver receiver, IntentFilter filter)
Edit, example code
As I said, you need to have some component of your app running, to receive the Intent.ACTION_HEADSET_PLUG
broadcast. The best option is probably a Service
.
So lets create a Service
, HeadsetObserverService.java:
public class HeadsetObserverService extends Service {
private static final String TAG = "HeadsetObserverService";
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Create a filter. We are interested in the Intent.ACTION_HEADSET_PLUG action
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_HEADSET_PLUG);
// Register a new HeadsetReceiver
registerReceiver(new HeadsetReceiver(), filter);
// We return START_STICKY. If our service gets destroyed, Android will try to restart it when resources are available.
return START_STICKY;
}
private class HeadsetReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
int state = intent.getIntExtra("state", -1);
switch (state) {
case 0:
Log.d("unplugged", "Headset was unplugged");
// Cancel notification here
break;
case 1:
Log.d("plugged", "Headset is plugged");
// Show notification
break;
default:
Log.w("uh", "I have no idea what the headset state is");
}
}
}
}
}
If you want this Service
to be started automatically when the phone reboots, you'll need a BroadcastReceiver
listening for the Intent.ACTION_BOOT_COMPLETED
broadcast.
public class BootReceiver extends BroadcastReceiver {
private static final String TAG = "BootReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "onReceive() - Received broadcast");
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Log.i(TAG, "onReceive() - Intent action: " + Intent.ACTION_BOOT_COMPLETED);
Log.i(TAG, "onReceive() - Starting service..");
// Phone is booted, lets start the service
Intent serviceIntent = new Intent(context, HeadsetObserverService.class);
context.startService(serviceIntent);
}
}
}
You need to add the new Service
and the BootReceiver
to your AndroidManifest
<receiver
android:name=".BootReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".HeadsetObserverService">
You should also have an Activity
where the user can start and stop the HeadsetObserverService