I would like to know how can i get the device/user area code from within my android app I know there is a lib named TelephonyManager but i couldn't find the country code feature
To make myself clear: I have an activity which asks the user to enter his phone number and i want the area code to be taken from the device (from his carrier?) If i would open my app in new york i would get +212 as the code and a text box to enter my own number
I know it way too late. But I have a batter solution of it. Since doing it with the list of
String
and looping through it might slow it down. UsingHashmap
would be a batter solution for it. I wrote aPhoneUtil
class for it. Here is code ( i only have 20 or so countries but you can add more to it )1- Add This Array to Strings.xml File
2-CopyThis Function To your Class
3-Call Function GetCountryZipCode()
4-write Thank you Wais.
in this case this would look like:
So nobody has to look and double-check (context is a local variable of a valid Context object). Constants.DEVICE_COUNTRY is of Type String :) (just a variable)
This is code to put in your Main Activity to replace the code that Wais posted near the top of this page. Take the array that Wais posted and add that to your app in the resources. I might have slightly changed the name of the array to be stored in resources, other than that, this code should work well and be easier to read if you are learning: