I'm currently working on a app to blacklist SMS from a specific country, I already created my BroadcastReceiver class, which start a service to compare the phone number to the ones stored in the database.
My database contains mobile country code, and I would like to compare the received country code to those in the database, unfortunately I didn't find any method to retrieve the country code from the number. I thought that I could retrieve it with a simple substring call, but I noticed that not all the country codes are the same length, so I couldn't do this like that.
I was wondering if the prefixes were unique (that is to say if a prefix could be the prefix of another), if no, then could I start by taking the maximum size prefix and then a shorter prefix and so on ?
Could you give me a hint on how to do this ?
Thank you
Phone number rules are rather complicated and there are many special cases. To extract country code reliably, I'd suggest to use a specialized library. There's a well-known library from Google, called libphonenumber:
https://code.google.com/p/libphonenumber/
Take a look at extractCountryCode method from PhoneNumberUtil, or PhoneNumberUtil.parse which returns a PhoneNumber object on which you can call getCountryCode.
It's open source, so, alternatively, if you don't want to mess with additional libraries, you can take a look at its implementation for hints on how to handle country codes by yourself.
Related
I have an app where visitors register from their phone numbers (much like WhatsApp or Telegram). I store the number in e164 format.
Once he is connected, the application synchronizes his contacts with the database in order to see which contact is using the application. during synchronization the application retrieves the list of numbers from its phonebook and converts them into E.164 format before sending them to the server. so far everything is working fine.
Here is where the problem arises: recently, a country (the Ivory Coast) has decided to change its phone numbers from 8 digits to 10 digits. What's the best way to handle this?
Treat a phone number as an arbitrary value until you can verify it works. Countries change the structure of their telephone numbers all the time and in unpredictable ways.
What is valid one day may not be valid the next. What rules apply one day may not apply the next. There is absolutely no way of knowing what might happen.
Phone numbers do not really adhere to any standard at all. E.164 only establishes the calling prefix, so that's something that might follow standards, but even those can change arbitrarily as countries get created, or merge.
In other words, this is all political and there is no way of predicting the future.
In some locales, a person's full name is written out with last name (family name, surname) then first name (personal name, given name), instead of the more common first name then last name order.Is there any API in Android which can take care of this name ordering for my app depending on the locale of the phone.
I'm afraid, there's no API for that. I've been looking fot it for a while and all that i know is what #Shervin shared.
Currently:
You can try to manage that all yourself, by getting title, name, second name and last name from user. In english it's easy to order it correctly and concatenate it with space. Unfortunately - not every language use spaces.
Research:
CLDR is project providing any locale-specific data (for example in ICU), but order of name and family name is not there.
Possible solution:
Most of solutions out there can be summed up to:
If designing a form or database that will accept names from people
with a variety of backgrounds, you should ask yourself whether you
really need to have separate fields for given name and family name.
Sometimes you may opt for separate fields because you want to be able
to use part of the name to address the person directly, or refer to
them. For example, when Google+ refers to "Richard's contacts". Or
perhaps it's because you want to send them emails with their name at
the top. Note that not only may you have problems due to name syntax
here, but you also have to account for varying expectations around the
world with regards to formality (not everyone is happy for a stranger
to call them by their given name). It may be better to ask separately,
when setting up a profile for example, how that person would like you
to address them.
http://www.w3.org/International/questions/qa-personal-names#fielddesign
So you must choose if you need to ask your user separately for first name and last name (should be usefull in sorting), but even then - you need to know how they name should be displayed.
Mostly you'll see form that asks users to provide: First name, Last name and Displayed name. Check here (fourth heading) for explanation.
I am building an app that makes use of phone numbers. I would like to have a user select a country and then based on that country prepend the country code to their phone number.
Is there a standard way for doing this in android? I thought the answer was Place AutoComplete, an android service for adding places, but that does not seem to be the answer; at least I can't get the country code from that service as explained on The Google Developer Academy site.
Does anyone know how I might get this done?
You can use this library by google for handling phone numbers on android. You will need to send the country code once the user selects and then use the PhoneNumberUtil.getInstance().parse(,)
Is there any algorithm which can parse or decode the String results obtained from BarCode Scanner using Zxing?
I want to extract Lot number or serial number from the scanned result. Is there any standard algorithm to do so?
I currently use,
String lotNumber=scanResults.substring(24,26);
if (lotNumber.equalsIgnoreCase("10")){
System.out.println("Lot numberrrrrrrrrrr " + a.substring(26,a.length()));
}
Thanks
Sneha
Look at the class ResultParser. It will try to parse the result string as something more meaningful. For example "mailto:foo#example.org" would come back as EmailParsedResult, letting you know it's an email address and parsing out the address for you.
I am not sure what this lot number is -- it's not well-known and so not implemented outside your system. But yes you can find a lot of this kind of logic in the project already, which you can perhaps reuse and modify.
no there is no magic in java or any language which can tell you what is that data for which that bar code was generated. You should know the data and should know its content type (json, xml etc) and then use the parser. Or if its not as per the defined standards (any random string) than do as what you are doing.
I'm pretty sure it's not viable to do what I'd like to based on some initial research, but I figured it couldn't hurt to ask the community of experts here in case someone knows a way.
I'd like to create a custom field for contacts that the user is able to edit from the main Contacts app; however, the user should only be allowed to select from a list of four specific values. A short list of string values would be ideal, but an int with a min/max range would suffice.
I'm interested in knowing if it's possible either way, but also wondering if it make sense to go this route performance wise. More specifically, would it be better to look up a contact (based on a phone number) each time a call or SMS message is received or better to store my own set of data (consisting of name, numbers, and the custom field) and just syncing contact info in a thread every so often? Or syncing contacts the first time the app is run and then registering for changes using ContentObserver?
Here is a similar question with an answer that explains how to add a custom field to a contact.
Thanks in advance.
I don't see the purpose to have your own set of data against contacts stored in your separate database, as you obviously will run into sync issues. You can use the mimetype and store whatever you want against the contact, in the manner you linked.
Whenever you want to looup contacts you can do that by using your custom mimetype. You get the contact id (from ContactsContract.Data.CONTENT_URI), and then you run another query to get the contact details (from ContactsContract.Contacts.CONTENT_URI). Please note these are different tables.
I'd like to create a custom field for contacts that the user is able to edit from the main Contacts app
I don't see that possible, editable from the main app, when you use your custom mimetypes, and you don't have much options here. The main contact app will display only the fields that are in SDK. You can store details against contacts but they won't show up in the inbuilt edit contact screen.