kareem Radwam
kareem Radwam

Reputation: 11

Send Voice or Text Command From My App To Google Assistant (Hey Google)

Can I sned Voice or Text Commands to Google Assistant to execute Commands?

I am using this Code now but it's open Normal Google Search Not Google Assistant.

 String command = "hey google , open camera";
 Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
 intent.setClassName("com.google.android.googlequicksearchbox", "com.google.android.googlequicksearchbox.SearchActivity");
 intent.putExtra("query", command);
 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //necessary if launching from Service
 startActivity(intent);

Upvotes: 1

Views: 684

Answers (2)

Hybridwane
Hybridwane

Reputation: 19

The work around I did was

1 - launch the Assistant with voice command intent

    Intent intent = new Intent(Intent.ACTION_VOICE_COMMAND);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);

2 - read the command with a TextToSpeach engine

first define the TTS variable outside the oncreate function

        private TextToSpeech mTTS;

then run this

        mTTS = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if(status==TextToSpeech.SUCCESS) {
                mTTS.setLanguage(Locale.US);

                mTTS.speak(" turn lights on ", TextToSpeech.QUEUE_FLUSH, null);
            }
        }

it worked like a charm

Upvotes: 1

youssef emam
youssef emam

Reputation: 1

this is how you open the google assistant from your app, but actually it is not possible to to send a command to it.

        Intent intent = new Intent(Intent.ACTION_VOICE_COMMAND);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);

Upvotes: 0

Related Questions