簡體   English   中英

從我的應用程序向 Google 助理發送語音或文本命令(Hey Google)

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

我可以向 Google 助理發送語音或文本命令以執行命令嗎?

我現在正在使用此代碼,但它打開的是普通的谷歌搜索而不是谷歌助手。

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

我所做的工作是

1 - 使用語音命令意圖啟動助手

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

2 - 使用 TextToSpeach 引擎讀取命令

首先在oncreate之外定義TTS變量function

        private TextToSpeech mTTS;

然后運行這個

        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);
            }
        }

它就像一個魅力

這是您從應用程序中打開谷歌助手的方式,但實際上無法向其發送命令。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM