Java 只从输入中获取数字并拨打该号码

Java 只从输入中获取数字并拨打该号码,java,android,numbers,Java,Android,Numbers,好的,我正在创建一个android应用程序,我非常喜欢从这里的输入中获取数字,不幸的是,我的代码由于某种原因无法工作 例如,当用户说:bel 0612345678 String one = "bel"; if (mostLikelyThingHeard.contains(one)) { String numbers = mostLikelyThingHeard.replaceAll("[^0-9]", ""); Intent call = new Intent(Intent.AC

好的,我正在创建一个android应用程序,我非常喜欢从这里的输入中获取数字,不幸的是,我的代码由于某种原因无法工作

例如,当用户说:bel 0612345678

String one = "bel";
if (mostLikelyThingHeard.contains(one)) {
    String numbers = mostLikelyThingHeard.replaceAll("[^0-9]", "");
    Intent call = new Intent(Intent.ACTION_DIAL);
    call.setData(Uri.parse("tel:" + numbers));
    tts.speak(numbers + " wordt gebeld.",
    TextToSpeech.QUEUE_FLUSH, null);
    found = true;
}
拨号程序必须拨打0612345678

String one = "bel";
if (mostLikelyThingHeard.contains(one)) {
    String numbers = mostLikelyThingHeard.replaceAll("[^0-9]", "");
    Intent call = new Intent(Intent.ACTION_DIAL);
    call.setData(Uri.parse("tel:" + numbers));
    tts.speak(numbers + " wordt gebeld.",
    TextToSpeech.QUEUE_FLUSH, null);
    found = true;
}
我还在AndroidManifest.xml中添加了正确的权限,我猜代码是错误的。

你能试试吗

Intent.ACTION_CALL
并确保您正在清单文件中使用这个before-application标记

<uses-permission android:name="android.permission.CALL_PHONE" />

您可以使用正则表达式并删除非数字

str = str.replaceAll("\\D+","");
试试这个


您需要从如下字符串中获取数字:

String numbers = mostLikelyThingHeard.replaceAll("[^\\d]", "");
要拨打该号码,您可以使用ACTION_call。操作拨号将只打开带有该号码的拨号屏幕。此外,您已经初始化了意图,但缺少对startActivityintent的调用

希望这有帮助

    String one = "bel";
String mostLikelyThingHeard="bel 0612345678";
if (mostLikelyThingHeard.contains(one)) {
    String numbers = mostLikelyThingHeard.replaceAll("[^0-9]", "");
    Intent callIntent = new Intent(Intent.ACTION_CALL);
    callIntent.setData(Uri.parse("tel:"+Uri.encode(numbers.trim())));
    startActivity(callIntent);
    tts.speak(numbers + " wordt gebeld.",
    TextToSpeech.QUEUE_FLUSH, null);
    found = true;
}
并在清单中添加权限

<uses-permission android:name="android.permission.CALL_PHONE" ></uses-permission>

您是否收到任何错误/异常?问题在于从输入中获取号码或呼叫?从输入中获取号码,我以前用于分割输入并获取最后分割的字符。这不起作用,所以我尝试了这段代码。意思是@user2683292您只需要字符串中的数值。我说得对吗?@是的!谢谢我想startActivity会正确触发拨号器。你也可以使用多个呼叫应用选项。是的,回答清晰有用!:D但是@MagicalPhoenixϡ那么所有角色都将被删除?比如说a6b7会变成67?是的。我尝试使用字符串编号=abc 9ddd935345.replaceAll[^0-9];它只保留数字:@user2683292[^0-9]和[^\\d]o.0有什么区别?对不起。。。正则表达式有点弱。。该离开办公室了。。happy Coding将尝试,trim做什么?编辑:它会删除空格,谢谢:
<uses-permission android:name="android.permission.CALL_PHONE" ></uses-permission>