android 耳机检测,检测耳机是否已插入Android设备。

您可以使用广播接收器。

因此,您可以在“ AndroidManifest.xml”中编写此代码

-->

但是,这不起作用。当OS发送此“ HEADSET_PLUG”意图时,OS设置标志“ Intent.FLAG_RECEIVER_located_ONLY”。因此,您应在Activity或Service类中编写如下代码,而不是“ AndroidManifest”。

public class BRHeadsetActivity extends Activity {

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

IntentFilter receiverFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);

HeadsetStateReceiver receiver = new HeadsetStateReceiver();

registerReceiver( receiver, receiverFilter );

}

希望本文对您有所帮助。再见!

这是Android SDK源代码“ HeadsetObserver.java”的一部分。

private final void sendIntent(int headset, int headsetState, int prevHeadsetState, String headsetName) {

if ((headsetState & headset) != (prevHeadsetState & headset)) {

//  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);

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值