【发布时间】:2018-12-11 20:58:46
【问题描述】:
所以我正在使用语音助手制作 Speech to Text 应用程序。我正在尝试拨打电话功能,以便用户可以说出一个号码,然后它会打电话给它。
我快到了,但我第二次说这个号码时才响起。第一次显示“未发送呼叫”。
我发现这是因为;当用户说出数字时,它不会先更新变量然后调用“调用”函数。我几乎尝试了所有方法,但没有正确更新变量。
即
private TextView txtSpeechInput;
public String num = "123";
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
txtSpeechInput.setText(result.get(0).replaceAll("\\s+", ""));
num = txtSpeechInput.getText().toString();
}
break;
}
}
}
public void dialPhoneNumber(String phone) {
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + phone));
if (intent.resolveActivity(getPackageManager()) != null) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
startActivity(intent);
}
return ;
}
}
private void processResult(String command) {
command = command.toLowerCase();
if(command.indexOf("time") != -1) {
Date now = new Date();
String time = DateUtils.formatDateTime(this, now.getTime(), DateUtils.FORMAT_SHOW_TIME);
speak("The time is " + time);
}
if(command.indexOf("date") != -1) {
String date = DateFormat.getDateInstance().format(new Date());
speak("The date is " + date);
}
else if (command.indexOf("open") != -1) {
if(command.indexOf("browser") != -1) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.co.uk/"));
startActivity(intent);
}
}
if(command.indexOf("call") != -1) {
promptSpeechInput();
try {
Thread.sleep(18000);
} catch (InterruptedException e) {
e.printStackTrace();
}
dialPhoneNumber(num);
}
}
在此代码中,当用户说“拨打电话”时,它会打开另一个提示以获取语音输入。将其存储在txtSpeechInput(其中显示 results.get0)中,然后在那个阶段我更新“num”变量并将其转换为字符串。
然后运行dialPhoneNumber
现在假设我第一次运行它并说“07123456789”,它会说call not sent,因为它试图调用默认的123,如果我再次说它或另一个号码,那么它会响起@ 987654326@.
在调用电话功能之前如何以及为什么不更新?
【问题讨论】:
-
事情在发生时发生。想想这个。如果您刚刚从活动中获得了获取号码的结果,那么下一行代码应该是拨打该号码的电话。如果您只是要求语音输入,那么您不会做任何其他事情。你已经完成了,不要去睡觉你在例程中没有更多的代码。如果让我感到困惑,请不要混淆 if 和 else。
-
是的..有道理哈哈。谢谢我在完全错误的区域调用拨号功能。
标签: android variables call speech-to-text