2016-11-12 83 views
0
<receiver android:name=".MusicIntentReceiver" android:exported="false"> 
      <intent-filter> 
       <action android:name="android.media.AUDIO_BECOMING_NOISY" /> 
      </intent-filter> 
      <intent-filter> 
       <action android:name="android.intent.action.MEDIA_BUTTON" /> 
      </intent-filter> 
     </receiver> 

這是細節MusicIntentReceiver ... 公共類MusicIntentReceiver擴展廣播接收器{ 私有靜態最後絃樂TAG = LogHelper.makeLogTag(MusicIntentReceiver.class);android.intent.action.MEDIA_BUTTON不能工作

@Override 
    public void onReceive(Context context, Intent intent) { 
     //LogHelper.i(TAG, "-------------------------------- MusicIntentReceiver."); 
     if (intent.getAction().equals(android.media.AudioManager.ACTION_AUDIO_BECOMING_NOISY)) { 
      LogHelper.d(TAG, "Headphones disconnected."); 
      // send an intent to our MusicService to telling it to pause the audio 
      try { 
       //TODO Something 
      } 
      catch (Exception e) { 
       //LogHelper.i(TAG, "MusicIntentReceiver onReceive ", e.getMessage()); 
      } 
     } 
     else { 
      if (!MainActivity.getInstance().isSDK50orGreater()) { 
       if (intent.getAction().equals(Intent.ACTION_MEDIA_BUTTON)) { 
        KeyEvent keyEvent = (KeyEvent) intent.getExtras().get(Intent.EXTRA_KEY_EVENT); 
        if (keyEvent.getAction() != KeyEvent.ACTION_DOWN) 
         return; 
        //LogHelper.i(TAG, "---------------------------- onReceive ", keyEvent.getKeyCode()); 
        switch (keyEvent.getKeyCode()) { 
         case KeyEvent.KEYCODE_HEADSETHOOK: 
         case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: 
          context.startService(new Intent(MediaContant.ACTION_TOGGLE_PLAYBACK)); 
          break; 
         case KeyEvent.KEYCODE_MEDIA_PLAY: 
          context.startService(new Intent(MediaContant.ACTION_PLAY)); 
          break; 
         case KeyEvent.KEYCODE_MEDIA_PAUSE: 
          context.startService(new Intent(MediaContant.ACTION_PAUSE)); 
          break; 
         case KeyEvent.KEYCODE_MEDIA_STOP: 
          context.startService(new Intent(MediaContant.ACTION_STOP)); 
          break; 
         case KeyEvent.KEYCODE_MEDIA_NEXT: 
          context.startService(new Intent(MediaContant.ACTION_NEXT)); 
          break; 
         case KeyEvent.KEYCODE_MEDIA_PREVIOUS: 
          // previous song 
          context.startService(new Intent(MediaContant.ACTION_PREVIOUS)); 
          break; 
        } 
       } 
      } 
     } 
    } 
} 

我想不能工作

+0

哪部分不工作?你可以插入日誌語句或插入一些敬酒,並告訴我們哪些沒有被激活?你的logcat出錯了嗎?你在測試什麼手機?它是Android 5.0還是更大? –

+0

當屏幕關閉不起作用 – NPBA

+0

[捕獲媒體按鈕上的Android> = 4.0(工程2.3)]可能重複](http://stackoverflow.com/questions/10537184/capture-media-button-on-android-4- 0-works-on-2-3) –

回答

0

如果你想你的應用程序繼續工作,當屏幕關閉時,你就需要獲得一個PARTIAL_WAKE_LOCK

要小心,這是有原因的當屏幕關閉時按鈕不起作用,這是爲了節省電池。你必須非常小心你的應用程序不會耗盡手機電池。

+0

我相信在播放音樂時CPU已經啓動並正在運行。但是,您可能想在〜5-10分鐘的靜音後釋放喚醒鎖。 –

0

我使用 mMediaButtonReceiverComponent = new ComponentName(this,MusicIntentReceiver.class); mAudioManager.registerMediaButtonEventReceiver(mMediaButtonReceiverComponent);

意圖mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); mediaButtonIntent .setComponent(mMediaButtonReceiverComponent); mRemoteControlClientCompat =新RemoteControlClient(PendingIntent.getBroadcast(getApplicationContext()/ 上下文/0/requestCode,忽略 /,mediaButtonIntent/意圖/0/標誌 /)); mAudioManager.registerRemoteControlClient(mRemoteControlClientCompat);

無法控制Android 4.4.2的遙控器