我希望我会足够清楚。
在我的应用程序中,我进行了设置,以便按下后退按钮激活谷歌的语音识别服务。在 中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;
}
}