拦截蓝牙耳机上的VOICE_COMMAND

时间:2015-09-03 15:13:13

标签: android-intent android-manifest android-bluetooth android-search

我有一个应用程序,我希望在蓝牙免提耳机上拿起按钮并执行我自己的功能。我认为这种类型的耳机可能与蓝牙音乐耳机不同。

无论如何,按下按钮似乎会产生一个;

android.intent.action.VOICE_COMMAND

android.intent.action.MEDIA_BUTTON

就像按下有线电缆耳机上的按钮一样。

系统会选择此功能并启动内置的Google语音搜索功能。我不想要这个。在我的应用程序中并按下时,我想处理并使用此操作。我不能!

如果我将其添加到清单中的应用程序;

<intent-filter android:priority="2147483647">
    <action android:name="android.intent.action.VOICE_COMMAND" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

然后我的应用程序启动,但你得到控制默认VOICE_COMMAND的对话框,我不想要。如果我删除了DEFAULT,那么按下按钮会直接进入Google搜索,并且不会靠近我的应用程序。

如果我将Intent过滤器添加到清单的Receivers部分,我也不会收到任何广播接收器。所以我找不到捕捉VOICE_COMMAND行动的方法。

拦截VOICE_COMMAND的正确/可行方式是什么,以便在活动运行时以自定义方式使用它,而不会默认系统语音搜索?

我很难找到答案,但我找不到任何方法以自定义方式处理应用程序中的VOICE_COMMAND操作。我很高兴按钮可以在其他时间启动谷歌搜索,但是当我的应用程序被使用时,我想在按下按钮时仅按 进行应用程序特定的事情。

想要这样做似乎是合理的,当然可以吗?

1 个答案:

答案 0 :(得分:0)

如果我正确理解您的问题,则只需在“活动”中处理此操作。为此,只需将意图过滤器添加到AndroidManifest.xml。我还指定了singleTop来防止Activity多次启动(就我而言,它可以多次启动self):

<activity
    android:name="YourActivity"
    android:launchMode="singleTop">
    <intent-filter>
        <action android:name="android.intent.action.VOICE_COMMAND" />

        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

然后只需在YourActivity onNewIntent方法中处理此意图:

protected void onNewIntent(Intent intent) {
    if (intent != null && intent.getAction() != null && intent.getAction().equalsIgnoreCase("android.intent.action.VOICE_COMMAND")) {
        // write your logic here
    }
}