嗨,我正在开发一个应用程序,该应用程序会在从手机上取下耳机时生成一个事件。我用接收方法创建了一个广播接收器
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>
和许可
但这不起作用任何人都可以指导我完成这个吗?