我已经在android:name="android.intent.action.MEDIA_BUTTON"的清单中注册了一个 BroadcastReceiver, 并且我已经使用registerMediaButtonEventReceiver方法向 AudioManager 注册了接收器。现在我的接收函数定义如下。
public void onReceive(final Context context, Intent intent) {
Log.e(LOG_TAG, "Entering Receiver");
KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
int action = event.getAction();
if (action == KeyEvent.ACTION_DOWN) {
Log.e(LOG_TAG, "Down");
} else {
Log.e(LOG_TAG, "Up");
}
}
此代码仅在记录媒体按钮向上和向下事件时打印。如果我单击得足够快,我将无法注册所有向上事件。你对什么是错的有任何想法吗?我的 LogCat 输出如下。请注意,可以通过按住按钮来重现此输出,尽管我没有这样做。
04-24 01:47:38.136: E/MicReceiver(3769): Entering Receiver
04-24 01:47:38.136: E/MicReceiver(3769): Down
04-24 01:47:38.187: E/MicReceiver(3769): Entering Receiver
04-24 01:47:38.187: E/MicReceiver(3769): Down
04-24 01:47:38.257: E/MicReceiver(3769): Entering Receiver
04-24 01:47:38.257: E/MicReceiver(3769): Down
04-24 01:47:38.281: E/MicReceiver(3769): Entering Receiver
04-24 01:47:38.281: E/MicReceiver(3769): Down
04-24 01:47:38.390: E/MicReceiver(3769): Entering Receiver
04-24 01:47:38.390: E/MicReceiver(3769): Down
04-24 01:47:38.402: E/MicReceiver(3769): Entering Receiver
04-24 01:47:38.402: E/MicReceiver(3769): Up