简体   繁体   中英

Google Now integration with app

I'm trying to build an voice controlled application which can perform some tasks depending on the commands.
I wanted to add Google Now features also to it so that if the user asks some questions like about weather info, news, about celebrities etc then I can get results from Google Now.

Is there any way to integrate Google now functionality in my app?

Check out the Voice Reorganization in Android

You can implement it as below :

Write the below code on click event of button which is responsible for firing off the voice intent.

/**
 * Instruct the app to listen for user speech input
 */
private void listenToSpeech() {
    //start the speech recognition intent passing required data
    Intent listenIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    //indicate package
    listenIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName());
    //message to display while listening
    listenIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say a word!");
    //set speech model
    listenIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    //specify number of results to retrieve
    listenIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 10);
    //start listening
    startActivityForResult(listenIntent, VR_REQUEST);
}

When the intent calls back, we display the transcribed voice.

/**
 * onActivityResults handles:
 *  - retrieving results of speech recognition listening
 *  - retrieving result of TTS data check
 */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    //check speech recognition result
    if (requestCode == VR_REQUEST && resultCode == RESULT_OK)
    {
        //store the returned word list as an ArrayList
        ArrayList<String> suggestedWords = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
        //set the retrieved list to display in the ListView using an ArrayAdapter
        wordList.setAdapter(new ArrayAdapter<String> (this, R.layout.word, suggestedWords));

     //to open the result in browser 
     Intent intent = new Intent(Intent.ACTION_VIEW, 
     Uri.parse("https://www.google.co.in/?gws_rd=cr#q="+suggestedWords));
startActivity(intent);
    }
    //tss code here
    //call superclass method
    super.onActivityResult(requestCode, resultCode, data);
}

So far the closest thing I have found to your requirements is

RecognizerIntent.ACTION_VOICE_SEARCH_HANDS_FREE

This basically launches the Google Now screen on top of your app and responds back with the voice feedback as Google Now does.

I haven't found any way yet to listen in background and get the speech result or text result which can be converted into speech by TTS engine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM