接受用户发言并开始特定服务

时间:2016-05-21 16:33:50

标签: android speech-recognition voice-recognition

这是我想要做的,我已经指定了一个触发词现在我想要我的应用程序要做的是监听表单用户,将该输入转换为字符串,然后将该字符串与我的触发器字符串进行比较,如果他们匹配然后我的应用程序应该带我们到消息服务我不知道如何将arraylist转换为字符串请求指导我进一步。 先感谢您  这是我的代码:

 package com.example.ahsan.test;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.widget.Toast;

import java.util.ArrayList;


/**
 * Created by ahsan on 5/21/2016.
 */

public class MainActivity extends Activity {
    private static final int RESULT_SPEECH = 1;
    String trigger="open messaging";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /* showing google speech input dialog*/
        Intent intent = new Intent(
                RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
        try {
            startActivityForResult(intent, RESULT_SPEECH);

        }
        catch (ActivityNotFoundException a) {
            Toast t = Toast.makeText(getApplicationContext(),
                    "Opps! Your device doesn't support Speech to Text",
                    Toast.LENGTH_SHORT);
            t.show();
        }


    }
    /* Receiving speech Input */

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && null != data) {

            ArrayList<String> text = data
                    .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

        }
    }
}

1 个答案:

答案 0 :(得分:1)

我认为这就是你的意思?下面的s是数组中的所有字符串:

    for(String s : text){
        if(s.matches(trigger){
        // We have a match!
          break;
        }
     }