2012-01-29 107 views
13

我研究了ACTION_MEDIA_BUTTON的意图,并试图使用它,并拦截按钮并使用吐司在屏幕上显示它们。我注册了接收器拦截两个目的:如何拦截Android中耳机上的按钮?

  1. ACTION_HEADSET_PLUG - 插上耳机

  2. ACTION_MEDIA_BUTTON - 如果接收到按钮按下

这是我的主要活动进行:

 IntentFilter mediaFilter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON); 
     mediaFilter.setPriority(10000); 
     registerReceiver(_receiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG)); 
     registerReceiver(_receiver, mediaFilter); 

这是r的部分eceiver处理该按钮按下:

if (action.equals(Intent.ACTION_HEADSET_PLUG)) 
    { 
     Toast.makeText(context, "earphones activity",Toast.LENGTH_SHORT).show(); 
     if (intent.getExtras().getInt("state")==1)//if plugged 
      Toast.makeText(context, "earphones plugged",Toast.LENGTH_LONG).show(); 
     else Toast.makeText(context, "earphones un-plugged",Toast.LENGTH_LONG).show(); 
    } 
    else 
    if (action.equals(Intent.ACTION_MEDIA_BUTTON)) 
    { 
     Toast.makeText(context, "button pressed",Toast.LENGTH_LONG).show(); 
     key=intent.getExtras().getString("EXTRA_KEY_EVENT"); 
     Toast.makeText(context, key,Toast.LENGTH_LONG).show(); 
    } 

现在处理耳机插件和拆除工作正常,但拦截按按键部分不是一部分。

是否有任何理由处理ACTION_MEDIA_BUTTON不起作用的代码?

是否有我需要拦截这种意图的特殊许可?

我正在使用三星Galaxy S2来测试代码。

我看过所有类似的帖子,并试过一切。不幸的是似乎没有任何工作

+0

你在测试什么设备? – slayton 2012-01-29 21:12:21

+0

samsung galaxy s 2 – 2012-01-29 21:26:23

+0

我不确定,但它可能是TouchWiz没有正确实现ANDROID_MEDIA_BUTTON意图... – slayton 2012-01-29 21:30:50

回答

3

我最近开发了回应媒体按钮的应用程序。我在三星Galaxy S II中测试过它,它工作正常。

首先,在你AndroidManifest.xml,该<application>区域内,放置以下:

<!-- Broadcast Receivers --> 
<receiver android:name="net.work.box.controller.receivers.RemoteControlReceiver" > 
    <intent-filter android:priority="1000000000000000" > 
     <action android:name="android.intent.action.MEDIA_BUTTON" /> 
    </intent-filter> 
</receiver> 

然后,在不同的文件中创建一个BroadcastReceiver

public class RemoteControlReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction()) { 
      KeyEvent event = (KeyEvent) intent .getParcelableExtra(Intent.EXTRA_KEY_EVENT); 

      if (event == null) { 
       return; 
      } 

      if (event.getAction() == KeyEvent.ACTION_DOWN) { 
       context.sendBroadcast(new Intent(Intents.ACTION_PLAYER_PAUSE)); 
      } 
     } 
    } 

} 

这可能是的最好的解决方案在那里(特别是上面的硬编码android:priority)。然而,它尝试了其他一些技术,但他们都没有工作。所以我不得不诉诸于此......我希望我能帮上忙。

+2

请检查以下链接。简单而容易。 http://android-developers.blogspot.com/2010/06/allowing-applications-to-play-nicer.html – Milton 2013-08-03 13:41:25

+1

这对我有用! – Prachi 2014-05-13 07:42:25

+0

PS:1000必须是较高的优先值,如果您使用android:priority =“1000000000000000”,则应该得到一个警告或错误指出。正确的事情是:经过几天的斗争, leoneboaventura 2016-03-12 03:29:14

0
if (Intent.ACTION_MEDIA_BUTTON.equals(action)) { 
    if (intent.hasExtra(Intent.EXTRA_KEY_EVENT)) { 
     String key = intent.getStringExtra(Intent.EXTRA_KEY_EVENT); 
     toast("Button "+key+" was pressed"); 
    } else 
     toast("Some button was pressed"); 
} 

此代码返回吐司“按钮无效按下”

+0

如果您遇到问题,请发表评论而不是提供答案。 – Mercurial 2014-02-26 10:24:44

2

感谢贡献。

对于所有其他挣扎这里是最终结论:

经过大量的血泪我终于认识到,有两种类型的广播,我可以拦截:有的像ACTION_HEADSET_PLUG需要在登记活动代码。需要

有些人则喜欢ACTION_MEDIA_BUTTON在清单文件进行注册。

在这个例子中,为了拦截这两种意图,我们需要两者都做。

将其设置在代码和清单文件中。

+0

:非常感谢你! – elgui 2012-12-03 15:58:08