충분히 명확 해지기를 바랍니다.Android의 onActivityResult 내에서 음성 인식이 가능합니다.
내 앱에서 뒤로 버튼을 누르면 Google 음성 인식 서비스가 활성화되도록 설정했습니다. onActivityResult
에서 첫 번째 단어가 "통화"인지 "텍스트"인지 확인하고 나머지 음성 문장과 함께 진행합니다. 텍스트의 경우 구체적으로 "텍스트 [연락처 이름] 메시지 [메시지 내용]"과 같이 이동 한 다음 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;
}
}
필자가 필요한 것은 speakOut (s) 메서드가 호출 된 후 메시지를 읽은 후 자동으로 음성 인식 활동을 시작하기 위해서입니다. 하지만 그 말은 내가 할 수없는 첫 번째 말 결과에 대한 사례 내에서 활동을 시작해야한다는 것을 의미합니다. – iMimi
@ user2303527 정확히 무엇을해야할까요? 'startActivityForResult()'를 호출하여 onActivityResult '. 왜 이럴 수 없니? – user113215
내가했을 때, 추락했습니다. 제안을 구현하려는 나의 시도로 게시물을 업데이트했습니다. – iMimi