2012-12-16 30 views
1

我現在可以獲得國家代碼,但我需要國家的全名。如何從代碼中獲取國家的全名?

我該怎麼辦?

public static String getCountry(Context c) { 
     TelephonyManager manager = (TelephonyManager) c 
       .getSystemService(Context.TELEPHONY_SERVICE); 

     return manager.getNetworkCountryIso().toUpperCase(); 
    } 

回答

5

首先寫這個代碼,並把這裏的國家代碼和它們對應的國家名稱。

HashMap countryLookupMap = null; 
    countryLookupMap = new HashMap(); 

    countryLookupMap.put("AD","Andorra"); 
    countryLookupMap.put("AE","United Arab Emirates"); 
    countryLookupMap.put("AF","Afghanistan"); 
    countryLookupMap.put("AG","Antigua and Barbuda"); 
    countryLookupMap.put("AI","Anguilla"); 
    countryLookupMap.put("AL","Albania"); 
    countryLookupMap.put("AM","Armenia"); 
    countryLookupMap.put("AN","Netherlands Antilles"); 
    countryLookupMap.put("AO","Angola"); 
    countryLookupMap.put("AQ","Antarctica"); 
    countryLookupMap.put("AR","Argentina"); 
    countryLookupMap.put("AS","American Samoa"); 
    countryLookupMap.put("AT","Austria"); 
    countryLookupMap.put("AU","Australia"); 
    countryLookupMap.put("AW","Aruba"); 
    countryLookupMap.put("AZ","Azerbaijan"); 
    countryLookupMap.put("BA","Bosnia and Herzegovina"); 
    countryLookupMap.put("BB","Barbados"); 
    countryLookupMap.put("BD","Bangladesh"); 
    countryLookupMap.put("BE","Belgium"); 
    countryLookupMap.put("BF","Burkina Faso"); 
    countryLookupMap.put("BG","Bulgaria"); 
    countryLookupMap.put("BH","Bahrain"); 
    countryLookupMap.put("BI","Burundi"); 
    countryLookupMap.put("BJ","Benin"); 
    countryLookupMap.put("BM","Bermuda"); 
    countryLookupMap.put("BN","Brunei"); 
    countryLookupMap.put("BO","Bolivia"); 
    countryLookupMap.put("BR","Brazil"); 
    countryLookupMap.put("BS","Bahamas"); 
    countryLookupMap.put("BT","Bhutan"); 
    countryLookupMap.put("BV","Bouvet Island"); 
    countryLookupMap.put("BW","Botswana"); 
    countryLookupMap.put("BY","Belarus"); 
    countryLookupMap.put("BZ","Belize"); 
    countryLookupMap.put("CA","Canada"); 
    countryLookupMap.put("CC","Cocos (Keeling) Islands"); 
    countryLookupMap.put("CD","Congo, The Democratic Republic of the"); 
    countryLookupMap.put("CF","Central African Republic"); 
    countryLookupMap.put("CG","Congo"); 
    countryLookupMap.put("CH","Switzerland"); 
    countryLookupMap.put("CI","Côte d?Ivoire"); 
    countryLookupMap.put("CK","Cook Islands"); 
    countryLookupMap.put("CL","Chile"); 
    countryLookupMap.put("CM","Cameroon"); 
    countryLookupMap.put("CN","China"); 
    countryLookupMap.put("CO","Colombia"); 
    countryLookupMap.put("CR","Costa Rica"); 
    countryLookupMap.put("CU","Cuba"); 
    countryLookupMap.put("CV","Cape Verde"); 
    countryLookupMap.put("CX","Christmas Island"); 
    countryLookupMap.put("CY","Cyprus"); 
    countryLookupMap.put("CZ","Czech Republic"); 
    countryLookupMap.put("DE","Germany"); 
    countryLookupMap.put("DJ","Djibouti"); 
    countryLookupMap.put("DK","Denmark"); 
    countryLookupMap.put("DM","Dominica"); 
    countryLookupMap.put("DO","Dominican Republic"); 
    countryLookupMap.put("DZ","Algeria"); 
    countryLookupMap.put("EC","Ecuador"); 
    countryLookupMap.put("EE","Estonia"); 
    countryLookupMap.put("EG","Egypt"); 
    countryLookupMap.put("EH","Western Sahara"); 
    countryLookupMap.put("ER","Eritrea"); 
    countryLookupMap.put("ES","Spain"); 
    countryLookupMap.put("ET","Ethiopia"); 
    countryLookupMap.put("FI","Finland"); 
    countryLookupMap.put("FJ","Fiji Islands"); 
    countryLookupMap.put("FK","Falkland Islands"); 
    countryLookupMap.put("FM","Micronesia, Federated States of"); 
    countryLookupMap.put("FO","Faroe Islands"); 
    countryLookupMap.put("FR","France"); 
    countryLookupMap.put("GA","Gabon"); 
    countryLookupMap.put("GB","United Kingdom"); 
    countryLookupMap.put("GD","Grenada"); 
    countryLookupMap.put("GE","Georgia"); 
    countryLookupMap.put("GF","French Guiana"); 
    countryLookupMap.put("GH","Ghana"); 
    countryLookupMap.put("GI","Gibraltar"); 
    countryLookupMap.put("GL","Greenland"); 
    countryLookupMap.put("GM","Gambia"); 
    countryLookupMap.put("GN","Guinea"); 
    countryLookupMap.put("GP","Guadeloupe"); 
    countryLookupMap.put("GQ","Equatorial Guinea"); 
    countryLookupMap.put("GR","Greece"); 
    countryLookupMap.put("GS","South Georgia and the South Sandwich Islands"); 
    countryLookupMap.put("GT","Guatemala"); 
    countryLookupMap.put("GU","Guam"); 
    countryLookupMap.put("GW","Guinea-Bissau"); 
    countryLookupMap.put("GY","Guyana"); 
    countryLookupMap.put("HK","Hong Kong"); 
    countryLookupMap.put("HM","Heard Island and McDonald Islands"); 
    countryLookupMap.put("HN","Honduras"); 
    countryLookupMap.put("HR","Croatia"); 
    countryLookupMap.put("HT","Haiti"); 
    countryLookupMap.put("HU","Hungary"); 
    countryLookupMap.put("ID","Indonesia"); 
    countryLookupMap.put("IE","Ireland"); 
    countryLookupMap.put("IL","Israel"); 
    countryLookupMap.put("IN","India"); 
    countryLookupMap.put("IO","British Indian Ocean Territory"); 
    countryLookupMap.put("IQ","Iraq"); 
    countryLookupMap.put("IR","Iran"); 
    countryLookupMap.put("IS","Iceland"); 
    countryLookupMap.put("IT","Italy"); 
    countryLookupMap.put("JM","Jamaica"); 
    countryLookupMap.put("JO","Jordan"); 
    countryLookupMap.put("JP","Japan"); 
    countryLookupMap.put("KE","Kenya"); 
    countryLookupMap.put("KG","Kyrgyzstan"); 
    countryLookupMap.put("KH","Cambodia"); 
    countryLookupMap.put("KI","Kiribati"); 
    countryLookupMap.put("KM","Comoros"); 
    countryLookupMap.put("KN","Saint Kitts and Nevis"); 
    countryLookupMap.put("KP","North Korea"); 
    countryLookupMap.put("KR","South Korea"); 
    countryLookupMap.put("KW","Kuwait"); 
    countryLookupMap.put("KY","Cayman Islands"); 
    countryLookupMap.put("KZ","Kazakstan"); 
    countryLookupMap.put("LA","Laos"); 
    countryLookupMap.put("LB","Lebanon"); 
    countryLookupMap.put("LC","Saint Lucia"); 
    countryLookupMap.put("LI","Liechtenstein"); 
    countryLookupMap.put("LK","Sri Lanka"); 
    countryLookupMap.put("LR","Liberia"); 
    countryLookupMap.put("LS","Lesotho"); 
    countryLookupMap.put("LT","Lithuania"); 
    countryLookupMap.put("LU","Luxembourg"); 
    countryLookupMap.put("LV","Latvia"); 
    countryLookupMap.put("LY","Libyan Arab Jamahiriya"); 
    countryLookupMap.put("MA","Morocco"); 
    countryLookupMap.put("MC","Monaco"); 
    countryLookupMap.put("MD","Moldova"); 
    countryLookupMap.put("MG","Madagascar"); 
    countryLookupMap.put("MH","Marshall Islands"); 
    countryLookupMap.put("MK","Macedonia"); 
    countryLookupMap.put("ML","Mali"); 
    countryLookupMap.put("MM","Myanmar"); 
    countryLookupMap.put("MN","Mongolia"); 
    countryLookupMap.put("MO","Macao"); 
    countryLookupMap.put("MP","Northern Mariana Islands"); 
    countryLookupMap.put("MQ","Martinique"); 
    countryLookupMap.put("MR","Mauritania"); 
    countryLookupMap.put("MS","Montserrat"); 
    countryLookupMap.put("MT","Malta"); 
    countryLookupMap.put("MU","Mauritius"); 
    countryLookupMap.put("MV","Maldives"); 
    countryLookupMap.put("MW","Malawi"); 
    countryLookupMap.put("MX","Mexico"); 
    countryLookupMap.put("MY","Malaysia"); 
    countryLookupMap.put("MZ","Mozambique"); 
    countryLookupMap.put("NA","Namibia"); 
    countryLookupMap.put("NC","New Caledonia"); 
    countryLookupMap.put("NE","Niger"); 
    countryLookupMap.put("NF","Norfolk Island"); 
    countryLookupMap.put("NG","Nigeria"); 
    countryLookupMap.put("NI","Nicaragua"); 
    countryLookupMap.put("NL","Netherlands"); 
    countryLookupMap.put("NO","Norway"); 
    countryLookupMap.put("NP","Nepal"); 
    countryLookupMap.put("NR","Nauru"); 
    countryLookupMap.put("NU","Niue"); 
    countryLookupMap.put("NZ","New Zealand"); 
    countryLookupMap.put("OM","Oman"); 
    countryLookupMap.put("PA","Panama"); 
    countryLookupMap.put("PE","Peru"); 
    countryLookupMap.put("PF","French Polynesia"); 
    countryLookupMap.put("PG","Papua New Guinea"); 
    countryLookupMap.put("PH","Philippines"); 
    countryLookupMap.put("PK","Pakistan"); 
    countryLookupMap.put("PL","Poland"); 
    countryLookupMap.put("PM","Saint Pierre and Miquelon"); 
    countryLookupMap.put("PN","Pitcairn"); 
    countryLookupMap.put("PR","Puerto Rico"); 
    countryLookupMap.put("PS","Palestine"); 
    countryLookupMap.put("PT","Portugal"); 
    countryLookupMap.put("PW","Palau"); 
    countryLookupMap.put("PY","Paraguay"); 
    countryLookupMap.put("QA","Qatar"); 
    countryLookupMap.put("RE","Réunion"); 
    countryLookupMap.put("RO","Romania"); 
    countryLookupMap.put("RU","Russian Federation"); 
    countryLookupMap.put("RW","Rwanda"); 
    countryLookupMap.put("SA","Saudi Arabia"); 
    countryLookupMap.put("SB","Solomon Islands"); 
    countryLookupMap.put("SC","Seychelles"); 
    countryLookupMap.put("SD","Sudan"); 
    countryLookupMap.put("SE","Sweden"); 
    countryLookupMap.put("SG","Singapore"); 
    countryLookupMap.put("SH","Saint Helena"); 
    countryLookupMap.put("SI","Slovenia"); 
    countryLookupMap.put("SJ","Svalbard and Jan Mayen"); 
    countryLookupMap.put("SK","Slovakia"); 
    countryLookupMap.put("SL","Sierra Leone"); 
    countryLookupMap.put("SM","San Marino"); 
    countryLookupMap.put("SN","Senegal"); 
    countryLookupMap.put("SO","Somalia"); 
    countryLookupMap.put("SR","Suriname"); 
    countryLookupMap.put("ST","Sao Tome and Principe"); 
    countryLookupMap.put("SV","El Salvador"); 
    countryLookupMap.put("SY","Syria"); 
    countryLookupMap.put("SZ","Swaziland"); 
    countryLookupMap.put("TC","Turks and Caicos Islands"); 
    countryLookupMap.put("TD","Chad"); 
    countryLookupMap.put("TF","French Southern territories"); 
    countryLookupMap.put("TG","Togo"); 
    countryLookupMap.put("TH","Thailand"); 
    countryLookupMap.put("TJ","Tajikistan"); 
    countryLookupMap.put("TK","Tokelau"); 
    countryLookupMap.put("TM","Turkmenistan"); 
    countryLookupMap.put("TN","Tunisia"); 
    countryLookupMap.put("TO","Tonga"); 
    countryLookupMap.put("TP","East Timor"); 
    countryLookupMap.put("TR","Turkey"); 
    countryLookupMap.put("TT","Trinidad and Tobago"); 
    countryLookupMap.put("TV","Tuvalu"); 
    countryLookupMap.put("TW","Taiwan"); 
    countryLookupMap.put("TZ","Tanzania"); 
    countryLookupMap.put("UA","Ukraine"); 
    countryLookupMap.put("UG","Uganda"); 
    countryLookupMap.put("UM","United States Minor Outlying Islands"); 
    countryLookupMap.put("US","United States"); 
    countryLookupMap.put("UY","Uruguay"); 
    countryLookupMap.put("UZ","Uzbekistan"); 
    countryLookupMap.put("VA","Holy See (Vatican City State)"); 
    countryLookupMap.put("VC","Saint Vincent and the Grenadines"); 
    countryLookupMap.put("VE","Venezuela"); 
    countryLookupMap.put("VG","Virgin Islands, British"); 
    countryLookupMap.put("VI","Virgin Islands, U.S."); 
    countryLookupMap.put("VN","Vietnam"); 
    countryLookupMap.put("VU","Vanuatu"); 
    countryLookupMap.put("WF","Wallis and Futuna"); 
    countryLookupMap.put("WS","Samoa"); 
    countryLookupMap.put("YE","Yemen"); 
    countryLookupMap.put("YT","Mayotte"); 
    countryLookupMap.put("YU","Yugoslavia"); 
    countryLookupMap.put("ZA","South Africa"); 
    countryLookupMap.put("ZM","Zambia"); 
    countryLookupMap.put("ZW","Zimbabwe"); 

獲得國家代碼後,重複hasmap和國家代碼的基礎上搜索,那麼你會得到國家代碼。

這可能對你有幫助。

3

而是有國家的一個巨大的HashMap和他們的代碼(這也將工作),你可以嘗試以下之一:

區域設置方法

String locale = context.getResources().getConfiguration().locale.getDisplayCountry(); 

然而,這1韓元在Android沒有Locale的國家工作。例如,在瑞士,這種語言可能會被設定爲德語或法語。這種方法會給你德國或法國,而不是瑞士。最好使用LocationManager或TelephonyManager方法。

電話管理辦法

TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); 
String countryCode = tm.getSimCountryIso(); 

這將通過從SIM卡獲取國家工作。它將需要READ_PHONE_STATE權限。當沒有SIM卡時不起作用,或者當電話在國際漫遊時不準確。

Location Manager Approach (most reliable)

首先,獲得LocationManager。然後,致電LocationManager.getLastKnownPosition()。然後,創建一個GeoCoder並致電GeoCoder.getFromLocation()。這是一個單獨的線程!這會給你一個Address對象的列表。打電話Address.getCountryName(),你明白了。

請記住,最後一個已知位置可能有點陳舊,所以如果用戶剛剛越過邊界,您可能暫時不知道它。但是,它比其他任何人都更可靠。

0

與Ajay的答案類似,您也可以使用XML文件。

country_name_map.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="MissingTranslation"> 

<string name="ad_country">Andorra</string> 
<string name="ae_country">United Arab Emirates</string> 
<string name="af_country">Afghanistan</string> 
<string name="ag_country">Antigua and Barbuda</string> 
<string name="ai_country">Anguilla</string> 
<string name="al_country">Albania</string> 
<string name="am_country">Armenia</string> 
<string name="an_country">Netherlands Antilles</string> 
<string name="ao_country">Angola</string> 
<string name="aq_country">Antarctica</string> 
<string name="ar_country">Argentina</string> 
<string name="as_country">American Samoa</string> 
<string name="at_country">Austria</string> 
<string name="au_country">Australia</string> 
<string name="aw_country">Aruba</string> 
<string name="ax_country">Åland Islands</string> 
<string name="az_country">Azerbaijan</string> 
<string name="ba_country">Bosnia and Herzegovina</string> 
<string name="bb_country">Barbados</string> 
<string name="bd_country">Bangladesh</string> 
<string name="be_country">Belgium</string> 
<string name="bf_country">Burkina Faso</string> 
<string name="bg_country">Bulgaria</string> 
<string name="bh_country">Bahrain</string> 
<string name="bi_country">Burundi</string> 
<string name="bj_country">Benin</string> 
<string name="bl_country">Saint Barthélemy</string> 
<string name="bm_country">Bermuda</string> 
<string name="bn_country">Brunei Darussalam</string> 
<string name="bo_country">Bolivia (Plurinational State of)</string> 
<string name="bq_country">Bonaire, Sint Eustatius and Saba</string> 
<string name="br_country">Brazil</string> 
<string name="bs_country">Bahamas</string> 
<string name="bt_country">Bhutan</string> 
<string name="bv_country">Bouvet Island</string> 
<string name="bw_country">Botswana</string> 
<string name="by_country">Belarus</string> 
<string name="bz_country">Belize</string> 
<string name="ca_country">Canada</string> 
<string name="cc_country">Cocos (Keeling) Islands</string> 
<string name="cd_country">Congo (Democratic Republic of the)</string> 
<string name="cf_country">Central African Republic</string> 
<string name="cg_country">Congo</string> 
<string name="ch_country">Switzerland</string> 
<string name="ci_country">Côte d\'Ivoire</string> 
<string name="ck_country">Cook Islands</string> 
<string name="cl_country">Chile</string> 
<string name="cm_country">Cameroon</string> 
<string name="cn_country">China</string> 
<string name="co_country">Colombia</string> 
<string name="cr_country">Costa Rica</string> 
<string name="cu_country">Cuba</string> 
<string name="cv_country">Cabo Verde</string> 
<string name="cw_country">Curaçao</string> 
<string name="cx_country">Christmas Island</string> 
<string name="cy_country">Cyprus</string> 
<string name="cz_country">Czechia</string> 
<string name="de_country">Germany</string> 
<string name="dj_country">Djibouti</string> 
<string name="dk_country">Denmark</string> 
<string name="dm_country">Dominica</string> 
<string name="do_country">Dominican Republic</string> 
<string name="dz_country">Algeria</string> 
<string name="ec_country">Ecuador</string> 
<string name="ee_country">Estonia</string> 
<string name="eg_country">Egypt</string> 
<string name="eh_country">Western Sahara</string> 
<string name="er_country">Eritrea</string> 
<string name="es_country">Spain</string> 
<string name="et_country">Ethiopia</string> 
<string name="fi_country">Finland</string> 
<string name="fj_country">Fiji</string> 
<string name="fk_country">Falkland Islands (Malvinas)</string> 
<string name="fm_country">Micronesia (Federated States of)</string> 
<string name="fo_country">Faroe Islands</string> 
<string name="fr_country">France</string> 
<string name="ga_country">Gabon</string> 
<string name="gb_country">United Kingdom of Great Britain and Northern Ireland</string> 
<string name="gd_country">Grenada</string> 
<string name="ge_country">Georgia</string> 
<string name="gf_country">French Guiana</string> 
<string name="gg_country">Guernsey</string> 
<string name="gh_country">Ghana</string> 
<string name="gi_country">Gibraltar</string> 
<string name="gl_country">Greenland</string> 
<string name="gm_country">Gambia</string> 
<string name="gn_country">Guinea</string> 
<string name="gp_country">Guadeloupe</string> 
<string name="gq_country">Equatorial Guinea</string> 
<string name="gr_country">Greece</string> 
<string name="gs_country">South Georgia and the South Sandwich Islands</string> 
<string name="gt_country">Guatemala</string> 
<string name="gu_country">Guam</string> 
<string name="gw_country">Guinea-Bissau</string> 
<string name="gy_country">Guyana</string> 
<string name="hk_country">Hong Kong</string> 
<string name="hm_country">Heard Island and McDonald Islands</string> 
<string name="hn_country">Honduras</string> 
<string name="hr_country">Croatia</string> 
<string name="ht_country">Haiti</string> 
<string name="hu_country">Hungary</string> 
<string name="id_country">Indonesia</string> 
<string name="ie_country">Ireland</string> 
<string name="il_country">Israel</string> 
<string name="im_country">Isle of Man</string> 
<string name="in_country">India</string> 
<string name="io_country">British Indian Ocean Territory</string> 
<string name="iq_country">Iraq</string> 
<string name="ir_country">Iran (Islamic Republic of)</string> 
<string name="is_country">Iceland</string> 
<string name="it_country">Italy</string> 
<string name="je_country">Jersey</string> 
<string name="jm_country">Jamaica</string> 
<string name="jo_country">Jordan</string> 
<string name="jp_country">Japan</string> 
<string name="ke_country">Kenya</string> 
<string name="kg_country">Kyrgyzstan</string> 
<string name="kh_country">Cambodia</string> 
<string name="ki_country">Kiribati</string> 
<string name="km_country">Comoros</string> 
<string name="kn_country">Saint Kitts and Nevis</string> 
<string name="kp_country">Korea (Democratic People\'s Republic of)</string> 
<string name="kr_country">Korea (Republic of)</string> 
<string name="kw_country">Kuwait</string> 
<string name="ky_country">Cayman Islands</string> 
<string name="kz_country">Kazakhstan</string> 
<string name="la_country">Lao People\'s Democratic Republic</string> 
<string name="lb_country">Lebanon</string> 
<string name="lc_country">Saint Lucia</string> 
<string name="li_country">Liechtenstein</string> 
<string name="lk_country">Sri Lanka</string> 
<string name="lr_country">Liberia</string> 
<string name="ls_country">Lesotho</string> 
<string name="lt_country">Lithuania</string> 
<string name="lu_country">Luxembourg</string> 
<string name="lv_country">Latvia</string> 
<string name="ly_country">Libya</string> 
<string name="ma_country">Morocco</string> 
<string name="mc_country">Monaco</string> 
<string name="md_country">Moldova (Republic of)</string> 
<string name="me_country">Montenegro</string> 
<string name="mf_country">Saint Martin (French part)</string> 
<string name="mg_country">Madagascar</string> 
<string name="mh_country">Marshall Islands</string> 
<string name="mk_country">Macedonia (the former Yugoslav Republic of)</string> 
<string name="ml_country">Mali</string> 
<string name="mm_country">Myanmar</string> 
<string name="mn_country">Mongolia</string> 
<string name="mo_country">Macao</string> 
<string name="mp_country">Northern Mariana Islands</string> 
<string name="mq_country">Martinique</string> 
<string name="mr_country">Mauritania</string> 
<string name="ms_country">Montserrat</string> 
<string name="mt_country">Malta</string> 
<string name="mu_country">Mauritius</string> 
<string name="mv_country">Maldives</string> 
<string name="mw_country">Malawi</string> 
<string name="mx_country">Mexico</string> 
<string name="my_country">Malaysia</string> 
<string name="mz_country">Mozambique</string> 
<string name="na_country">Namibia</string> 
<string name="nc_country">New Caledonia</string> 
<string name="ne_country">Niger</string> 
<string name="nf_country">Norfolk Island</string> 
<string name="ng_country">Nigeria</string> 
<string name="ni_country">Nicaragua</string> 
<string name="nl_country">Netherlands</string> 
<string name="no_country">Norway</string> 
<string name="np_country">Nepal</string> 
<string name="nr_country">Nauru</string> 
<string name="nu_country">Niue</string> 
<string name="nz_country">New Zealand</string> 
<string name="om_country">Oman</string> 
<string name="pa_country">Panama</string> 
<string name="pe_country">Peru</string> 
<string name="pf_country">French Polynesia</string> 
<string name="pg_country">Papua New Guinea</string> 
<string name="ph_country">Philippines</string> 
<string name="pk_country">Pakistan</string> 
<string name="pl_country">Poland</string> 
<string name="pm_country">Saint Pierre and Miquelon</string> 
<string name="pn_country">Pitcairn</string> 
<string name="pr_country">Puerto Rico</string> 
<string name="ps_country">Palestine, State of</string> 
<string name="pt_country">Portugal</string> 
<string name="pw_country">Palau</string> 
<string name="py_country">Paraguay</string> 
<string name="qa_country">Qatar</string> 
<string name="re_country">Réunion</string> 
<string name="ro_country">Romania</string> 
<string name="rs_country">Serbia</string> 
<string name="ru_country">Russian Federation</string> 
<string name="rw_country">Rwanda</string> 
<string name="sa_country">Saudi Arabia</string> 
<string name="sb_country">Solomon Islands</string> 
<string name="sc_country">Seychelles</string> 
<string name="sd_country">Sudan</string> 
<string name="se_country">Sweden</string> 
<string name="sg_country">Singapore</string> 
<string name="sh_country">Saint Helena, Ascension and Tristan da Cunha</string> 
<string name="si_country">Slovenia</string> 
<string name="sj_country">Svalbard and Jan Mayen</string> 
<string name="sk_country">Slovakia</string> 
<string name="sl_country">Sierra Leone</string> 
<string name="sm_country">San Marino</string> 
<string name="sn_country">Senegal</string> 
<string name="so_country">Somalia</string> 
<string name="sr_country">Suriname</string> 
<string name="ss_country">South Sudan</string> 
<string name="st_country">Sao Tome and Principe</string> 
<string name="sv_country">El Salvador</string> 
<string name="sx_country">Sint Maarten (Dutch part)</string> 
<string name="sy_country">Syrian Arab Republic</string> 
<string name="sz_country">Swaziland</string> 
<string name="tc_country">Turks and Caicos Islands</string> 
<string name="td_country">Chad</string> 
<string name="tf_country">French Southern Territories</string> 
<string name="tg_country">Togo</string> 
<string name="th_country">Thailand</string> 
<string name="tj_country">Tajikistan</string> 
<string name="tk_country">Tokelau</string> 
<string name="tl_country">Timor-Leste</string> 
<string name="tm_country">Turkmenistan</string> 
<string name="tn_country">Tunisia</string> 
<string name="to_country">Tonga</string> 
<string name="tr_country">Turkey</string> 
<string name="tt_country">Trinidad and Tobago</string> 
<string name="tv_country">Tuvalu</string> 
<string name="tw_country">Taiwan, Province of China[a]</string> 
<string name="tz_country">Tanzania, United Republic of</string> 
<string name="ua_country">Ukraine</string> 
<string name="ug_country">Uganda</string> 
<string name="um_country">United States Minor Outlying Islands</string> 
<string name="us_country">United States of America</string> 
<string name="uy_country">Uruguay</string> 
<string name="uz_country">Uzbekistan</string> 
<string name="va_country">Holy See</string> 
<string name="vc_country">Saint Vincent and the Grenadines</string> 
<string name="ve_country">Venezuela (Bolivarian Republic of)</string> 
<string name="vg_country">Virgin Islands (British)</string> 
<string name="vi_country">Virgin Islands (U.S.)</string> 
<string name="vn_country">Viet Nam</string> 
<string name="vu_country">Vanuatu</string> 
<string name="wf_country">Wallis and Futuna</string> 
<string name="ws_country">Samoa</string> 
<string name="ye_country">Yemen</string> 
<string name="yt_country">Mayotte</string> 
<string name="za_country">South Africa</string> 
<string name="zm_country">Zambia</string> 
<string name="zw_country">Zimbabwe</string> 

</resources> 

注意,我所附_country因爲do是保留關鍵字。

然後你可以定義一個獲取國名的方法。

public static String getCountryNameFromIso2Code(Context context, String iso2Code) { 
    if(context == null || TextUtils.isEmpty(iso2Code)) return ""; 

    String key = String.format(Locale.US, "%s%s", iso2Code.toLowerCase(Locale.US), "_country"); 
    int resId = context.getResources().getIdentifier(key, "string", context.getPackageName()); 

    return context.getString(resId); 
} 

使用範例:

String sampleInputCountryCode = "an"; 
String fullCountryName = MyUtilityClass.getCountryNameFromIso2Code(getApplicationContext(), sampleInputCountryCode); 
相關問題