1

How would i go about getting the area code of a device?

I have an import script where im grabbing all phone numbers in there contacts list, so i can compare it to a list in my database and act accordingly. However i know some people wont put the area code for some of there contacts due to the fact that they dont need it for a local number. I need to get there area code so i can add it to the number im checking.

NOT the postal 5 digit zip code, the 3 digit area code.

All the ways i found want me to get there location then get it, i dont want to do that, thats too much work to get 3 digits.

NodeDad
  • 1,519
  • 2
  • 19
  • 48

1 Answers1

1

If the contacts do not have an area code, then you assume that the area code is the local area code. If its the local area code, then the user's phone number must have this area code. Thus, you can get the user's telephone number to determine the local area code and apply that area code to any numbers that do not have an area code.

To get the user's phone number you can use this code -

TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tm.getLine1Number();

Copied from this question.

Then you can extract the area code by looking at the first 3 (or possibly 4 if the length of the String is 11 chars) digits.

String areaCode = mPhoneNumer.substring(0, mPhoneNumber.length()==10 ? 3 : 4);

Note: Ensure that you add the READ_PHONE_STATE permission to the AndroidManifest.xml file by adding this line -

<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
Community
  • 1
  • 1
David says Reinstate Monica
  • 19,209
  • 22
  • 79
  • 122
  • This is great and works great, HOWEVER what about tablet devices that have a phonebook... theres no phone number stored for tablets. Because i get a NULL value when testing on my tablet and it crashes. Granted im going to put a try and catch to solve the crashing and if statement for if the value is null alert that they dont have a phone number and ask for there local area code for there contacts. But do you believe theres another way? – NodeDad Sep 25 '13 at 20:39
  • 1
    @AaronRussell Ah.... that makes things a bit more complicated. I dont have anything now, but Ill try to think of something. My initial hunch though is that it is impossible because (1) tablet have no phone capabilities and (2) there is no way to make a phone number less ambiguous without the area code. Bottom line - I dont think there is another way besides what you said if there is no phone number. – David says Reinstate Monica Sep 25 '13 at 20:48
  • Now i know my question is how to get it without using location services, maybe i can wrap an if statement for if its null to try and get it with location services? Do you know how i can it like that? We can put together a full proof way with that and post it for everyone. – NodeDad Sep 25 '13 at 21:21
  • Since i know we'll put something together i went ahead and rewarded the bounty and accepted your answer. – NodeDad Sep 25 '13 at 21:23
  • I don't think location services would be a reliable way to do this, especially if you are on a tablet. The user could be anywhere, so you would have to take their current location, look it up in some sort of area code database (which wouldn't be reliable because some areas have multiple area codes, and some area codes are somewhat area-less, such as cellphone only area codes) and then guess that. I think if you REALLY wanted to use location based services, you could prompt for an area code and use location services to generate a suggestion, but nothing more. – David says Reinstate Monica Sep 25 '13 at 21:32
  • I think in the end just a simple popup dialog with a input box would be better, and more accurate i suppose. If you think of anything else ever in the future, please let me know. Thanks @Dgrin91 – NodeDad Sep 25 '13 at 22:27