【问题标题】:ACTION_MEDIA_BUTTON static broadcastreceiver in android 3.0+android 3.0+ 中的 ACTION_MEDIA_BUTTON 静态广播接收器
【发布时间】:2013-06-09 16:37:47
【问题描述】:

我正在尝试使用 Android 3.0+ 中的广播接收器捕获 ACTION_MEDIA_BUTTON 意图。

我的接收器是我的 MainActivity 类的静态内部类。它是静态的,因为它是在我的 AndroidManifest.xml 中注册的,它必须找到类。但是,这意味着当按下播放/暂停按钮时,我的 BroadcastReceiver 无法返回我的活动。 onReceive 方法被调用,但由于该类是静态的,我无法通知我的活动。

使用对我的活动或 Handler 对象的引用也不起作用,因为我无法获取 Android 系统正在调用的 BroadcastReceiver 对象。

动态声明接收者也应该可以工作,但这在 Android 3.0+ 上不起作用,原因很奇怪。它与以下内容有关:

AudioManager.registerMediaButtonEventReceiver(ComponentName)

需要调用。

我的班级的一些插图:

   public class MainActivity extends Activity {

        public static class MicReceiver extends BroadcastReceiver {
            // onReceive is called
                // How do I inform MainActivity of the press?
        }
    }

你有什么解决办法吗?

谢谢!

[编辑] 请参阅下面的代码以动态注册我的接收器:(这目前不起作用)

mReceiver = new RemoteControlReceiver();

IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
filter.setPriority(2147483647);
registerReceiver(mReceiver, filter);

AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
am.registerMediaButtonEventReceiver(new ComponentName(getPackageName(), RemoteControlReceiver.class.getName()));

【问题讨论】:

  • 你为什么不使用registerReceiver(),而是将接收者放在清单中?当您的活动不在前台时,您想对媒体按钮按什么操作?
  • 当我的活动不在前台时,我什么都不想做,否则它不起作用。我在我的 onCreate 中使用此代码来尝试动态注册它。 (见编辑)
  • 啊,我明白了,registerMediaButtonEventReceiver() 是清单中的预期内容。鉴于您是前台活动,您确定需要打扰registerMediaButtonEventReceiver() 吗?您不能使用标准onKeyEvent() 来了解这些印刷机吗?
  • 是的,这可以解决问题。谢谢你。我可以创建最终答案来接受它,但也许它对你更好,因为你会得到 REP ;)

标签: android class android-activity static broadcastreceiver


【解决方案1】:

AFAIK,registerMediaButtonEventReceiver() 是如果您想在后台接收媒体按钮事件。前台 Activity 可以使用 the standard onKeyDown() callback 查找媒体按钮事件。

【讨论】:

    【解决方案2】:

    在 Manifest 中声明接收者将尝试实例化接收者并调用onRecieve(),即使活动不在身边。

    要使其成为活动绑定接收器,请将其设为非静态类并在onCreate() 中实例化它。然后,分别在onResume()onPause()注册和注销。由于类是非静态的,并且仅在活动处于活动状态时才注册,因此您可以安全地从内部接收器类调用父活动方法。

    【讨论】:

      【解决方案3】:

      如果您想在 BroadcastReceiver 中处理某些内容,代码应该如下所示

      public class MainActivity extends Activity {
      
              public static class MicReceiver extends BroadcastReceiver {
      
          public MicReceiver() {
              // TODO Auto-generated constructor stub
              super();
          }
          @Override
          public void onReceive(Context context, Intent intent) {
              // TODO Auto-generated method stub
      
              String intentAction = intent.getAction();
              if (!Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
                  return;
              }
              KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
              if (event == null) {
                  return;
              }
              int action = event.getAction();
              if (action == KeyEvent.ACTION_DOWN) {
      
                Toast.makeText(context, "BUTTON PRESSED! ", Toast.LENGTH_SHORT).show(); 
              }
              abortBroadcast();
          }
      
      
      
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多