我如何拦截按键presses在Android上的耳机? [英] How do I intercept button presses on the headset in Android?

查看:150
本文介绍了我如何拦截按键presses在Android上的耳机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学的是 ACTION_MEDIA_BUTTON 意图,我试着去使用它,并用举杯拦截按钮presses和present他们在屏幕上。我注册了接收器拦截两个目的:

I studied the ACTION_MEDIA_BUTTON intent and Im trying to use it and intercept the button presses and present them on screen using a toast. I registered the receiver to intercept two intents:

  1. ACTION_HEADSET_PLUG - 插入耳机

ACTION_MEDIA_BUTTON - 接收按钮presses

ACTION_MEDIA_BUTTON - receiving the button presses

这是我的主要活动完成的:

This is done in my main activity:

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

这是处理该按钮presses接收器的一部分:

This is the part of the receiver that handles the button presses:

    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();
    }

现在处理该耳机插件和拆除工作正常,但拦截按钮preSS的部分是没有的部分。

Now the part that handles the headset plug-in and removal works fine, but the part that intercept the button press isn't.

是否有任何理由的code处理该 ACTION_MEDIA_BUTTON 不工作?

Is there any reason the code that handles the ACTION_MEDIA_BUTTON doesn't work?

有没有特殊的权限,我需要拦截这样的意图是什么?

Is there a special permission I need to intercept such an intent?

我使用的是三星Galaxy S2测试code。

I'm using a Samsung Galaxy S2 to test the code.

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

I've looked at all the similar posts and tried everything. Unfortunately nothing seems to work.

推荐答案

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

I recently developed an application which responded to the media button. I tested it in the Samsung Galaxy S II, and it worked.

首先,在你的的Andr​​oidManifest.xml ,里面的<应用> 区域,将以下内容:

First, in your AndroidManifest.xml, inside the <application> area, place the following:

<!-- 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));
            }
        }
    }

}

这是可能的不可以最好的解决办法在那里(特别是硬codeD 安卓优先以上)。然而,它尝试了一些其他的技术,其中没有一个似乎工作。所以,我不得不求助于这个...我希望我帮助。

This is probably not the best solution out there (specially the hard coded android:priority above). However, it tried a couple of other techniques and none of them seemed to work. So I have to resort to this one... I hope I helped.

这篇关于我如何拦截按键presses在Android上的耳机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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