Get only digits from input and call that number Android

时光毁灭记忆、已成空白 提交于 2020-01-05 17:58:10

问题


Okay, I am creating an android app, I do like to get the number from an input here mostLikelyThingHeard unfortenately my code doesn't work for some reason.

For example when the user says: bel 0612345678

The dialer has to call 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;
}

I also added the right permissions in AndroidManifest.xml, I guess the code is wrong.


回答1:


You need to get numbers from String like this:

String numbers = mostLikelyThingHeard.replaceAll("[^\\d]", "");

To call that number, you can use ACTION_CALL. ACTION_DIAL will just open a dialer screen with that number. Also, you have initialized intent, but you are missing call to startActivity(intent)

Intent call = new Intent(Intent.ACTION_DIAL);
call.setData(Uri.parse("tel:" + numbers));
startActivity(call);

Hope this helps.




回答2:


Can you try this

Intent.ACTION_CALL

and make sure that you are using this before application tag in manifest file

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



回答3:


You can use regex and delete non-digits.

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



回答4:


Try this one..

   Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + yours_numbers_values));
   startActivity(intent);



回答5:


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

And in Manifest add permission

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


来源:https://stackoverflow.com/questions/27505055/get-only-digits-from-input-and-call-that-number-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!