【发布时间】:2012-07-30 06:20:09
【问题描述】:
您好,我正在开发一个应用程序,该应用程序会在耳机从手机上取下时生成一个事件。 我创建了一个接收方法为的广播接收器
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
Log.i("Broadcast Receiver", "Hello");
if( (action.compareTo(Intent.ACTION_HEADSET_PLUG)) == 0) //if the action match a headset one
{
int headSetState = intent.getIntExtra("state", 0); //get the headset state property
int hasMicrophone = intent.getIntExtra("microphone", 0);//get the headset microphone property
if( (headSetState == 0) && (hasMicrophone == 0)) //headset was unplugged & has no microphone
{
//do whatever
}
}
}
如下调用该方法
IntentFilter receiverFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
HeadSetBroadCastReceiver receiver = new HeadSetBroadCastReceiver();
registerReceiver( receiver, receiverFilter );
我也已在清单中将此注册为
<receiver android:name=".HeadsetBroadCastReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_HEADSET_PLUG"/>
</intent-filter>
</receiver>
和许可
但这不起作用,谁能指导我完成这个?
【问题讨论】:
-
普通人没人能指导我吗?
-
我已经检查过了,但 logcatif (intent.hasExtra("state")){ if (headsetConnected && intent.getIntExtra("state", 0) == 0){耳机连接 = 假; Log.d("值","内部"); } } else if (!headsetConnected && intent.getIntExtra("state", 0) == 1){ 耳机连接 = true; } }
-
你的
onReceive()被叫了吗?
标签: android