使用Android获取电话国家代码

我想知道如何从我的android应用程序中获取设备/用户国家代码我知道有一个名为TelephonyManager的库,但我找不到国家代码function

为了使自己清楚:我有一个活动,要求用户input他的电话号码,我想国家代码从设备(从他的载体?)如果我打开我的应用程序在纽约我会得到+212作为国家代码和一个文本框input我自己的号码

1-将此数组添加到Strings.xml文件

<string-array name="CountryCodes" > <item>93,AF</item> <item>355,AL</item> <item>213,DZ</item> <item>376,AD</item> <item>244,AO</item> <item>672,AQ</item> <item>54,AR</item> <item>374,AM</item> <item>297,AW</item> <item>61,AU</item> <item>43,AT</item> <item>994,AZ</item> <item>973,BH</item> <item>880,BD</item> <item>375,BY</item> <item>32,BE</item> <item>501,BZ</item> <item>229,BJ</item> <item>975,BT</item> <item>591,BO</item> <item>387,BA</item> <item>267,BW</item> <item>55,BR</item> <item>673,BN</item> <item>359,BG</item> <item>226,BF</item> <item>95,MM</item> <item>257,BI</item> <item>855,KH</item> <item>237,CM</item> <item>1,CA</item> <item>238,CV</item> <item>236,CF</item> <item>235,TD</item> <item>56,CL</item> <item>86,CN</item> <item>61,CX</item> <item>61,CC</item> <item>57,CO</item> <item>269,KM</item> <item>242,CG</item> <item>243,CD</item> <item>682,CK</item> <item>506,CR</item> <item>385,HR</item> <item>53,CU</item> <item>357,CY</item> <item>420,CZ</item> <item>45,DK</item> <item>253,DJ</item> <item>670,TL</item> <item>593,EC</item> <item>20,EG</item> <item>503,SV</item> <item>240,GQ</item> <item>291,ER</item> <item>372,EE</item> <item>251,ET</item> <item>500,FK</item> <item>298,FO</item> <item>679,FJ</item> <item>358,FI</item> <item>33,FR</item> <item>689,PF</item> <item>241,GA</item> <item>220,GM</item> <item>995,GE</item> <item>49,DE</item> <item>233,GH</item> <item>350,GI</item> <item>30,GR</item> <item>299,GL</item> <item>502,GT</item> <item>224,GN</item> <item>245,GW</item> <item>592,GY</item> <item>509,HT</item> <item>504,HN</item> <item>852,HK</item> <item>36,HU</item> <item>91,IN</item> <item>62,ID</item> <item>98,IR</item> <item>964,IQ</item> <item>353,IE</item> <item>44,IM</item> <item>972,IL</item> <item>39,IT</item> <item>225,CI</item> <item>81,JP</item> <item>962,JO</item> <item>7,KZ</item> <item>254,KE</item> <item>686,KI</item> <item>965,KW</item> <item>996,KG</item> <item>856,LA</item> <item>371,LV</item> <item>961,LB</item> <item>266,LS</item> <item>231,LR</item> <item>218,LY</item> <item>423,LI</item> <item>370,LT</item> <item>352,LU</item> <item>853,MO</item> <item>389,MK</item> <item>261,MG</item> <item>265,MW</item> <item>60,MY</item> <item>960,MV</item> <item>223,ML</item> <item>356,MT</item> <item>692,MH</item> <item>222,MR</item> <item>230,MU</item> <item>262,YT</item> <item>52,MX</item> <item>691,FM</item> <item>373,MD</item> <item>377,MC</item> <item>976,MN</item> <item>382,ME</item> <item>212,MA</item> <item>258,MZ</item> <item>264,NA</item> <item>674,NR</item> <item>977,NP</item> <item>31,NL</item> <item>599,AN</item> <item>687,NC</item> <item>64,NZ</item> <item>505,NI</item> <item>227,NE</item> <item>234,NG</item> <item>683,NU</item> <item>850,KP</item> <item>47,NO</item> <item>968,OM</item> <item>92,PK</item> <item>680,PW</item> <item>507,PA</item> <item>675,PG</item> <item>595,PY</item> <item>51,PE</item> <item>63,PH</item> <item>870,PN</item> <item>48,PL</item> <item>351,PT</item> <item>1,PR</item> <item>974,QA</item> <item>40,RO</item> <item>7,RU</item> <item>250,RW</item> <item>590,BL</item> <item>685,WS</item> <item>378,SM</item> <item>239,ST</item> <item>966,SA</item> <item>221,SN</item> <item>381,RS</item> <item>248,SC</item> <item>232,SL</item> <item>65,SG</item> <item>421,SK</item> <item>386,SI</item> <item>677,SB</item> <item>252,SO</item> <item>27,ZA</item> <item>82,KR</item> <item>34,ES</item> <item>94,LK</item> <item>290,SH</item> <item>508,PM</item> <item>249,SD</item> <item>597,SR</item> <item>268,SZ</item> <item>46,SE</item> <item>41,CH</item> <item>963,SY</item> <item>886,TW</item> <item>992,TJ</item> <item>255,TZ</item> <item>66,TH</item> <item>228,TG</item> <item>690,TK</item> <item>676,TO</item> <item>216,TN</item> <item>90,TR</item> <item>993,TM</item> <item>688,TV</item> <item>971,AE</item> <item>256,UG</item> <item>44,GB</item> <item>380,UA</item> <item>598,UY</item> <item>1,US</item> <item>998,UZ</item> <item>678,VU</item> <item>39,VA</item> <item>58,VE</item> <item>84,VN</item> <item>681,WF</item> <item>967,YE</item> <item>260,ZM</item> <item>263,ZW</item> </string-array> 

2-CopyThisfunction到您的课堂

 public String GetCountryZipCode(){ String CountryID=""; String CountryZipCode=""; TelephonyManager manager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE); //getNetworkCountryIso CountryID= manager.getSimCountryIso().toUpperCase(); String[] rl=this.getResources().getStringArray(R.array.CountryCodes); for(int i=0;i<rl.length;i++){ String[] g=rl[i].split(","); if(g[1].trim().equals(CountryID.trim())){ CountryZipCode=g[0]; break; } } return CountryZipCode; } 

3-Call函数GetCountryZipCode()

4写谢谢Wais。

对不起,以恢复死亡线程,但我花了一段时间试图解决这个问题,所以希望我的回应会帮助别人。

原始问题中的例子令人困惑,你说你想追加国家代码,但以纽约地区代码212为例; 这不是一个国家代码,它是一个地方代码。 美国的国家代码是1.所以你最终的号码看起来像“1-212-xxx-xxxx”。 下面的答案解释了如何确定国家代码,而不是本地区号。

要获取自己号码的国家代码,请使用getSimCountryIso()。 不要使用getNetworkCountryIso(); getNetworkCountryIso将返回您当前所在的国家,但如果您正在旅行,将返回错误的国家。 例如,如果您是来自阿尔巴尼亚,但在纽约旅行,getSimCountryIso将返回“al”,但getNetworkCountryIso将返回“我们”。 显然,如果你使用美国的国家代码,你的号码是错误的。

现在你有两个字符的国家代码; 例如加拿大将是“ca”。 然后,您可以将其用作下表(从http://www.sikosoft.com/item/international_telephone_country_codes_in_xml_format_using_iso_639-1获取; )的查找键。

由于国家/地区代码不会更改,因此可以在应用程序中对查找表进行硬编码。 不幸的是,似乎没有直接的方式来获取用户目前的国家代码。

 <?xml version='1.0' encoding='UTF-8'?> <icc> <AF>93</AF> <AL>355</AL> <DZ>213</DZ> <AD>376</AD> <AO>244</AO> <AQ>672</AQ> <AR>54</AR> <AM>374</AM> <AW>297</AW> <AU>61</AU> <AT>43</AT> <AZ>994</AZ> <BH>973</BH> <BD>880</BD> <BY>375</BY> <BE>32</BE> <BZ>501</BZ> <BJ>229</BJ> <BT>975</BT> <BO>591</BO> <BA>387</BA> <BW>267</BW> <BR>55</BR> <BN>673</BN> <BG>359</BG> <BF>226</BF> <MM>95</MM> <BI>257</BI> <KH>855</KH> <CM>237</CM> <CA>1</CA> <CV>238</CV> <CF>236</CF> <TD>235</TD> <CL>56</CL> <CN>86</CN> <CX>61</CX> <CC>61</CC> <CO>57</CO> <KM>269</KM> <CG>242</CG> <CD>243</CD> <CK>682</CK> <CR>506</CR> <HR>385</HR> <CU>53</CU> <CY>357</CY> <CZ>420</CZ> <DK>45</DK> <DJ>253</DJ> <TL>670</TL> <EC>593</EC> <EG>20</EG> <SV>503</SV> <GQ>240</GQ> <ER>291</ER> <EE>372</EE> <ET>251</ET> <FK>500</FK> <FO>298</FO> <FJ>679</FJ> <FI>358</FI> <FR>33</FR> <PF>689</PF> <GA>241</GA> <GM>220</GM> <GE>995</GE> <DE>49</DE> <GH>233</GH> <GI>350</GI> <GR>30</GR> <GL>299</GL> <GT>502</GT> <GN>224</GN> <GW>245</GW> <GY>592</GY> <HT>509</HT> <HN>504</HN> <HK>852</HK> <HU>36</HU> <IN>91</IN> <ID>62</ID> <IR>98</IR> <IQ>964</IQ> <IE>353</IE> <IM>44</IM> <IL>972</IL> <IT>39</IT> <CI>225</CI> <JP>81</JP> <JO>962</JO> <KZ>7</KZ> <KE>254</KE> <KI>686</KI> <KW>965</KW> <KG>996</KG> <LA>856</LA> <LV>371</LV> <LB>961</LB> <LS>266</LS> <LR>231</LR> <LY>218</LY> <LI>423</LI> <LT>370</LT> <LU>352</LU> <MO>853</MO> <MK>389</MK> <MG>261</MG> <MW>265</MW> <MY>60</MY> <MV>960</MV> <ML>223</ML> <MT>356</MT> <MH>692</MH> <MR>222</MR> <MU>230</MU> <YT>262</YT> <MX>52</MX> <FM>691</FM> <MD>373</MD> <MC>377</MC> <MN>976</MN> <ME>382</ME> <MA>212</MA> <MZ>258</MZ> <NA>264</NA> <NR>674</NR> <NP>977</NP> <NL>31</NL> <AN>599</AN> <NC>687</NC> <NZ>64</NZ> <NI>505</NI> <NE>227</NE> <NG>234</NG> <NU>683</NU> <KP>850</KP> <NO>47</NO> <OM>968</OM> <PK>92</PK> <PW>680</PW> <PA>507</PA> <PG>675</PG> <PY>595</PY> <PE>51</PE> <PH>63</PH> <PN>870</PN> <PL>48</PL> <PT>351</PT> <PR>1</PR> <QA>974</QA> <RO>40</RO> <RU>7</RU> <RW>250</RW> <BL>590</BL> <WS>685</WS> <SM>378</SM> <ST>239</ST> <SA>966</SA> <SN>221</SN> <RS>381</RS> <SC>248</SC> <SL>232</SL> <SG>65</SG> <SK>421</SK> <SI>386</SI> <SB>677</SB> <SO>252</SO> <ZA>27</ZA> <KR>82</KR> <ES>34</ES> <LK>94</LK> <SH>290</SH> <PM>508</PM> <SD>249</SD> <SR>597</SR> <SZ>268</SZ> <SE>46</SE> <CH>41</CH> <SY>963</SY> <TW>886</TW> <TJ>992</TJ> <TZ>255</TZ> <TH>66</TH> <TG>228</TG> <TK>690</TK> <TO>676</TO> <TN>216</TN> <TR>90</TR> <TM>993</TM> <TV>688</TV> <AE>971</AE> <UG>256</UG> <GB>44</GB> <UA>380</UA> <UY>598</UY> <US>1</US> <UZ>998</UZ> <VU>678</VU> <VA>39</VA> <VE>58</VE> <VN>84</VN> <WF>681</WF> <YE>967</YE> <ZM>260</ZM> <ZW>263</ZW> </icc> 

这个问题帮了我很大的忙,我想给你们提供一个从电话代码提供国家代码的类,反之亦然。

用法:

  • CountryCodes.getCode(String country)从国家代码返回国际代码
  • CountryCodes.getCountry(String code)从国际代码返回国家标志

这个类的一个实例可以作为视图的适配器提供,例如在Activity onCreate中:

 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_layout); // your layout xml Spinner s = ( Spinner )findViewById( R.id.spinner1 ); // id of your spinner CountryCodes cc = new CountryCodes( this ); s.setAdapter( cc ); TelephonyManager man = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); int index = CountryCodes.getIndex( man.getSimCountryIso() ); if( index > -1 ) { s.setSelection( index ); } } 

以下是class级代码

CountryCodes.java

 import java.util.ArrayList; import java.util.Locale; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; /** * Call getCountry( String code ) to get matching country sign. * Call getCode( String country ) to get matching phone code. * It has been extended from BaseAdapter in order to make it compatible with Spinner, * ListView and so on (just instance it and give it as adapter). * * This class is provided AS IS without any warranty. * * @author Niki Romagnoli * */ public class CountryCodes extends BaseAdapter{ private static final String[] m_Countries = { "AD", "AE", "AF", "AL", "AM", "AN", "AO", "AQ", "AR", "AT", "AU", "AW", "AZ", "BA", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BL", "BN", "BO", "BR", "BT", "BW", "BY", "BZ", "CA", "CC", "CD", "CF", "CG", "CH", "CI", "CK", "CL", "CM", "CN", "CO", "CR", "CU", "CV", "CX", "CY", "CZ", "DE", "DJ", "DK", "DZ", "EC", "EE", "EG", "ER", "ES", "ET", "FI", "FJ", "FK", "FM", "FO", "FR", "GA", "GB", "GE", "GH", "GI", "GL", "GM", "GN", "GQ", "GR", "GT", "GW", "GY", "HK", "HN", "HR", "HT", "HU", "ID", "IE", "IL", "IM", "IN", "IQ", "IR", "IT", "JO", "JP", "KE", "KG", "KH", "KI", "KM", "KP", "KR", "KW", "KZ", "LA", "LB", "LI", "LK", "LR", "LS", "LT", "LU", "LV", "LY", "MA", "MC", "MD", "ME", "MG", "MH", "MK", "ML", "MM", "MN", "MO", "MR", "MT", "MU", "MV", "MW", "MX", "MY", "MZ", "NA", "NC", "NE", "NG", "NI", "NL", "NO", "NP", "NR", "NU", "NZ", "OM", "PA", "PE", "PF", "PG", "PH", "PK", "PL", "PM", "PN", "PR", "PT", "PW", "PY", "QA", "RO", "RS", "RU", "RW", "SA", "SB", "SC", "SD", "SE", "SG", "SH", "SI", "SK", "SL", "SM", "SN", "SO", "SR", "ST", "SV", "SY", "SZ", "TD", "TG", "TH", "TJ", "TK", "TL", "TM", "TN", "TO", "TR", "TV", "TW", "TZ", "UA", "UG", "US", "UY", "UZ", "VA", "VE", "VN", "VU", "WF", "WS", "YE", "YT", "ZA", "ZM", "ZW" }; private static final String[] m_Codes = { "376", "971", "93", "355", "374", "599", "244", "672", "54", "43", "61", "297", "994", "387", "880", "32", "226", "359", "973", "257", "229", "590", "673", "591", "55", "975", "267", "375", "501", "1", "61", "243", "236", "242", "41", "225", "682", "56", "237", "86", "57", "506", "53", "238", "61", "357", "420", "49", "253", "45", "213", "593", "372", "20", "291", "34", "251", "358", "679", "500", "691", "298", "33", "241", "44", "995", "233", "350", "299", "220", "224", "240", "30", "502", "245", "592", "852", "504", "385", "509", "36", "62", "353", "972", "44", "91", "964", "98", "39", "962", "81", "254", "996", "855", "686", "269", "850", "82", "965", "7", "856", "961", "423", "94", "231", "266", "370", "352", "371", "218", "212", "377", "373", "382", "261", "692", "389", "223", "95", "976", "853", "222", "356", "230", "960", "265", "52", "60", "258", "264", "687", "227", "234", "505", "31", "47", "977", "674", "683", "64", "968", "507", "51", "689", "675", "63", "92", "48", "508", "870", "1", "351", "680", "595", "974", "40", "381", "7", "250", "966", "677", "248", "249", "46", "65", "290", "386", "421", "232", "378", "221", "252", "597", "239", "503", "963", "268", "235", "228", "66", "992", "690", "670", "993", "216", "676", "90", "688", "886", "255", "380", "256", "1", "598", "998", "39", "58", "84", "678", "681", "685", "967", "262", "27", "260", "263" }; private Context m_Context; public CountryCodes( Context cxt ) { super(); m_Context = cxt; } /** * Get phone code from country sign. * * @param country: two-chars country sign to fetch ("US", "IT", "GB", ...) * @return string of matching phone code ("1", "39", "44", ...). null if none matches. */ public static String getCode( String country ) { int index = getIndex( country ); return index == -1? null: getCode(index); } /** * Get international code at provided index. * * @param index: array index * @return international code */ public static String getCode( int index ) { return m_Codes[index]; } /** * Get country signs from phone code. * More countries may match the same code. * * @param code: phone code to fetch ("1", "39", "44", ...) * @return list of uppercase country signs (["US","PR","CA"], ["IT","VA"], ["GB","IM"], ...) * Empty list if none matches. */ public static ArrayList<String> getCountry( String code ) { ArrayList<String> matches = new ArrayList<String>(); getCountry(code, matches); return matches; } /** * Memory cheap version of country fetching: uses user provided list as output which outside * could be recycled on desire. * * @param code: country sign to fetch * @param matches: list to fill with matches, used as output */ public static void getCountry( String code, ArrayList<String> matches ) { matches.clear(); for( int i=0; i<m_Codes.length; ++i ) if( m_Codes[i].equals( code ) ) matches.add(getCountry(i)); } /** * Returns country sign at specified index of internal array. * * @param index: index to fetch * @return country sign */ public static String getCountry( int index ) { return m_Countries[index]; } /** * Looks for country sign array index. * * @param country: country sign to search * @return array index. -1 if none matches. */ public static int getIndex( String country ) { String search = country.toUpperCase(Locale.getDefault()); for( int i=0; i<m_Countries.length; ++i ) if( m_Countries[i].equals( search ) ) return i; return -1; } @Override public int getCount() { return m_Codes.length; } @Override public Object getItem(int index) { return m_Countries[index]; } @Override public long getItemId(int index) { return index; } @Override public View getView(int index, View recycleView, ViewGroup viewGroup) { TextView view; if( recycleView == null ) { view = new TextView(m_Context); view.setPadding(30, 10, 10, 10); } else { view = (TextView)recycleView; } view.setText(m_Countries[index]); return view; } } 

我使用它,似乎工作,但是,你知道,死亡和税收是确定的。 随意定制它,并build议修复。

HTH

在这种情况下,这将看起来像:

  /** * network iso code: referred and listed at: * http://en.wikipedia.org/wiki/List_of_mobile_country_codes */ TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); Constants.DEVICE_COUNTRY = manager.getNetworkCountryIso(); 

所以没有人需要仔细查看并重新检查(context是一个有效的Context对象的局部variables)。 Constants.DEVICE_COUNTRY是typesstring:)(只是一个variables)

使用compile 'com.googlecode.libphonenumber:libphonenumber:8.0.1'依赖。 这使得任务简单,并提供准确的结果,因为它是由谷歌。

在这里,你可以知道更多关于它。

我得到的输出是这样的:

在这里输入图像说明

在这里我使用微调。

1)我创build了一个自定义的Adapter CountryCodesAdapter:

 package com.amitupadhyay.getcountrycode; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.CheckedTextView; import android.widget.TextView; import com.google.i18n.phonenumbers.PhoneNumberUtil; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Locale; /** * Created by aupadhyay on 1/24/17. */ public class CountryCodesAdapter extends BaseAdapter { private final LayoutInflater mInflater; private final List<CountryCode> mData; private final int mViewId; private final int mDropdownViewId; private int mSelected; public static final class CountryCode implements Comparable<String> { public String regionCode; public int countryCode; public String regionName; @Override public int compareTo(String another) { return regionCode != null && another != null ? regionCode.compareTo(another) : 1; } @Override public boolean equals(Object o) { if (this == o) return true; if (o != null && o instanceof CountryCode) { CountryCode other = (CountryCode) o; return regionCode != null && regionCode.equals(other.regionCode); } return false; } @Override public String toString() { return regionCode; } } public CountryCodesAdapter(Context context, int viewId, int dropdownViewId) { this(context, new ArrayList<CountryCode>(), viewId, dropdownViewId); } public CountryCodesAdapter(Context context, List<CountryCode> data, int viewId, int dropdownViewId) { mInflater = LayoutInflater.from(context); mData = data; mViewId = viewId; mDropdownViewId = dropdownViewId; } public void add(CountryCode entry) { mData.add(entry); } public void add(String regionCode) { CountryCode cc = new CountryCode(); cc.regionCode = regionCode; cc.countryCode = PhoneNumberUtil.getInstance().getCountryCodeForRegion(regionCode); cc.regionName = getRegionDisplayName(regionCode, Locale.getDefault()); mData.add(cc); } public void clear() { mData.clear(); } public void sort(Comparator<? super CountryCode> comparator) { Collections.sort(mData, comparator); } @Override public int getCount() { return mData.size(); } @Override public Object getItem(int position) { return mData.get(position); } @Override public long getItemId(int position) { CountryCode e = mData.get(position); return (e != null) ? e.countryCode : -1; } public int getPositionForId(CountryCode cc) { return cc != null ? mData.indexOf(cc) : -1; } public void setSelected(int position) { mSelected = position; } @Override public View getDropDownView(int position, View convertView, ViewGroup parent) { CheckedTextView textView; View view; if (convertView == null) { view = mInflater.inflate(mDropdownViewId, parent, false); textView = (CheckedTextView) view.findViewById(android.R.id.text1); view.setTag(textView); } else { view = convertView; textView = (CheckedTextView) view.getTag(); } CountryCode e = mData.get(position); StringBuilder text = new StringBuilder(5) .append(e.regionName) .append(" (+") .append(e.countryCode) .append(')'); textView.setText(text); textView.setChecked((mSelected == position)); return view; } @Override public View getView(int position, View convertView, ViewGroup parent) { TextView textView; View view; if (convertView == null) { view = mInflater.inflate(mViewId, parent, false); textView = (TextView) view.findViewById(android.R.id.text1); view.setTag(textView); } else { view = convertView; textView = (TextView) view.getTag(); } CountryCode e = mData.get(position); StringBuilder text = new StringBuilder(3) .append('+') .append(e.countryCode) .append(" (") .append(e.regionName) .append(')'); textView.setText(text); return view; } /** Returns the localized region name for the given region code. */ public String getRegionDisplayName(String regionCode, Locale language) { return (regionCode == null || regionCode.equals("ZZ") || regionCode.equals(PhoneNumberUtil.REGION_CODE_FOR_NON_GEO_ENTITY)) ? "" : new Locale("", regionCode).getDisplayCountry(language); } } 

2)现在我的活动,我正在显示国家代码的微调的布局是:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical"> <include layout="@layout/toolbar_simple"/> <ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbars="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="vertical" android:padding="18dip" > <Spinner android:id="@+id/phone_cc" android:layout_width="match_parent" android:layout_height="wrap_content" android:spinnerMode="dialog"/> <EditText android:id="@+id/phone_number" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/hint_validation_number" android:inputType="phone" /> <Button android:id="@+id/button_validate" android:layout_width="match_parent" android:layout_height="fill_parent" android:text="@string/button_validate" /> </LinearLayout> </ScrollView> </LinearLayout> 

3)java活动代码如下:

 package com.amitupadhyay.getcountrycode; import android.annotation.SuppressLint; import android.content.Context; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.telephony.TelephonyManager; import android.view.View; import android.widget.AdapterView; import android.widget.Button; import android.widget.EditText; import android.widget.Spinner; import com.amitupadhyay.getcountrycode.CountryCodesAdapter.CountryCode; import com.google.i18n.phonenumbers.PhoneNumberUtil; import com.google.i18n.phonenumbers.Phonenumber; import java.util.Comparator; import java.util.HashSet; import java.util.Locale; import java.util.Set; public class NumberValidation extends AppCompatActivity { private Spinner mCountryCode; private EditText mPhone; private Button mValidateButton; /** * Compatibility method for {@link PhoneNumberUtil#getSupportedRegions()}. * This was introduced because crappy Honeycomb has an old version of * libphonenumber, therefore Dalvik will insist on we using it. * In case getSupportedRegions doesn't exist, getSupportedCountries will be * used. */ @SuppressWarnings("unchecked") private Set<String> getSupportedRegions(PhoneNumberUtil util) { try { return (Set<String>) util.getClass() .getMethod("getSupportedRegions") .invoke(util); } catch (NoSuchMethodException e) { try { return (Set<String>) util.getClass() .getMethod("getSupportedCountries") .invoke(util); } catch (Exception helpme) { // ignored } } catch (Exception e) { // ignored } return new HashSet<>(); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.number_validation); //setupToolbar(false); mCountryCode = (Spinner) findViewById(R.id.phone_cc); mPhone = (EditText) findViewById(R.id.phone_number); mValidateButton = (Button) findViewById(R.id.button_validate); // populate country codes final CountryCodesAdapter ccList = new CountryCodesAdapter(this, android.R.layout.simple_list_item_1, android.R.layout.simple_spinner_dropdown_item); PhoneNumberUtil util = PhoneNumberUtil.getInstance(); Set<String> ccSet = getSupportedRegions(util); for (String cc : ccSet) ccList.add(cc); ccList.sort(new Comparator<CountryCodesAdapter.CountryCode>() { public int compare(CountryCodesAdapter.CountryCode lhs, CountryCodesAdapter.CountryCode rhs) { return lhs.regionName.compareTo(rhs.regionName); } }); mCountryCode.setAdapter(ccList); mCountryCode.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { ccList.setSelected(position); } public void onNothingSelected(AdapterView<?> parent) { // TODO Auto-generated method stub } }); // FIXME this doesn't consider creation because of configuration change Phonenumber.PhoneNumber myNum = getMyNumber(this); if (myNum != null) { CountryCode cc = new CountryCode(); cc.regionCode = util.getRegionCodeForNumber(myNum); if (cc.regionCode == null) cc.regionCode = util.getRegionCodeForCountryCode(myNum.getCountryCode()); mCountryCode.setSelection(ccList.getPositionForId(cc)); mPhone.setText(String.valueOf(myNum.getNationalNumber())); } else { final TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); final String regionCode = tm.getSimCountryIso().toUpperCase(Locale.US); CountryCode cc = new CountryCode(); cc.regionCode = regionCode; cc.countryCode = util.getCountryCodeForRegion(regionCode); mCountryCode.setSelection(ccList.getPositionForId(cc)); } } /** Returns the (parsed) number stored in this device SIM card. */ @SuppressLint("HardwareIds") public Phonenumber.PhoneNumber getMyNumber(Context context) { try { final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); final String regionCode = tm.getSimCountryIso().toUpperCase(Locale.US); return PhoneNumberUtil.getInstance().parse(tm.getLine1Number(), regionCode); } catch (Exception e) { return null; } } } 

几个额外的function:

a)您的国家将在微调框中被自动选中。

为了实现我用过这个:

  Phonenumber.PhoneNumber myNum = getMyNumber(this); if (myNum != null) { CountryCode cc = new CountryCode(); cc.regionCode = util.getRegionCodeForNumber(myNum); if (cc.regionCode == null) cc.regionCode = util.getRegionCodeForCountryCode(myNum.getCountryCode()); mCountryCode.setSelection(ccList.getPositionForId(cc)); mPhone.setText(String.valueOf(myNum.getNationalNumber())); } else { final TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); final String regionCode = tm.getSimCountryIso().toUpperCase(Locale.US); CountryCode cc = new CountryCode(); cc.regionCode = regionCode; cc.countryCode = util.getCountryCodeForRegion(regionCode); mCountryCode.setSelection(ccList.getPositionForId(cc)); } 

我认为这种方法比这里列出的其他方法好一点。 所以我发布了这个。 希望有人可能会发现它有帮助:-)

GitHub上的这个项目就在这里

如果您碰巧使用Google的libphonenumber ,并且如果您使用电话号码,则很可能您会这样做,您可以使用其内部数据描述相同的内容。 这不是公开的,所以你必须使用reflection来达到它:

 Map<Integer, List<String>> CountryCodeToRegionCodeMap_getCountryCodeToRegionCodeMap() { try { Method method = CountryCodeToRegionCodeMap.class.getDeclaredMethod("getCountryCodeToRegionCodeMap"); method.setAccessible(true); return (Map<Integer, List<String>>) method.invoke(null); } catch (Exception e) { Log.e(..., e.toString()); return null; } } 

然后,您可以search或处理该地图以获取所需的数据。

将这些添加到string.xml享受:)

 <string-array name="country_code"> <item></item> <item> +93 </item> <item> +355 </item> <item> +213 </item> <item> +1684 </item> <item> +376 </item> <item> +244 </item> <item> +1264 </item> <item> +672 </item> <item> +1268 </item> <item> +54 </item> <item> +374 </item> <item> +297 </item> <item> +61 </item> <item> +43 </item> <item> +994 </item> <item> +1242 </item> <item> +973 </item> <item> +880 </item> <item> +1246 </item> <item> +375 </item> <item> +32 </item> <item> +501 </item> <item> +229 </item> <item> +1441 </item> <item> +975 </item> <item> +591 </item> <item> +387 </item> <item> +267 </item> <item> +55 </item> <item> +246 </item> <item> +1284 </item> <item> +673 </item> <item> +359 </item> <item> +226 </item> <item> +257 </item> <item> +855 </item> <item> +237 </item> <item> +1 </item> <item> +238 </item> <item> +1345 </item> <item> +236 </item> <item> +235 </item> <item> +56 </item> <item> +86 </item> <item> +61 </item> <item> +61 </item> <item> +57 </item> <item> +269 </item> <item> +682 </item> <item> +506 </item> <item> +385 </item> <item> +53 </item> <item> +599 </item> <item> +357 </item> <item> +420 </item> <item> +243 </item> <item> +45 </item> <item> +253 </item> <item> +1767 </item> <item> +1809 </item> <item> +670 </item> <item> +593 </item> <item> +20 </item> <item> +503 </item> <item> +240 </item> <item> +291 </item> <item> +372 </item> <item> +251 </item> <item> +500 </item> <item> +298 </item> <item> +679 </item> <item> +358 </item> <item> +33 </item> <item> +689 </item> <item> +241 </item> <item> +220 </item> <item> +995 </item> <item> +49 </item> <item> +233 </item> <item> +350 </item> <item> +30 </item> <item> +299 </item> <item> +1473 </item> <item> +1671 </item> <item> +502 </item> <item> +441481 </item> <item> +224 </item> <item> +245 </item> <item> +592 </item> <item> +509 </item> <item> +504 </item> <item> +852 </item> <item> +36 </item> <item> +354 </item> <item> +91 </item> <item> +62 </item> <item> +98 </item> <item> +964 </item> <item> +53 </item> <item> +441624 </item> <item> +972 </item> <item> +39 </item> <item> +225 </item> <item> +1876 </item> <item> +81 </item> <item> +441534 </item> <item> +962 </item> <item> +7 </item> <item> +254 </item> <item> +686 </item> <item> +383 </item> <item> +965 </item> <item> +996 </item> <item> +856 </item> <item> +371 </item> <item> +961 </item> <item> +266 </item> <item> +231 </item> <item> +218 </item> <item> +423 </item> <item> +370 </item> <item> +352 </item> <item> +853 </item> <item> +389 </item> <item> +261 </item> <item> +265 </item> <item> +60 </item> <item> +960 </item> <item> +223 </item> <item> +356 </item> <item> +692 </item> <item> +222 </item> <item> +230 </item> <item> +262 </item> <item> +52 </item> <item> +691 </item> <item> +373 </item> <item> +377 </item> <item> +976 </item> <item> +382 </item> <item> +1664 </item> <item> +212 </item> <item> +258 </item> <item> +95 </item> <item> +264 </item> <item> +674 </item> <item> +977 </item> <item> +31 </item> <item> +599 </item> <item> +687 </item> <item> +64 </item> <item> +505 </item> <item> +227 </item> <item> +234 </item> <item> +683 </item> <item> +850 </item> <item> +1670 </item> <item> +47 </item> <item> +968 </item> <item> +92 </item> <item> +680 </item> <item> +970 </item> <item> +507 </item> <item> +675 </item> <item> +595 </item> <item> +51 </item> <item> +63 </item> <item> +64 </item> <item> +48 </item> <item> +351 </item> <item> +1787 </item> <item> +974 </item> <item> +242 </item> <item> +262 </item> <item> +40 </item> <item> +7 </item> <item> +250 </item> <item> +590 </item> <item> +290 </item> <item> +1869 </item> <item> +1758 </item> <item> +590 </item> <item> +508 </item> <item> +1784 </item> <item> +685 </item> <item> +378 </item> <item> +239 </item> <item> +966 </item> <item> +221 </item> <item> +381 </item> <item> +248 </item> <item> +232 </item> <item> +65 </item> <item> +1721 </item> <item> +421 </item> <item> +386 </item> <item> +677 </item> <item> +252 </item> <item> +27 </item> <item> +82 </item> <item> +211 </item> <item> +34 </item> <item> +94 </item> <item> +249 </item> <item> +597 </item> <item> +47 </item> <item> +268 </item> <item> +46 </item> <item> +41 </item> <item> +963 </item> <item> +886 </item> <item> +992 </item> <item> +255 </item> <item> +66 </item> <item> +228 </item> <item> +690 </item> <item> +676 </item> <item> +1868 </item> <item> +216 </item> <item> +90 </item> <item> +993 </item> <item> +1649 </item> <item> +688 </item> <item> +1340 </item> <item> +256 </item> <item> +380 </item> <item> +971 </item> <item> +44 </item> <item> +1 </item> <item> +598 </item> <item> +998 </item> <item> +678 </item> <item> +379 </item> <item> +58 </item> <item> +84 </item> <item> +681 </item> <item> +212 </item> <item> 967 </item> <item> 260 </item> <item> 263 </item> </string-array> <string-array name="country_arrays"> <item>Code</item> <item>Afghanistan</item> <item>Albania</item> <item>Algeria</item> <item>American Samoa</item> <item>Andorra</item> <item>Angola</item> <item>Anguilla</item> <item>Antarctica</item> <item>Antigua and Barbuda</item> <item>Argentina</item> <item>Armenia</item> <item>Aruba</item> <item>Australia</item> <item>Austria</item> <item>Azerbaijan</item> <item>Bahrain</item> <item>Bangladesh</item> <item>Barbados</item> <item>Belarus</item> <item>Belgium</item> <item>Belize</item> <item>Benin</item> <item>Bermuda</item> <item>Bhutan</item> <item>Bolivia</item> <item>Bosnia and Herzegovina</item> <item>Botswana</item> <item>Bouvet Island</item> <item>Brazil</item> <item>British Indian Ocean Territory</item> <item>British Virgin Islands</item> <item>Brunei</item> <item>Bulgaria</item> <item>Burkina Faso</item> <item>Burundi</item> <item>Cambodia</item> <item>Cameroon</item> <item>Canada</item> <item>Cape Verde</item> <item>Cayman Islands</item> <item>Central African Republic</item> <item>Chad</item> <item>Chile</item> <item>China</item> <item>Christmas Island</item> <item>Cocos (Keeling) Islands</item> <item>Colombia</item> <item>Comoros</item> <item>Congo</item> <item>Cook Islands</item> <item>Costa Rica</item> <item>Cote d\'Ivoire</item> <item>Croatia</item> <item>Cuba</item> <item>Cyprus</item> <item>Czech Republic</item> <item>Democratic Republic of the Congo</item> <item>Denmark</item> <item>Djibouti</item> <item>Dominica</item> <item>Dominican Republic</item> <item>East Timor</item> <item>Ecuador</item> <item>Egypt</item> <item>El Salvador</item> <item>Equatorial Guinea</item> <item>Eritrea</item> <item>Estonia</item> <item>Ethiopia</item> <item>Faeroe Islands</item> <item>Falkland Islands</item> <item>Fiji</item> <item>Finland</item> <item>Former Yugoslav Republic of Macedonia</item> <item>France</item> <item>French Guiana</item> <item>French Polynesia</item> <item>French Southern Territories</item> <item>Gabon</item> <item>Georgia</item> <item>Germany</item> <item>Ghana</item> <item>Gibraltar</item> <item>Greece</item> <item>Greenland</item> <item>Grenada</item> <item>Guadeloupe</item> <item>Guam</item> <item>Guatemala</item> <item>Guinea</item> <item>Guinea-Bissau</item> <item>Guyana</item> <item>Haiti</item> <item>Heard Island and McDonald Islands</item> <item>Honduras</item> <item>Hong Kong</item> <item>Hungary</item> <item>Iceland</item> <item>India</item> <item>Indonesia</item> <item>Iran</item> <item>Iraq</item> <item>Ireland</item> <item>Israel</item> <item>Italy</item> <item>Jamaica</item> <item>Japan</item> <item>Jordan</item> <item>Kazakhstan</item> <item>Kenya</item> <item>Kiribati</item> <item>Kuwait</item> <item>Kyrgyzstan</item> <item>Laos</item> <item>Latvia</item> <item>Lebanon</item> <item>Lesotho</item> <item>Liberia</item> <item>Libya</item> <item>Liechtenstein</item> <item>Lithuania</item> <item>Luxembourg</item> <item>Macau</item> <item>Madagascar</item> <item>Malawi</item> <item>Malaysia</item> <item>Maldives</item> <item>Mali</item> <item>Malta</item> <item>Marshall Islands</item> <item>Martinique</item> <item>Mauritania</item> <item>Mauritius</item> <item>Mayotte</item> <item>Mexico</item> <item>Micronesia</item> <item>Moldova</item> <item>Monaco</item> <item>Mongolia</item> <item>Montenegro</item> <item>Montserrat</item> <item>Morocco</item> <item>Mozambique</item> <item>Myanmar</item> <item>Namibia</item> <item>Nauru</item> <item>Nepal</item> <item>Netherlands</item> <item>Netherlands Antilles</item> <item>New Caledonia</item> <item>New Zealand</item> <item>Nicaragua</item> <item>Niger</item> <item>Nigeria</item> <item>Niue</item> <item>Norfolk Island</item> <item>North Korea</item> <item>Northern Marianas</item> <item>Norway</item> <item>Oman</item> <item>Pakistan</item> <item>Palau</item> <item>Panama</item> <item>Papua New Guinea</item> <item>Paraguay</item> <item>Peru</item> <item>Philippines</item> <item>Pitcairn Islands</item> <item>Poland</item> <item>Portugal</item> <item>Puerto Rico</item> <item>Qatar</item> <item>Reunion</item> <item>Romania</item> <item>Russia</item> <item>Rwanda</item> <item>Sqo Tome and Principe</item> <item>Saint Helena</item> <item>Saint Kitts and Nevis</item> <item>Saint Lucia</item> <item>Saint Pierre and Miquelon</item> <item>Saint Vincent and the Grenadines</item> <item>Samoa</item> <item>San Marino</item> <item>Saudi Arabia</item> <item>Senegal</item> <item>Serbia</item> <item>Seychelles</item> <item>Sierra Leone</item> <item>Singapore</item> <item>Slovakia</item> <item>Slovenia</item> <item>Solomon Islands</item> <item>Somalia</item> <item>South Africa</item> <item>South Georgia and the South Sandwich Islands</item> <item>South Korea</item> <item>South Sudan</item> <item>Spain</item> <item>Sri Lanka</item> <item>Sudan</item> <item>Suriname</item> <item>Svalbard and Jan Mayen</item> <item>Swaziland</item> <item>Sweden</item> <item>Switzerland</item> <item>Syria</item> <item>Taiwan</item> <item>Tajikistan</item> <item>Tanzania</item> <item>Thailand</item> <item>The Bahamas</item> <item>The Gambia</item> <item>Togo</item> <item>Tokelau</item> <item>Tonga</item> <item>Trinidad and Tobago</item> <item>Tunisia</item> <item>Turkey</item> <item>Turkmenistan</item> <item>Turks and Caicos Islands</item> <item>Tuvalu</item> <item>Virgin Islands</item> <item>Uganda</item> <item>Ukraine</item> <item>United Arab Emirates</item> <item>United Kingdom</item> <item>United States</item> <item>United States Minor Outlying Islands</item> <item>Uruguay</item> <item>Uzbekistan</item> <item>Vanuatu</item> <item>Vatican City</item> <item>Venezuela</item> <item>Vietnam</item> <item>Wallis and Futuna</item> <item>Western Sahara</item> <item>Yemen</item> <item>Yugoslavia</item> <item>Zambia</item> <item>Zimbabwe</item> </string-array> 

在类TelephonyManager中,我find一个方法public String getNetworkCountryIso () ,这可能是你想要的。 此外,还有一些其他方法可以返回有关手机的更多信息。 您可以阅读http://en.wikipedia.org/wiki/List_of_mobile_country_codes查看哪个国家由MCC代表。;

我知道这太晚了。 但我有一个连击解决scheme。 由于使用String列表并循环执行,可能会降低速度。 使用Hashmap将是一个连击解决scheme。 我为它写了一个PhoneUtil类。 这是代码(我只有20个左右的国家,但你可以添加更多)

 import android.content.Context; import android.telephony.TelephonyManager; import java.util.HashMap; /** * Created by Dell on 1/19/2017. */ public class PhoneUtil { public static String getCountryCode(Context context) { TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); if (manager != null) { return getCountryCodeFromMap(manager.getSimCountryIso().toUpperCase()); } else { return null; } } private static String getCountryCodeFromMap(String countryCode) { HashMap<String, String> countryCodeMap = new HashMap<>(); countryCodeMap.put("TJ", "+992"); countryCodeMap.put("TZ", "+255"); countryCodeMap.put("TH", "+66"); countryCodeMap.put("TG", "+228"); countryCodeMap.put("TK", "+690"); countryCodeMap.put("TO", "+676"); countryCodeMap.put("TN", "+216"); countryCodeMap.put("TR", "+90"); countryCodeMap.put("TM", "+993"); countryCodeMap.put("TV", "+688"); countryCodeMap.put("AE", "+971"); countryCodeMap.put("UG", "+256"); countryCodeMap.put("GB", "+44"); countryCodeMap.put("UA", "+380"); countryCodeMap.put("UY", "+598"); countryCodeMap.put("US", "+1"); countryCodeMap.put("UZ", "+998"); countryCodeMap.put("VU", "+678"); countryCodeMap.put("VA", "+39"); countryCodeMap.put("VE", "+58"); countryCodeMap.put("VN", "+84"); countryCodeMap.put("WF", "+681"); countryCodeMap.put("YE", "+967"); countryCodeMap.put("ZM", "+260"); countryCodeMap.put("ZW", "+263"); countryCodeMap.put( "MY","+60"); countryCodeMap.put("PK","+92"); return countryCodeMap.get(countryCode); } }