2011-09-20 108 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