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一直处于null状态。即使耳机插上电源也是如此!
发布于 2012-06-13 04:16:11
当头戴式耳机插入或拔出手持设备时,安卓系统会触发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");https://stackoverflow.com/questions/10532391
复制相似问题