4

I have an app where I wish to pick up the button on a bluetooth hands free headset and do my own functionality. I think this type of headset may behave different that a Bluetooth music headset.

Anyway, pressing the button seems to generate a;

android.intent.action.VOICE_COMMAND

Not;

android.intent.action.MEDIA_BUTTON

Like when pressing the button on a tethered cable headset.

This is picked up by the system and starts the inbuilt google voice search. I don't want this. When in my application and pressed, I want to process and consume this action. I can't!

If I add this to the Application in the manifest;

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

Then my application starts but you get the dialog to take control of the default VOICE_COMMAND, which I dont't want. If I remove the DEFAULT, then the button press goes straight to the Google search and does not go near my application.

If I add the Intent filter to the Receivers section of manifest, I get no broadcast receiver called either. So I can find no way to capture the VOICE_COMMAND action.

What is the correct/working way to intercept the VOICE_COMMAND so it can be used in a custom way while an activity is running, without defaulting to the system voice search?

I have looked very hard to find an answer for this, but I can't find any way to handle the VOICE_COMMAND action within an application in a custom manner. I am happy for the button to start google search other times, but when my application is being used, I want to do application specific things only on the button press.

It seems a reasonable thing to want to do, surely is possible?

user1667016
  • 123
  • 2
  • 8

1 Answers1

0

If I understand your question right you just need to handle this action in your Activity. For this just add the intent-filter to AndroidManifest.xml. I also specified singleTop to prevent Activity launching multiple times (in my case it can launch self several times):

<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>

And then just handle this intent inside YourActivity onNewIntent method:

protected void onNewIntent(Intent intent) {
    if (intent != null && intent.getAction() != null && intent.getAction().equalsIgnoreCase("android.intent.action.VOICE_COMMAND")) {
        // write your logic here
    }
}   
Alex Misiulia
  • 1,542
  • 17
  • 16