7

我正在尝试接收物理按钮事件,例如手机侧面的音量按钮点击。我需要在服务中执行此操作,因为如果用户随时单击音量增大/减小按钮,我想接收。我在网上阅读了很多内容,但我无法使其正常工作,也不知道可能出了什么问题。这是我的代码:

在清单中:

    <receiver android:name=".clsMediaButtonReceiver" >
        <intent-filter android:priority="1000" >
            <action android:name="android.intent.action.MEDIA_BUTTON" />               
        </intent-filter>
    </receiver>  

在服务 OnStart() 中:

   AudioManager manager = (AudioManager) getSystemService(AUDIO_SERVICE);
   manager.registerMediaButtonEventReceiver(new ComponentName(getPackageName(), clsMediaButtonReceiver.class.getName())); 

一个广播接收器:

public class clsMediaButtonReceiver extends BroadcastReceiver
{  

   @Override public void onReceive(Context context, Intent intent)
   {  //This is never shown
      Toast toast1 = Toast.makeText(Context, intent.getAction(), Toast.LENGTH_SHORT);
      toast1.show();
   }

} 
4

3 回答 3

9

我添加<action android:name="android.media.VOLUME_CHANGED_ACTION" />到我的意图过滤器中,现在我得到了音量键事件。我还没有弄清楚如何在我的应用程序中正确分发它们......希望它有所帮助!

于 2013-07-03T09:04:21.223 回答
4

我知道这是一个老问题,我遇到了同样的问题,发现我们必须在持有 AudioManager.AUDIOFOCUS_GAIN 之后注册 registerMediaButtonEventReceiver(..) 否则它不起作用。

我尝试使用@Ton 编写的相同 Manifest 和 BroadcastReciever

private class ClassOnAudioFocusChangeListener implements AudioManager.OnAudioFocusChangeListener {
    @Override
    public void onAudioFocusChange(int focusChange) {
         if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT){    
             Log.e("ClassOnAudioFocusChangeListener: ", "AUDIOFOCUS_LOSS_TRANSIENT");
         }
         else if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
             Log.e("ClassOnAudioFocusChangeListener: ", "AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
         }
         else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
             Log.e("ClassOnAudioFocusChangeListener: ", "AUDIOFOCUS_GAIN");
              mAudioManager.registerMediaButtonEventReceiver(mMediaButtonEventComponenName);
         }
         else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
             Log.e("ClassOnAudioFocusChangeListener: ", "AUDIOFOCUS_LOSS");                              
         }
    }
};

这是相同的清单。

    <receiver 
        android:name="MainActivity$MediaButtonEventReceiver">           
        <intent-filter android:priority="1000" >
        <action android:name="android.intent.action.MEDIA_BUTTON" />               
        </intent-filter>
    </receiver>
于 2014-09-04T17:01:39.940 回答
0

我和你有同样的问题,想知道你解决了没有。我的代码的不同之处在于我在另一个类中拥有广播接收器类。像这样的东西:

public class MyClass extends Activity 
{
    /// Code

    public class MediaButtonReceiver extends BroadcastReceiver
    {
         @Override
         public void onReceive(Context context, Intent intent)
         {
              /// Code
         }
    }
}

和清单:

<receiver android:name="net.mypackage.MyClass.MediaButtonReceiver">
    <intent-filter android:priority="1000" >
        <action android:name="android.intent.action.MEDIA_BUTTON" />               
    </intent-filter>
</receiver>

谢谢你的帮助

于 2013-06-21T10:21:56.597 回答