2011-09-20 51 views
5

我在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(); 

     } 

    } 
} 
+0

ACTION_HEADSET_PLUG does not显示为android.permission的一个选项 – shim

回答

1

名称是错误的清单项。使用完整的包名称,或者如果你想它隐含附加到应用程序的软件包的名称以一段开始吧:

<receiver android:enabled="true" android:name=".BroadcastHandler"> 
0

大多数接收机的例子不启动AlertDialog但吐司消息或创建通知在状态栏。我相信你不能开始一个活动,因为“内容”对象停止存在,但如果你可以建立一个AlertDialog,则不会。 (documentation of Broadcasts

所以你可以尝试在你的接收器Toast消息,以确保它的工作。

实施例与Notification:http://www.anddev.org/recognize-react_on_incoming_sms-t295.html

+0

[“关于您的错误,清单注册的BroadcastReceiver不是一个Activity,因此它不能创建对话框。”](http://stackoverflow.com/questions/3432601/alertdialog-in-broadcastreceiver/3432645#3432645) – madlymad

5

用于收听耳机变化,广播接收机不能在清单中声明的​​,它必须被动态注册。并非所有接收器都在清单中声明时工作,并且这是一个需要以编程方式注册的示例。

您可以在活动的的onResume方法或服务的的onCreate方法调用此例如:然后在活动的的onPause方法或服务的的onDestroy方法

headsetReceiver = new BroadcastHandler(); 
registerReceiver(headsetReceiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG)); 

你需要取消注册接收器:

unregisterReceiver(headsetReceiver); 
+1

为什么这个与其他的不同?你有链接到文件证实这一点? – shim

+0

http://developer.android.com/reference/android/content/Intent.html为无法通过应用清单注册的人记下一笔,而这不是其中之一。 – shim

+0

但是,我能够使用这种方法。谢谢! – shim