android - registerMediaButtonEventReceiver 没有收到事件

标签 android volume

我正在尝试接收物理按钮事件,例如手机侧面的音量按钮点击。我需要在服务中这样做,因为我想在用户随时单击音量增大/减小按钮时接收。 我在网上看了很多,但我不能让它工作,也不知道哪里出了问题。这是我的代码:

在 list 中:

    <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();
   }

} 

最佳答案

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

关于android - registerMediaButtonEventReceiver 没有收到事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16926182/

相关文章:

android - 如何通过单个 Intent (从小部件)调用多个 Activity ?

android - 如何将 ArrayList<HashMap<String, String>> 数据存储到 sharedpreferences 中?

android - MediaPlayer 无法调高音量

kubernetes - 禁止 : SizeLimit field disabled by feature-gate for EmptyDir volumes

python - 3D 体积的时间序列插值(MATLAB 或 Python)

iPhone SDK - 如果按下硬件按钮,如何禁用音量指示器 View ?

linux - 模拟挂载卷错误导致只读

java - 在模拟器上按下某个键之前,ListView 不会更新

android - Java Reflections 不适用于 Android 即时运行

android - InputMultiplexer 仅在舞台上工作