Sunday, April 21, 2013

[android help] Prompt voice recognition from within onActivityResult in Android


I hope I'll be clear enough.


In my app, I have set it so that pressing the back button activates google's voice recognition service. In the onActivityResult, it checks whether the first word is "call" or "text" and proceed accordingly with the rest of the spoken sentence. For text specifically, it goes like "text [contact name] message [message content]" and then sends it using an smsManager. But since it might get the name or the message wrong, I want it to read out the message and the person's name first for confirmation which I did just fine.


The problem is, to confirm or cancel, I want to also use voice recognition. If after the message is read out, the user says something like send and only then it should proceed to send the message. So, what I need to know is how/if can I implement this (newVoiceCommand) in the code below:



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


.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...