如何在Android应用程序中获取ISO国家代码?
我是 Android 应用程序的新开发人员。当我传递带有国家/地区代码的手机号码时,我想获取 ISO 国家/地区代码。如果我传递手机号码为 1-319-491-6338,我可以在 android 中获得国家 ISO 代码为 US / USA 吗?
我写的代码如下:
TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String countryCode = tm.getSimCountryIso();
String mobileno="1-319-491-6338";
这里,哪里可以传手机号码呢?
有人可以帮我吗?
提前致谢
I am a new developer on android application. I would like to get the ISO Country code when I pass the mobile number with country code. If I pass the mobile number as 1-319-491-6338, can I get country ISO code as US / USA in android?
I have written the code as follows:
TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String countryCode = tm.getSimCountryIso();
String mobileno="1-319-491-6338";
Here, where can I pass the mobile number?
Can anybody please help me ?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能无法通过标准 API 以编程方式查询国家/地区代码,但您可以在应用程序中包含一个表格。通过 Google 可以轻松找到这样的表格(例如 http://countrycode.org/)。
危险威尔·罗宾逊!:然而,人们应该问自己你想回答什么问题。您的问题隐含着这样的假设:国际拨号代码和 ISO 国家/地区代码之间存在一对一的映射。这是不真实的。例如,美国和加拿大的国际拨号代码都是“1”。
也许考虑重新构建应用程序的界面。允许用户选择与电话号码关联的国家/地区,但使用 http://countrycode.org/ 中的表格来订购最可能的国家/地区高层候选人?
You may not be able to query the country code programmatically via the standard API but you could include a table in your app. Such a table is easily found via Google (e.g. http://countrycode.org/).
Danger Will Robinson!: However, one should ask yourself what question you are trying to answer. Implicit in your question is that assumption that there is a one-to-one mapping between international dialling codes and ISO country codes. This is not true. For example, both the USA and Canada have the international dialling code '1'.
Perhaps think about re-structuring your app's interface. Allow the user to select a country to associate with the phone number but use the table from http://countrycode.org/ to order the most likely candidates at the top?
有同样的问题。最终我把所有的数据都放到了excel中,并读取了excel表。
实现如下:
使用下面的类来读取文件:
公共类国家代码{
私有 HashMap mCountryByName = new HashMap();
私有 HashMap mCountryByCode = new HashMap();;
私有 ArrayList mCountries = new ArrayList();
}
Had the same problem. Eventually I put all the data in excel and read the excel sheet.
Here is the implementation:
Use the following class to read the file:
public class CountryCodes {
private HashMap mCountryByName = new HashMap();
private HashMap mCountryByCode = new HashMap();;
private ArrayList mCountries = new ArrayList();
}
第 1 步
您可以在以下网址中获取
国家/地区呼叫代码
及其ISO名称
http://en.wikipedia.org/wiki/List_of_country_calling_codes
或
http://www.unc.edu/~rowlett/units/codes/country.htm
Step-2 您可以使用 java 获取该文件的页面源代码程序。您将获得 HTMl 格式的文件
第 3 步,您可以使用任何可用的解析器将这些 HTML 文件转换为 XML 格式。请参阅Java 中的开源 HTML 解析器
步骤 4通过电话号码您可以获得呼叫代码。例如,如果号码为“1-319-491-6338”,则呼叫代码为 1
步骤 5 将此呼叫代码与您从 XML 解析器获得的呼叫代码和国家/地区名称列表进行匹配。这样就可以得到iso国家
Step-1
You can get
country calling code
as well as itsISO name
in the following URLhttp://en.wikipedia.org/wiki/List_of_country_calling_codes
or
http://www.unc.edu/~rowlett/units/codes/country.htm
Step-2 You can get page source of that file using java program. You will get file in HTMl format
Step-3 you can convert those HTML files into XML format using any of available parsers. see Open Source HTML Parsers in Java
Step-4 Form the phone number you can get the calling code. Example if the number is "1-319-491-6338" then calling code is 1
Step-5 Match this calling code against the calling code and country name list that you have got from XML parser. In this way you can get iso country