KeyEvent.ACTION_UP解雇两次ACTION_MEDIA_BUTTON [英] KeyEvent.ACTION_UP fired TWICE for ACTION_MEDIA_BUTTON

查看:173
本文介绍了KeyEvent.ACTION_UP解雇两次ACTION_MEDIA_BUTTON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的广播接收机ACTION_MEDIA_BUTTON,实际上同时适用于安卓2.x和Android的4.1,但出于某种奇怪的原因,在的Andr​​oid 2.X(只),我得到每个连< STRONG>两次(对于单一的点击当然暂停按钮,):

I have this broadcast receiver for ACTION_MEDIA_BUTTON which actually works for both Android 2.x and Android 4.1, but for some strange reason, on Android 2.x (only), I get each even twice (for a single click on the pause button, of course):

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:
        }
      }     
    }
  }

}

试图了解的奥秘,我把该事件记录时间为每个出现这样的:

Attempting to understand the mystery, I log the event time for each such occurrence:

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)

此外,这种双重发生的不会发生在的Andr​​oid 4.1。它只发生在安卓2.x的。

Again, this double-occurrence doesn't happen in Android 4.1. It only happens in Android 2.x.

任何想法的为什么?

(虽然我可以使用相同的事件时测井技术来筛选出第二次出现,我preFER首先要了解发生了什么(可能的编程错误,在我的身边?),看看是否有更好的解决方案为)

(while I can use the same event time logging technique to filter-out the second occurrence, I prefer to understand first what's happening (possible programming mistake on my side?) and to see whether there is a better solution for that)

回答以下问题:(你究竟怎么了注册接收器)

Answering the question below: ("how exactly you register your receiver")

首先在应用程序的清单:

First in the app's manifest:

<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中():

Then, in my library's activity (per this tip), in OnCreate():

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

我希望这提供了一个更全面的了解,可以帮助解开这个谜。

I hope this provides a more complete picture that can help solve this mystery.

推荐答案

我注意到,您在图书馆使用该接收机(在你的清单中的MYLIB的一部分)。

I noticed that you are using this receiver in a library (the "mylib" part in your manifest).

如果这是真实的情况,你有一个接收器注册的两个应用程序共享相同的注册code,你会看到这些事件的两次

If this is indeed the case and you have that receiver registered by two applications sharing the same registration code, you will see those events twice.

如果三个应用程序注册一个接收器,您会收到这些事件翻了三倍......

If three applications register that receiver, you will receive those events tripled...

确保每个应用程序使用不同的(唯一的)&LT;接收机器人:名称

Make sure that each application uses a different (unique) <receiver android:name.

这篇关于KeyEvent.ACTION_UP解雇两次ACTION_MEDIA_BUTTON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆