3

我希望我会足够清楚。

在我的应用程序中,我进行了设置,以便按下后退按钮激活谷歌的语音识别服务。在 中onActivityResult,它会检查第一个单词是“call”还是“text”,并相应地处理剩下的句子。具体而言,对于文本,它类似于“文本 [联系人姓名] 消息 [消息内容]”,然后使用smsManager. 但由于它可能会弄错名称或消息,我希望它先读出消息和人的姓名以进行确认,我做得很好。

问题是,要确认或取消,我还想使用语音识别。如果在读出消息后,用户说类似发送的话,那么它才应该继续发送消息。所以,我需要知道的是如何/是否可以newVoiceCommand在下面的代码中实现这个():

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

    switch (requestCode) {
    case RESULT_SPEECH: {
        if (resultCode == RESULT_OK && null != data) {
            s="";
            text = data
                    .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            decide = text.get(0).split("\\s");
            for (int i=1; i<decide.length;i++) s= s+decide[i] + " ";
            if (decide [0].equals("text")){
                if (!s.equals("")){
                    msg = s.split(" message");
                    char[] stringArray = msg[0].toCharArray();
                    stringArray[0] = Character.toUpperCase(stringArray[0]);
                    msg[0] = new String(stringArray);
                    contact = get_Number (test(msg[0]));
                    Intent intent = getIntent();
                    finish();
                    startActivity(intent);
                    String temp = "Are you sure you want to send " + msg[1] + " to " + test(msg[0]);
                    speakOut (temp);

                    if (newVoiceCommand.equals("send")){
                        try {
                            SmsManager smsManager = SmsManager.getDefault();
                            out.append(contact);
                            smsManager.sendTextMessage(contact, null, msg[1], null, null);
                            Toast.makeText(getApplicationContext(), "SMS Sent! to "+msg[0] + " at " +contact,
                                        Toast.LENGTH_LONG).show();
                          } catch (Exception e) {
                            Toast.makeText(getApplicationContext(),
                                "SMS faild, please try again later!",
                                Toast.LENGTH_LONG).show();
                            e.printStackTrace();
                          }
                    }
                    else if (newVoiceCommand.equals("no")) out.append("Not sending");
                }
            }
            else if (decide[0].equals("call")){
                out.append (s);
                if (!s.equals(""))  {
                    call (s);
                }
            }
        }
        break;
    }
    }
}

我目前的尝试:

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

    switch (requestCode) {
    case RESULT_SPEECH: {
        if (resultCode == RESULT_OK && null != data) {
            s="";
            //Grab the speech results and save them in an arraylist
            text = data
                    .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            //Split each word into an array item
            decide = text.get(0).split("\\s");
            //Re-concatenate the words starting with the second word together
            for (int i=1; i<decide.length;i++) s= s+decide[i] + " ";
            //If the first word is "text", then send an SMS using the rest of the information spoken
            if (decide [0].equals("text")){
                if (!s.equals("")){
                    check (s);
                    Intent spIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                    spIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en_US");

                    try {
                        startActivityForResult(spIntent, RESULT_SPEECH2);
                    } catch (ActivityNotFoundException a) {
                        Toast t = Toast.makeText(getApplicationContext(),
                                "Opps! Your device doesn't support Speech to Text",
                                Toast.LENGTH_SHORT);
                        t.show();
                    }
                    switch (requestCode) {
                    case RESULT_SPEECH2: {
                        if (resultCode == RESULT_OK && null != data) {
                            text2 = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                            s2 = "";
                            if (text2.get(0).equals("send")){
                                smsText (s);
                            }
                            else if (text2.get(0).equals("cancel")) return;
                        }
                    }
4

1 回答 1

2

简而言之,所有语音识别结果都将通过对onActivityResult. 您不会阻止并等待结果,因此您需要一种方法来确定识别是响应命令还是在收到结果时进行确认。

这是参数requestCodeActivity.startActivityForResult()用途:

有时您想在活动结束时从活动中获取结果。例如,您可以启动一个活动,让用户在联系人列表中选择一个人;当它结束时,它会返回被选中的人。为此,您startActivityForResult(Intent, int)使用第二个整数参数来标识调用的版本。结果将通过您的onActivityResult(int, int, Intent) 方法返回。

如果您在启动语音识别活动时使用两个不同的值requestCode(例如,定义private final static int REQUEST_SPEECH_COMMAND = 1, REQUEST_SPEECH_CONFIRMATION = 2),您可以在 中做出不同的响应onActivityResult。请注意,您需要存储第一次语音识别的结果(操作、消息和收件人),以便第二次可以使用此信息。

于 2013-04-21T03:16:04.720 回答