我在AndroidManifest.xml中声明了一个BroadcsastReceiver(Action_headset_plug)并且定义了一个BroadcastHandler.class实现BroadcsastReceiver。我在设备上运行apk并且接收器不启动。但是,当我在Activity中使用registerReceiver()时,它可以正常工作。我在AndroidManifest.xml中错过了什么?[Android] BroadcastReceiver(Action_headset_plug)没有射击
这是AndroidManifest.xml中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="irdc.Earphone_test"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="9" />
<uses-permission android:name="android.permission.ACTION_HEADSET_PLUG"></uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:enabled="true" android:name="BroadcastHandler">
<intent-filter>
<action android:name="android.intent.ACTION_HEADSET_PLUG"></action>
</intent-filter>
</receiver>
<activity android:name=".Earphone_testActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是接收器代码
public class BroadcastHandler extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)){
String mes;
int state = intent.getIntExtra("state", 4);
if(state == 0){
mes ="out";
}else if(state == 1){
mes ="in";
}else {
mes ="others";
}
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Headset broadcast");
builder.setMessage(mes);
builder.setPositiveButton("Okey-dokey", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.create().show();
}
}
}
ACTION_HEADSET_PLUG does not显示为android.permission的一个选项 – shim