I've tried these phones: Motorolla Backflip 1.5, Nexus One 2.1
Basically I register BroadcastReceiver to get ACTION_HEADSET_PLUG broadcast and look on 3 extras that come in intent:
- state
- name
- microphone
Here is the description from API:
* state - 0 for unplugged, 1 for plugged.
* name - Headset type, human readable string
* microphone - 1 if headset has a microphone, 0 otherwise
Issue #1: Broadcast comes when activity is started (not expected), when screen rotation happens (not expected) and when headset/headphones plugged/unplugged (expected).
Issue #2: Backflip phone (1.5) sends null for state + microphone, 'No Device' as name when headset/headphones unplugged, and sends null for state + microphone, 'Stereo HeadSet'/'Stereo HeadPhones' as name when headset/headphones plugged.
UPDATE: T-Mobile G1 with 1.6 behaves the same as Backflip phone.
Nexus even worse, it always sends null for state + microphone, 'Headset' as name when headset/headphones plugged or unplugged.
Question: How it can be explained that API is broken so much on both 1.5 and 2.1 versions and different devices, manufactures?
UPDATE:
Code in onCreate of main Activity:
// Register receiver
this.registerReceiver(new BroadcastsHandler(), new IntentFilter(Intent.ACTION_HEADSET_PLUG));
Now the code of BroadcastReceiver:
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();
}
}
}