How can I get my app to know which contact name is calling? I'd like the app to check the entire number, if that's possible. I saw someone here on SO saying to check only a part of the number, and that's a partial solution. But would it be possible to check the entire number? I think it must be, because Dialer always knows who's calling me (unless they cheat somewhere by getting the country of the phone or something).
For example in Portugal we have +351 123 456 789. On other contries the number are bigger or smaller, and the code is different. From what I saw here (https://en.wikipedia.org/wiki/E.164):
As described in by the ITU, the E.164 general format must contain only digits split as follows:
- Country code (1 to 3 digits)
- Subscriber number (max 12 digits)
So I can't check remove the first 3 digits because there are countries with 1 or 2 digits. Also I can't check 12 digits because as a start, in Portugal we have only 9. I read there are countries with 7 digits, so that would be the minimum, and 12 would be the maximum. But I'd have to put the minimum and it could get the number wrong. But the Dialer app always knows somehow. Does anyone know how to do it properly?
I've thought already in checking all the codes from all the countries (would need a list) and check the first digits to see if any match any country code. But 2 problems came: I'd have to be updating it, and more important, let's say a country has +35 as code. Then the person's number starts with a 1. That's +351, which is from Portugal. No idea how I can avoid these situations.
Hopefully there's an easier way, maybe even some class from Google which I don't know about, or some library or if not, maybe someone has better ideas.
Just in case it might be useful, here's a link from Wikipedia with all the country codes: https://en.wikipedia.org/wiki/National_conventions_for_writing_telephone_numbers.
Any help would be greatly appreciated!
NOTE: I haven't put Android on the tags, but I'm currently working on it. This doesn't matter to the question because I only need to know the algorithm, UNLESS there is some easier way of checking the phone number from the Android library or anything related to Android and in that case, I give preference to the Android solution.
Update: I could check from the end to the beginning of both numbers and see if they match until the smaller one end. For example +351 123 456 789 called and I have saved 123 456 789 only. But in another country a number might be +35 1123 456 789, and in this case, I'd be excluding the country code of some countries. Are there any other ideas?