使用 Android 获取电话美国本地区号
我想知道如何从我的 Android 应用程序中获取设备/用户区域代码 我知道有一个名为 TelephonyManager 的库,但我找不到国家/地区代码功能
为了让自己清楚: 我有一个活动要求用户输入他的电话号码,并且我希望从设备中获取区号(从他的运营商?) 如果我在纽约打开我的应用程序,我会得到 +212 作为代码和一个文本框来输入我自己的号码
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
使用
编译“com.googlecode.libphonenumber:libphonenumber:8.0.1”
依赖项。这使得任务变得简单并提供准确的结果,因为它是由谷歌提供的。在这里您可以了解更多信息。
我得到的输出是这样的:
这里我使用微调器。
1) 我创建了一个自定义适配器 CountryCodesAdapter:
2) 现在,我显示国家/地区代码微调器的活动的布局是:
3) java 活动代码如下:
很少有额外的功能:
a) 您的国家/地区将自动选择旋转器。
为了实现这一点,我使用了这个:
我认为这个方法比这里列出的其他方法好一点。所以我发布了这个。希望有人会觉得它有帮助:-)
GitHub 上的这个项目位于这里
Use
compile 'com.googlecode.libphonenumber:libphonenumber:8.0.1'
dependency. This makes the task easy and provides accurate result because it's by google.Here you can know more about it.
The output I m getting is something like this:
Here I m using spinner.
1) I have created a custom Adapter CountryCodesAdapter:
2) Now the layout of my Activity where I am displaying the spinner of country code is:
3) java Activity code is below:
Few extra features:
a) Your country will get automatically selected in the spinner.
To achieve I have used this:
I think this method is little better than other ways listed here. So I posted this. Hope someone may find it helpful :-)
This project on GitHub is here
在这种情况下,这看起来像:
所以没有人需要查看和仔细检查(上下文是有效 Context 对象的局部变量)。
Constants.DEVICE_COUNTRY 是字符串类型:)(只是一个变量)
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)
将这些添加到 string.xml
享受:)
add these to the string.xml
Enjoy:)
如果您碰巧使用 Google 的 libphonenumber,并且您使用电话号码,那么您很可能会这样做,您可以使用描述相同内容的内部数据。它不是公开的,因此您必须使用反射来访问它:
然后您可以搜索或处理该映射以获取您想要的数据。
If you happen to use Google's libphonenumber anyway, and if you work with phone numbers, it's rather likely you do, you can use its internal data that describes the same. It's not public, so you have to use reflection to reach it:
Then you can search or process that map to get the data you want.
我知道已经太晚了但我有一个更好的解决方案。因为使用
String
列表执行此操作并循环遍历它可能会减慢速度。使用Hashmap
将是一个更好的解决方案。我为它编写了一个
PhoneUtil
类。这是代码(我只有 20 个左右的国家,但你可以添加更多)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 a
PhoneUtil
class for it. Here is code ( i only have 20 or so countries but you can add more to it )在TelephonyManager类中,我找到了一个方法
public String getNetworkCountryIso()
,这可能就是你想要的。此外,还有一些其他方法可以返回有关手机的更多信息。您可以阅读 http://en.wikipedia.org/wiki/List_of_mobile_country_codes 查看哪个国家/地区以MCC为代表。In class TelephonyManager, I find a method
public String getNetworkCountryIso ()
, this may be what you want. Also, there are some other method which can return more info about the phone. You may read http://en.wikipedia.org/wiki/List_of_mobile_country_codes to see which country is represented by a MCC.这是放入您的主要活动中的代码,以替换 Wais 在本页顶部附近发布的代码。获取 Wais 发布的数组并将其添加到资源中的应用程序中。我可能稍微更改了要存储在资源中的数组的名称,除此之外,如果您正在学习,此代码应该可以很好地工作并且更易于阅读:
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:
1- 将此数组添加到 Strings.xml 文件
2- 将此函数复制到您的类
3- 调用函数 GetCountryZipCode()
4- 写入 谢谢 Wais。
1- Add This Array to Strings.xml File
2-CopyThis Function To your Class
3-Call Function GetCountryZipCode()
4-write Thank you Wais.
很抱歉恢复死线程,但我花了一段时间尝试解决这个问题,所以希望我的回答对其他人有所帮助。
原始问题中的示例令人困惑,您说要附加国家/地区代码,但以纽约区号 212 为例;这不是国家代码,而是本地区域代码。美国的国家/地区代码是 1。因此您希望最终号码看起来像“1-212-xxx-xxxx”。我的下面的回答解释了如何确定国家代码,而不是本地区域代码。
要获取您自己号码的国家/地区电话代码,请使用 getSimCountryIso()。不要使用 getNetworkCountryIso(); getNetworkCountryIso 将返回您当前所在的国家/地区,但如果您正在旅行,这将返回错误的国家/地区。例如,如果您来自阿尔巴尼亚但在纽约旅行,getSimCountryIso 将返回“al”,但 getNetworkCountryIso 将返回“us”。显然,如果您使用美国的国家/地区代码,则您的号码是错误的。
现在您有一个两个字符的国家/地区代码;例如加拿大将是“ca”。然后,您可以将其用作下表的查找键(从 http://www.sikosoft.com/item/ 获得International_telephone_country_codes_in_xml_format_using_iso_639-1)。
由于国家/地区代码永远不会改变,因此可以在应用程序中对查找表进行硬编码。不幸的是,似乎没有直接的方法来获取用户当前的国家/地区代码。
Sorry to revive a dead thread, but I spent a while trying to solve this issue so hopefully my response will help someone else.
The example in the original question is confusing, you say you want to append the country code, but give New York area code 212 as an example; That is not a country code it is a local area code. The country code for USA is 1. So you would want the final number to look like "1-212-xxx-xxxx". My answer below explains how to determine the country code, not the local area code.
To get the country calling code for your own number, use getSimCountryIso(). Do not use getNetworkCountryIso(); getNetworkCountryIso will return the country that you are currently located in, but if you are travelling this will return the wrong country. For example if you are from Albania but are travelling in New York getSimCountryIso will return "al", but getNetworkCountryIso will return "us". Obviously if you use the country code for the USA, you have the wrong number.
Now you have a two character country code; for example Canada will be "ca". You can then use this as a lookup key for the table below (obtained from http://www.sikosoft.com/item/international_telephone_country_codes_in_xml_format_using_iso_639-1).
Since country codes don't ever change, it is acceptable to hard code a lookup table in your application. Unfortunately there doesn't seem to be a direct way to get the user's current country code.
这个问题对我帮助很大,我想为大家提供一个从电话代码提供国家/地区代码的课程,反之亦然。
用法:
CountryCodes.getCode(String country)
从国家/地区符号返回国际代码CountryCodes.getCountry(String code)
从国际代码返回国家/地区符号可以提供此类的实例作为视图的适配器,例如在 Activity onCreate 中:
接下来是类代码
我正在使用它并且似乎可以工作,但是,您知道,死亡和税收是确定的。请随意定制它并提出修复建议。
华泰
This question helped me so much, I want to give you all a class that provide country code from phone code and viceversa.
Usage:
CountryCodes.getCode(String country)
returns international code from country signCountryCodes.getCountry(String code)
returns country signs from international codeAn instance of this class can be provided as adapter to Views, for example in Activity onCreate:
What follows is the class code
I'm using it and seems to work but, you know, just death and taxes are certain. Feel free to customize it and suggest fixes.
HTH