1
public PluginResult execute(String action, JSONArray args, String callbackId) {         
  try {
    Intent intent = new Intent(Intent.ACTION_HEADSET_PLUG);
    String nm = intent.getStringExtra("name");
    String st1 = intent.getStringExtra("state");
    Log.v("nikhil","i="+intent+" name="+nm+" 2="+st1);

    return new PluginResult(PluginResult.Status.OK);

  } catch(Exception e) {            

    Log.e("MailApp", "Could not send email", e); 
  }
  return null; 
}

nm 和 st1 一直为空。即使插入耳机!

4

1 回答 1

1

当耳机插入或拔出手机时,Android 系统会触发 Intent Intent.ACTION_HEADSET_PLUG。

您需要实现一个 BroadcastReceiver 以在此事件发生时捕获它。这是一个粘性事件,因此您可以使用以下快捷方式:

IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
Intent headsetState= this.registerReceiver(null, filter);
int state = headsetState.getIntExtra("state", -1);
String name = headsetState.getStringExtra("name");
于 2012-06-12T20:16:10.773 回答