在Android中使用BroadcastReceiver注册耳机按钮单击 [英] Registering a headset button click with BroadcastReceiver in Android

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

问题描述

我有一个具有单个按钮的耳机,并且想在按下按钮时做一个简单的Toast.

I have a headset with single button and want to do a simple Toast when the button is pressed.

现在我有以下代码:

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.

我很确定清单中的我的权限/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>

....

和:

<uses-permission android:name="android.permission.BLUETOOTH" /> 

我在LogCat中注意到,当我按下按钮时,我从"BluetoothIntentReceiver"收到一条错误消息,说"onReceive()Action: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"

推荐答案

只是想回答我自己的问题,以防其他人遇到类似问题.

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

代码确实有效,只是我没看到Toast,因为我安装了另一个耳机按钮控制器应用程序(并在后台运行),所以我想它比我的优先级高.但是当我放

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,也不需要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天全站免登陆