【问题标题】:Android Making a Call Only Works the Second TimeAndroid 拨打电话只能在第二次使用
【发布时间】: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


【解决方案1】:

根据 danny117 的评论...

promptSpeechInput();

        try {
            Thread.sleep(18000);

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        dialPhoneNumber(num);

promptSpeechInput 开始一个新活动,该活动的结果是语音文本?如果这是正确的,那你为什么要提示输入,休眠 18 秒(顺便说一下,不要在主线程上休眠),然后假设你的输入已经准备好?正如 danny 所说,提示输入应该是 if 块做的最后一件事。拨打号码应从onActivityResult发起。

另外,为什么会有“123”的默认电话号码?这永远不会是正确的,因此不是明智的默认设置。重申一下,如果你让线程休眠一段时间以等待其他事情发生,那么你几乎可以肯定是以错误的方式接近它。而且,如果您在 Android 中让主线程进入睡眠状态,那您的做法绝对是错误的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多