In my android app, I am sending USSD codes (#144#73#) using below Intent :
String baseUssd = Uri.encode("#") + "144" + Uri.encode("#");
StringBuilder builder = new StringBuilder();
builder.append(baseUssd);
builder.append("73");
builder.append(Uri.encode("#"));
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + builder.toString()));
It's working well.
What I want now is to send this code :
#144#73MA#
I run this using the dial pad, following the Operator USSD menu, that worked.
But if I try to do this programmatically using the above Intent that didn't work.
I know that alphabetic characters can't be used when typing code with the Dial Pad, but I though that It can be possible programmatically !!
Any Idea please !
Edit
When I try to send this programmatically : #144#73MA# I noticed that the Dialer application changes the alphabetic characters to their corresponding digit in the dial pad. Meaning that the dialer transform this : #144#73MA#
to this #144#7362# : why ?
Because :
the M matches the digit 6
the A matches the digit 2
Meaning that the dialer transform this : #144#73MA#
to this #144#7362# : why ?
I will try to answer only the why part.
Intent.ACTION_CALL is handled by OutgoingCallBroadcaster class. If you look at processIntent() method, there is this piece of code (lines 438~448 as of this writing):
String number = PhoneNumberUtils.getNumberFromIntent(intent, this);
// Check the number, don't convert for sip uri
// TODO put uriNumber under PhoneNumberUtils
if (number != null) {
if (!PhoneNumberUtils.isUriNumber(number)) {
number = PhoneNumberUtils.convertKeypadLettersToDigits(number);
number = PhoneNumberUtils.stripSeparators(number);
}
} else {
Log.w(TAG, "The number obtained from Intent is null.");
}
There PhoneNumberUtils.convertKeypadLettersToDigits() converts the letters into the equivalent numeric digits:
public static String convertKeypadLettersToDigits (String input)
Translates any alphabetic letters (i.e. [A-Za-z]) in the specified phone number into the equivalent numeric digits, according to the phone keypad letter mapping described in ITU E.161 and ISO/IEC 9995-8.
Returns
the input string, with alpha letters converted to numeric digits using the phone keypad letter mapping. For example, an input of "1-800-GOOG-411" will return "1-800-4664-411".
Hope this helps.
Related
I generate OTP after getting the phone number but now I want the FIRST TWO digits and LAST TWO digits of the mobile number.
For example, if user enters 9866523487 as the mobile number then OTP will be 9887.
You can try this method in your code.
String generateOTP (String number) {
StringBuilder otp = new StringBuilder("");
if(number != null) {
otp.append(number.substring(0,2));
otp.append(number.substring(number.length() - 2, number.length()));
}
return otp.toString();
}
This method uses the substring properties of a string. Read in details from here. Substring in Java.
I am calculating priceAfterDsicount then place value in EditText(so user can modify it after App calculation)
Value retured from format is arabic numbers
this is code
private fun handleDiscount() {
val price = edPackagePrice.text.toString().toDoubleOrNull()
val discount = discount_percentage_edit_text.text.toString().toIntOrNull()
"handleDiscount before price$price discount$discount".log(mTag)
price?.let {
discount?.let {
val finalValue = String.format("%.1f", ValuesHelper.getPercentage(price, discount),Locale.US)
price_after_discount_edit_text.setText(finalValue)
"handleDiscount ook price$price discount$discount, final $finalValue".log(mTag)
}
}
if (discount == null) {
"handleDiscount $price , ${edPackagePrice.text}".log(mTag)
price_after_discount_edit_text.setText("")
price?.let { price_after_discount_edit_text.setText(price.toString()) }
}
"handleDiscount after price_after_discount_edit_text${price_after_discount_edit_text.text.toString()} ".log(mTag)
}
Output at Run
so what is problem?
NOTE
App language is arabic(user can change it from app).
I found other way to convert arabic number to english
Use Java.math.BigDecimal ,it will automatically construct English numeric equivalent to Arabic numeric equivalent , After you have English numeric equivalent do your calculation and when you want to update the UI after calculation use the device locale to show the end result to user in Arabic , BigDecimal only work with digits i.e. 0123. For special characters like , you have to do exception handling , we have .isDigit() method of Character class that you can leverage to iterate over whole input string and remove , before doing calculation,hope this helps.
I have EditText, in that we can enter all keyboard characters like alphanumerics, smileys, special characters.
I have to validate for some special characters and all smileys not to be allowed. Needs to show all the entered non-allowed characters as alert dialog.
I tried with many smileys, for smileys it is taking only empty spaces inside the below function and for validation it is showing only one smiley, sometimes it is showing two.
Below is the code.
public static String validateSpecialCharacters(String message, Pattern messagePattern){
StringBuilder matchedCharacters = new StringBuilder();
Map<Character, Integer> uniqueSpecialCharacters = new HashMap<>();
Matcher m = messagePattern.matcher(message);
while (m.find()){
if(!uniqueSpecialCharacters.containsKey(message.charAt(m.start()))) {
uniqueSpecialCharacters.put(message.charAt(m.start()), 1);
matchedCharacters.append(message.substring(m.start(),m.end()));
}
}
return matchedCharacters.toString();
}
Inside the while the if condition is getting true only once or twice. But I have entered different smileys. The issue is the previous key added for the first smiley is similar with the second. But actually the smiley's are not similar. How to solve this.
So using google places reference (detailed web-service) i retrieved a "formatted phone number" its in the form of (256) 922-0556. The goal is to call this number. The way I am trying is be using an intent. However, the number above is not a in the format to use Uri parse method. Anyone know a solution to call this number? Is there a different intent or a good way to turn this into Uri data? I have seen the opposite of this done like so:
1234567890 → (123) 456-7890
String formattedNumber = PhoneNumberUtils.formatNumber(unformattedNumber);
But i want to do the reverse of this. any ideas or alternative solutions?
Here is my code:
protected void onPostExecute(Boolean result){
Intent callintent = new Intent(Intent.ACTION_CALL);
callintent.setData(Uri.parse(phoneNum));
try {
startActivity(callintent);
}catch (Exception e) {e.printStackTrace();}
}
Where phoneNum is a formatted phone number string retrieved from GooglePlaces via JSON
To expand on Peotropo's comment: is there a better way to replace values than the following?
phoneNum = phoneNum.replace(" ", ""); // gets rid of the spaces
phoneNum = phoneNum.replace("-", ""); // gets rid of the -
phoneNum = phoneNum.replace("(", ""); // gets rid of the (
phoneNum = phoneNum.replace(")", ""); // gets rid of the )
This is simple string. Use String.replace() method to remove extra chars.
You can also use replaceAll method:
String phoneNumber = "(123)123-456465"
return phoneNumber.replaceAll("[^0-9]", "");
Not tested docs are here:
replaceAll
Java regular expressions
You don't need to do a string replace. You can use the Spannable code below to have your phone automatically recognize the number and call it. It adjusts for parentheses, spaces and dashes.
// call the phone
SpannableString callphone = new SpannableString("Phone: " + phone);
callphone.setSpan(new StyleSpan(Typeface.BOLD), 0, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
callphone.setSpan(new URLSpan("tel:"+phone), 7, 21, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView zphone = (TextView) findViewById(R.id.phone);
zphone.setText(callphone);
zphone.setMovementMethod(LinkMovementMethod.getInstance());
It will display
Phone: (123) 456-7890
Where you see 7,21 in the code above it is saying to start at the 8th character, which is the ( and end at the 21st character which is the last digit in the phone number. Adjust it to display how you want.
Nothing special to do in your view:
<!-- Phone Number Label -->
<TextView
android:id="#+id/phone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"/>
I can obtain the phone number from an incoming call or from a sms message. unfortunately, in case of the SMS there might be the country code in it. So, basically I need to obtain the plain phone number, without country code, in order to compare it with existing numbers in Contacts.
If you want to compare phone numbers you can always use the
PhoneNumberUtils.compare(number1, number2);
or
PhoneNumberUtils.compare(context, number1, number2);
Then you don't have to worry about the country code, it will just compare the numbers from the reversed order and see if they match (enough for callerID purposes at least).
fast untested approach (AFAIK phone numbers have 10 digits):
// As I said, AFAIK phone numbers have 10 digits... (at least here in Mexico this is true)
int digits = 10;
// the char + is always at first.
int plus_sign_pos = 0;
// Always send the number to this function to remove the first n digits (+1,+52, +520, etc)
private String removeCountryCode(String number) {
if (hasCountryCode(number)) {
// +52 for MEX +526441122345, 13-10 = 3, so we need to remove 3 characters
int country_digits = number.length() - digits;
number = number.substring(country_digits);
}
return number;
}
// Every country code starts with + right?
private boolean hasCountryCode(String number) {
return number.charAt(plus_sign_pos) == '+'; // Didn't String had contains() method?...
}
then you just call these functions