问题
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