2

我在Android 2.1中工作,我想要檢測何時插入/取出耳機。我很新的android。在Android中如何註冊接收耳機插頭播放?

我認爲這樣做的方法是使用廣播接收器。我sublcassed這一點,我也把以下放在我的AndroidManifest.xml。但是,您是否還需要在接收器中註冊其他人,如在活動中?我知道這裏有很多線索,但我不太明白他們在說什麼。另外,在AndroidManifest.xml中註冊與在您的活動中動態註冊有什麼區別?

<receiver android:enabled="true" android:name="AudioJackReceiver" > 
     <intent-filter> 
      <action android:name="android.intent.action.HEADSET_PLUG" > 
      </action> 
     </intent-filter> 
    </receiver> 

這是類的實現(加進口)

public class AudioJackReceiver extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 
    Log.w("DEBUG", "headset state received"); 
} 

}

我只是想看看它是否工作,但沒有顯示出來,當我拔下/插頭在運行應用程序時在耳機中。

編輯:文檔不會說這個,但是如果註冊在清單中,這個可能不起作用嗎?我能得到它,當我註冊的接收器在我的應用程序一個反應(或你必須這樣做呢?)

回答

2

這裏有兩個網站,可以幫助更詳細地解釋它:

你必須定義你的意圖;否則將不會訪問系統功能。廣播接收機;會提醒您的應用程序您想要傾聽的更改。

每個接收器都需要被分類;它必須包含一個onReceive()。要實施onReceive(),您需要創建一個包含兩個項目的方法:上下文 & 意圖

更有可能服務將是理想的;但你會創建一個服務並通過它定義你的上下文。在上下文中;你會定義你的意圖。

一個例子:

context.startService 
     (new Intent(context, YourService.class)); 

非常基本的例子。然而;您的特定目標是利用全系統廣播。你希望你的申請被通知Intent.ACTION_HEADSET_PLUG

如何通過清單訂閱:

<receiver 
    android:name="AudioJackReceiver" 
    android:enabled="true" 
    android:exported="true" > 
    <intent-filter> 
     <action android:name="android.intent.action.HEADSET_PLUG" /> 
    </intent-filter> 
</receiver> 

或者您也可以通過應用程序簡單地定義;但。您的特殊要求;如果您打算檢測藍牙MODIFY_AUDIO_SETTINGS將需要用戶權限。

+3

您不能使用清單聲明來接收此意圖。您的接收器需要通過代碼進行定義。請參閱http://stackoverflow.com/questions/6249023/detecting-whether-a-headset-is-plugged-in-or-not/6366238#6366238 – William

+0

另外,我無法保證初學者會覺得有多混亂需要明確的註冊*和*代碼註冊。這個答案正確使用「**或**,你可以簡單地通過你的應用程序定義」。它是清單或代碼,而不是兩者。在HEADSET_PLUG的情況下,威廉是正確的:你不能使用清單聲明來接收這個意圖。所以只有代碼選項仍然存在。 – WebViewer

0

您需要enable the broadcast receiver,並設置exported屬性true

<receiver 
    android:name="AudioJackReceiver" 
    android:enabled="true" 
    android:exported="true" > 
    <intent-filter> 
     <action android:name="android.intent.action.HEADSET_PLUG" /> 
    </intent-filter> 
</receiver> 
+0

謝謝,但我後來補充說,(編輯響應剛纔的更新),但似乎仍然沒有工作。 export =「false」是做什麼的? – shim

+0

對不起,'嘗試導出=「true」'。我相信默認值是'false'(如果未定義)。如果它沒有設置爲「true」,系統將不會調用它,只能由您的應用程序調用。 – twaddington

9

只是補充Greg`s答案,這裏是你需要在兩個部分

  1. 註冊的第一個活動服務劃分(這裏其所謂MainActivity.java)的代碼。

  2. 切換BroadCastReceiverACTION_HEADSET_PLUG動作的結果。

這裏有雲:

public class MainActivity extends Activity { 
private static final String TAG = "MainActivity"; 
private MusicIntentReceiver myReceiver; 

@Override protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    myReceiver = new MusicIntentReceiver(); 
} 

@Override public void onResume() { 
    IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG); 
    registerReceiver(myReceiver, filter); 
    super.onResume(); 
} 

private class MusicIntentReceiver extends BroadcastReceiver { 
    @Override public void onReceive(Context context, Intent intent) { 
     if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) { 
      int state = intent.getIntExtra("state", -1); 
      switch (state) { 
      case 0: 
       Log.d(TAG, "Headset is unplugged"); 
       break; 
      case 1: 
       Log.d(TAG, "Headset is plugged"); 
       break; 
      default: 
       Log.d(TAG, "I have no idea what the headset state is"); 
      } 
     } 
    } 
} 
+2

我試着將接收器和過濾器放在清單中,同時使用android:enabled =「true」和android:exported =「true」,但我的hanlder沒有被調用。按照Renato的建議,通過代碼註冊,它就像魅力一樣! – zontar

相關問題