I tried these phones: Motorolla Backflip 1.5, Nexus One 2.1
I basically register BroadcastReceiver to get the ACTION_HEADSET_PLUG broadcast and look at the 3 add-ons that come in target:
Here is a description from the API:
* state - 0 for unplugged, 1 for plugged. * name - Headset type, human readable string * microphone - 1 if headset has a microphone, 0 otherwise
Problem # 1: Broadcasting occurs when activity starts (not expected), when the screen rotates (not expected), and when the headset / headphones are connected / disconnected (expected).
Problem No. 2: Backflip phone (1.5) sends null for status + microphone, “No device” as name when headset / headphone is turned off and sends zero for status + microphone, “Stereo HeadSet” / “Stereo HeadPhones” as name when connecting headphones / headphones.
UPDATE: The T-Mobile G1 with 1.6 behaves the same as a Backflip phone.
Nexus is even worse, it always sends zero for status + microphone, “Headset” as a name when a headset / headphone is connected or disconnected.
Question: How can I explain that the API is so badly broken both on version 1.5, and on version 2.1, and on different devices?
UPDATE:
Code in onCreate main activity:
// Register receiver this.registerReceiver(new BroadcastsHandler(), new IntentFilter(Intent.ACTION_HEADSET_PLUG));
Now the BroadcastReceiver code:
public class BroadcastsHandler extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equalsIgnoreCase(Intent.ACTION_HEADSET_PLUG)) { String data = intent.getDataString(); Bundle extraData = intent.getExtras(); String st = intent.getStringExtra("state"); String nm = intent.getStringExtra("name"); String mic = intent.getStringExtra("microphone"); String all = String.format("st=%s, nm=%s, mic=%s", st, nm, mic); AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("Headset broadcast"); builder.setMessage(all); builder.setPositiveButton("Okey-dokey", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); } }
}