Android的 - 注册一个耳机按钮点击使用的BroadcastReceiver [英] Android - registering a headset button click with BroadcastReceiver

查看:130
本文介绍了Android的 - 注册一个耳机按钮点击使用的BroadcastReceiver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以我有一个耳机瓦特/单按钮,并希望做一个简单的烤面包当按钮是pressed。

Alright, so I have a headset w/ single button and want to do a simple Toast when the button is pressed.

现在我有以下的code:

Right now I have the following code:

public class MediaButtonIntentReceiver extends BroadcastReceiver {

public MediaButtonIntentReceiver() {
    super();
}

@Override
public void onReceive(Context context, Intent intent) {
    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) {
    // do something
        Toast.makeText(context, "BUTTON PRESSED!", Toast.LENGTH_SHORT).show(); 
    }
    abortBroadcast();
}
}

和我的主要活动如下:

public class mainActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
    MediaButtonIntentReceiver r = new MediaButtonIntentReceiver();
    registerReceiver(r, filter); 

}
}

没有任何反应,虽然当我按下按钮虽然。

Nothing happens though when I push the button though.

我是pretty的肯定的东西是错误与我的权限/ XML的清单。这里的接收器XML至今:

I'm pretty sure something is wrong with my permissions/xml in the manifest. Here's the receiver XML so far:

    <receiver android:name=".MediaButtonIntentReceiver">
        <intent-filter>
            <action android:name="android.intent.action.MEDIA_BUTTON" />
        </intent-filter>
    </receiver>

...

我注意到LogCat中,当我preSS的按钮,我从BluetoothIntentReceiver说的onReceive()动作:android.intent.action.MEDIA_BUTTON得到一个错误

I notice in LogCat that when I press the button I get an error from "BluetoothIntentReceiver" saying "onReceive() Action : android.intent.action.MEDIA_BUTTON"

感谢您的帮助球员,得到真正的卡在这最后几天。

Thanks for any help guys, been really stuck on this the last few days.

推荐答案

只是想回答我的问题的情况下,其他人遇到类似的问题。

Just wanted to answer my own question in case others come across similar issues.

在code没有工作,只是我没有看到敬酒,因为我已经安装了其它耳机按钮控制的应用程序(在后台运行),所以我想它交给我的了优先级。然而,当我把

The code does work, just I wasn't seeing the Toast because I had another headset button controller app installed (and running in the background), so I guess it took priority over mine. However when I put

    IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);//"android.intent.action.MEDIA_BUTTON"
    MediaButtonIntentReceiver r = new MediaButtonIntentReceiver();
    filter.setPriority(1000); //this line sets receiver priority
    registerReceiver(r, filter);

它能够即使安装其他应用程序来工作。另外,就不需要上述两个和XML,一个或另一个是细如登记意向接收器的方法。

It was able to work even with the other app installed. Also, you don't need both the above AND the XML, one or the other is fine as ways of registering the intent receiver.

这篇关于Android的 - 注册一个耳机按钮点击使用的BroadcastReceiver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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