Android App Integrated with OK Google
Asked Answered
A

3

23

Is there a way to issue a voice command something like:

OK GOOGLE ASK XXX Some App Specific Question or Command

And have it launch "APP" with the recognized text: "Some App Specific Question or Command"

My app has speech recognition as a service ... but when using my APP I can't ask questions that OK Google can handle ...

Acquit answered 7/6, 2015 at 2:52 Comment(1)
is speech recognition as a service decreasing battery life? can you explain that scenario?Snicker
O
18

Through the Voice Actions API, your app can register for system actions, one of which is 'search' (so you could do 'search for Some Question or command on APP').

In the past, some developers were able to submit a custom voice action request. Upon approval, users could do a specific action with your app via voice. This is no longer an option.

Orthochromatic answered 7/6, 2015 at 3:39 Comment(9)
Excellent ... I have not seen this API before ... this looks to be what I need.Acquit
I should have commented a while ago ... I did submit a custom voice request ... never did get any feedback.Acquit
I've been looking for this for a very long time-do you know how I can have continues voice recognition while the screen is off, just like Google now? Thanks so much!Outcome
@RuchirBaronia - I'd really, really encourage you to ask your own question rather than comment on existing answers to different questions.Orthochromatic
Thanks for the response! I don't think I have enough code yet to actually ask my own question, and I'm sure it would get closed immediately. If I had a continues service that would run forever (and start on boot) and I wanted to constantly listen for certain voice inputs (and execute some code when I get that input), where do you think I should start? Similar to how "OK - Google" works even when the screen is off. Any information is extremely helpful, and I would really appreciate your help!Outcome
@RuchirBaronia - as of Marshmallow, it is all centered around VoiceInteractionService - that's your lightweight, always on service.Orthochromatic
@Orthochromatic This will only work for marshmallow? I was trying to target most devices...I noticed it's a service, which is great! So I'm assuming that this will work when the screen is off. So if I tried to set it to do something when I said "Hi", it would work even when the screen is off, right? I have gone through the documentation, please let me know if it will work for other devices. Again, thanks so much, this will help me a lot if it works when the screen is off! The only problem might be to keep recognizing even after already saying "Hi" once...Outcome
Also, if this is difficult to do offline, do you suggest any libraries I can download?Outcome
Currently google is not accepting any new custom voice action requests.Hereinto
B
10

This is actually pretty simple, With the built in voice Actions API you can do that both in online and offline mode. Here a short demo for you,

First prompt the user to input some voice,

    private void promptSpeechInput() {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
                getString(R.string.speech_prompt));
        try {
            startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
        } catch (ActivityNotFoundException a) {
            Toast.makeText(getApplicationContext(),
                    getString(R.string.speech_not_supported),
                    Toast.LENGTH_SHORT).show();
        }
    }

This will bring up the built in Google speech input screen and will take the voice inputs. Now after a voice input check the result and get the voice into a converted string,

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
        case REQ_CODE_SPEECH_INPUT: {
            if (resultCode == RESULT_OK && null != data) {

                ArrayList<String> result = data
                        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                // here the string converted from your voice
            String converted_text = (result.get(0);
            }
            break;
        }

        }
    }

Now you can manipulate the string in any way you want or Compare them with pre-defined action strings to execute a specific action and many more....

UPDATE:

To make the app work on after saying a specific command e.g. "OK Google", Just define a static String called "OK Google" and compare each voice input with this pre-defined String. If that matches the "OK Google" String then move to the next worlds and execute the instructions. For example,

"OK Google speak the the current time"

Here you can compare the first two words "OK Google" if that matches your pre-defined String move to the next words which is "speak the current time". For this you may save a set of arrays containing your commands like "speak the current time" will speak out the time.

To make it look more intelligent you can implement a background service and keeps listening to user's voice input.

PS: I'm not sure if that would be an efficient way but it's just another approach of doing this.

Bulbul answered 7/6, 2015 at 4:22 Comment(5)
While this is appropriate for receiving voice input on request by your app, the original question was to have 'OK google' trigger the app opening, which this does not doOrthochromatic
If I'm not wrong "OK Google" is just a pre-defined text which Google use to recognize the command. This can be done easily by defining a static string called "OK Google" and compare it with the user's voice input. It that matches the string we can compare the next instructions. To make it more fun we can implement this in background service so that it keeps listening to user in the background. Please correct me if anything is wrong with my idea. @OrthochromaticBulbul
Your answer, of course, relies on an activity being present and does not do any real time processing so using it to trigger a system wide 'ok google' style detection that you get through the Google app's voice actions API is not at all appropriate nor would your code work as a background service. Again, not a bad answer - just not an answer to the question asked.Orthochromatic
I've been looking for this for a very long time-do you know how I can have continues voice recognition while the screen is off, just like Google now? Thanks so much!Outcome
@RuchirBaronia did you find any solution for this... even i want to implement this functionality.Tearing
F
5

To integrate "OK Google" in your app is easy with following two steps.

First we need to declare in the manifest File

<activity..
 <intent-filter>
                <action android:name="com.google.android.gms.actions.SEARCH_ACTION" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
/>

Now we need to declare in the Activity onCreate

 if (getIntent().getAction() != null && getIntent().getAction().equals("com.google.android.gms.actions.SEARCH_ACTION")) {
            query = getIntent().getStringExtra(SearchManager.QUERY);
            Log.e("Query:",query);   //query is the search word              
        }

User should follow the syntax to detect by "Ok google", When a user says, “OK Google, search for phrase on app name”, Google first checks if there is an app called app name installed which has declared itself to be capable of handling such queries.

Frier answered 8/11, 2016 at 10:4 Comment(2)
you also need your app to be published on the google play store, otherwise Google Now will not recognize "app name" , see this guy experience, under "what should I watch out for ?" blog.prolificinteractive.com/2015/11/06/…Syzygy
@AnthonyDahanne, i have published my app in alpha testing and it is not working on many devices. does it also needed to be downloaded from playstore to make it work ?Eck

© 2022 - 2024 — McMap. All rights reserved.