KeyEvent.ACTION_UP为ACTION_MEDIA_BUTTON触发了TWICE

我有这个ACTION_MEDIA_BUTTON广播接收器,它实际上适用于Android 2.x和Android 4.1,但由于一些奇怪的原因,在Android 2.x(仅) ,我甚至得到两次单击暂停button,当然):

public class RemoteControlReceiver extends BroadcastReceiver { private static long prevEventTime = 0; @Override public void onReceive(Context ctx, Intent intent) { if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) { KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); long curEventTime = event.getEventTime(); if (event != null && (event.getAction() == KeyEvent.ACTION_UP) /*&& (curEventTime != prevEventTime)*/) { int keycode = event.getKeyCode(); switch (keycode) { case KeyEvent.KEYCODE_MEDIA_NEXT: Log.i(TAG, "KEYCODE_MEDIA_NEXT"); break; case KeyEvent.KEYCODE_HEADSETHOOK: Log.i(TAG, "KEYCODE_HEADSETHOOK" + " " + curEventTime + " <> " + prevEventTime + " (" + event.getAction() + ")"); prevEventTime = curEventTime; break; case KeyEvent.KEYCODE_MEDIA_PREVIOUS: Log.i(TAG, "KEYCODE_MEDIA_PREVIOUS"); break; default: } } } } } 

试图了解这个秘密,我logging每个事件的发生时间:

 03-01 18:27:05.264: I/RemoteControlReceiver.onReceive(22377): KEYCODE_HEADSETHOOK 142304436 <> 0 (1) 03-01 18:27:05.434: I/RemoteControlReceiver.onReceive(22377): KEYCODE_HEADSETHOOK 142304436 <> 142304436 (1) 03-01 18:27:14.054: I/RemoteControlReceiver.onReceive(22377): KEYCODE_HEADSETHOOK 142313265 <> 142304436 (1) 03-01 18:27:14.074: I/RemoteControlReceiver.onReceive(22377): KEYCODE_HEADSETHOOK 142313265 <> 142313265 (1) 03-01 18:27:24.254: I/RemoteControlReceiver.onReceive(22377): KEYCODE_HEADSETHOOK 142323464 <> 142313265 (1) 03-01 18:27:24.264: I/RemoteControlReceiver.onReceive(22377): KEYCODE_HEADSETHOOK 142323464 <> 142323464 (1) 03-01 18:27:37.574: I/RemoteControlReceiver.onReceive(22377): KEYCODE_HEADSETHOOK 142336795 <> 142323464 (1) 03-01 18:27:37.614: I/RemoteControlReceiver.onReceive(22377): KEYCODE_HEADSETHOOK 142336795 <> 142336795 (1) 03-01 18:27:45.214: I/RemoteControlReceiver.onReceive(22377): KEYCODE_HEADSETHOOK 142344433 <> 142336795 (1) 03-01 18:27:45.284: I/RemoteControlReceiver.onReceive(22377): KEYCODE_HEADSETHOOK 142344433 <> 142344433 (1) 03-01 18:27:52.474: I/RemoteControlReceiver.onReceive(22377): KEYCODE_HEADSETHOOK 142351687 <> 142344433 (1) 03-01 18:27:52.504: I/RemoteControlReceiver.onReceive(22377): KEYCODE_HEADSETHOOK 142351687 <> 142351687 (1) 

同样, Android 4.1中也不会发生这种双重事件 它只发生在Android 2.x.

任何想法为什么?

(虽然我可以使用相同的事件时间logging技术来滤除第二个事件,但我更愿意首先了解发生了什么事情(可能是我的编程错误?),并查看是否有更好的解决scheme)


回答下面的问题:( “你怎么注册你的接收器”)

首先在应用程序的清单:

 <receiver android:name="com.example.mylib.RemoteControlReceiver" android:enabled="true"> <intent-filter android:priority="2147483647" > <action android:name="android.intent.action.MEDIA_BUTTON" /> </intent-filter> </receiver> 

然后,在我的图书馆的活动( 按此技巧 ),在OnCreate():

 mRemoteControlReceiver = new ComponentName(this, RemoteControlReceiver.class); mAudioManager.registerMediaButtonEventReceiver(mRemoteControlReceiver); 

我希望这提供了一个更完整的图片,可以帮助解决这个谜。

Solutions Collecting From Web of "KeyEvent.ACTION_UP为ACTION_MEDIA_BUTTON触发了TWICE"

我注意到你正在库中使用这个接收器(清单中的“mylib”部分)。

如果情况确实如此,并且您已经通过共享相同注册码的两个应用程序注册了该接收者,则您将看到这些事件两次

如果三个应用程序注册该接收器,您将收到这些事件的三倍…

确保每个应用程序使用不同的(唯一的) <receiver android:name

我跑过这个问题,花了一段时间,我的头撞墙之前,我意识到,每按一下button生成两个事件:KeyEvent.ACTION_DOWN和KeyEvent.ACTION_UP。 因此需要两个testing来使用媒体button:

 if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {} 

 if (event.getAction() == KeyEvent.ACTION_DOWN) {} 

所以如果有人因为同样的错误而在这里发现自己,也许这个答复会有帮助。

为了确保这是第一次总是可以使用一个标志来说明这是第一个动作。

 boolean firstAction= false; if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) { firstAction= !firstAction; if(!firstAction) { return true; } KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); long curEventTime = event.getEventTime(); if (event != null && (event.getAction() == KeyEvent.ACTION_UP) /*&& (curEventTime != prevEventTime)*/) { int keycode = event.getKeyCode(); switch (keycode) { case KeyEvent.KEYCODE_MEDIA_NEXT: Log.i(TAG, "KEYCODE_MEDIA_NEXT"); break; case KeyEvent.KEYCODE_HEADSETHOOK: Log.i(TAG, "KEYCODE_HEADSETHOOK" + " " + curEventTime + " <> " + prevEventTime + " (" + event.getAction() + ")"); prevEventTime = curEventTime; break; case KeyEvent.KEYCODE_MEDIA_PREVIOUS: Log.i(TAG, "KEYCODE_MEDIA_PREVIOUS"); break; default: } } }