2014-01-08 48 views
1

我希望我的應用在用戶長按(按住)耳機按鈕時執行一些特定操作。我使用BroadcastReceiver代替MEDIA_BUTTON,並且在長時間不運行Google應用的聲音操作的手機中工作良好。但在自動運行Google的手機中,我的應用程序剛剛被忽略。如何現在停用Google並檢測耳機按鈕長按。這裏是我的BroadcastReceiver類中的onReceive方法。谷歌現在不會讓我的應用檢測到耳機按鈕長按

public void onReceive(Context context, Intent intent) { 
String intentAction = intent.getAction(); 
    if (!Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) { 
     return; 
    } 
    KeyEvent event = (KeyEvent) intent 
      .getParcelableExtra(Intent.EXTRA_KEY_EVENT); 
    if (event == null) { 
     return; 
    } 
    int action = event.getAction(); 

    switch (event.getKeyCode()) { 
    case KeyEvent.KEYCODE_HEADSETHOOK: 
      /** 
      * for long click 
      */ 
      if (action == KeyEvent.ACTION_DOWN) { 

       if (!isDown) { 
        isDown = true; 
        handler.postDelayed(l, LONG_CLICK_DELAY); 
       } 

      } 

      if (action == KeyEvent.ACTION_UP) { 

            /** 
       * for long click 
       */ 
       if (isDown) 
        isDown = false; 
      } 
      break; 
    } 
    abortBroadcast(); 
} 

回答

2

我相信你的努力可能徒勞due to this Android bug

我認爲將覆蓋任何你嘗試,或充其量,開始您的應用程序以及谷歌現在....

我只是設法解決這個問題的根源,而設備我等待谷歌修復它嘆息

1

您可以重寫onKeyDown並查找KeyEvent.KEYCODE_HEADSETHOOK鍵,以處理您的應用處於前景時的情況。

至於背景,有序的廣播可以通過接收器中止,這將阻止任何其他應用程序接收廣播,我猜這就是Google Play正在做的事情。解決這個問題的唯一方法是將清單中的android:priority設置爲Google Play之前的適當高價值,並希望Google Play開發者不會選擇Integer.MAX_VALUE

+1

感謝您的回答。我想在後臺處理耳機按鈕,所以首先不能解決我的問題。而且我已經嘗試了第二種解決方案,但它不起作用。 – Mehrdad

3

我想你可以控制長按這一點在清單中的代碼。這將打開一個對話框,讓您選擇長按哪個應用程序。當手機解鎖時,它對我有效,但手機鎖定時無法工作。它會彈出對話框,但是當您解鎖手機時它會消失。希望我能得到一些幫助。

關鍵是打開「活動」,而不是實施廣播接收器。

<activity 
      android:name="packagename.activityname" 
       android:launchMode="singleInstance" 
       android:showOnLockScreen="true"> 
      <intent-filter android:priority="2147483647"> 
       <action android:name="android.speech.action.WEB_SEARCH" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
      <intent-filter android:priority="2147483647"> 
       <action android:name="android.speech.action.VOICE_SEARCH_HANDS_FREE" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
      <intent-filter> 
       <action android:name="com.sec.action.SVOICE" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 

我已經在Kitkat上測試過了。不知道以前的版本會發生什麼。

+0

感謝@engico您的回答,我會盡快嘗試。 – Mehrdad