我正在尝试在appStartup上设置电话代码。
可能的解决方案:从地理编码器获取国家/地区名称,之后我拥有xml 原始资源,其中保留了所有国家/地区名称,代码, phoneCode。
然后创建了一个活动来获取数组列表中的所有内容(使用了getter和setter)
这是我的国家/地区列表文件:
public class ListCountry {
static String TAG="Class Country";
String nameCode;
String phoneCode;
public ListCountry(){}
public ListCountry(String nameCode,String phoneCode){
this.nameCode=nameCode;
this.phoneCode=phoneCode;
}
public static List<ListCountry> readXMLofCountries(Context context){
List<ListCountry> countries = new ArrayList<ListCountry>();
try{
XmlPullParserFactory xmlFactoryObject=XmlPullParserFactory.newInstance();
XmlPullParser xmlPullParser=xmlFactoryObject.newPullParser();
InputStream ins=context.getResources().openRawResource(R.raw.countries);
xmlPullParser.setInput(ins,null);
int event=xmlPullParser.getEventType();
while(event!=xmlPullParser.END_DOCUMENT){
String name=xmlPullParser.getName();
switch(event){
case XmlPullParser.START_TAG:
break;
case xmlPullParser.END_TAG:
if(name.equals("country")){
ListCountry country=new ListCountry();
country.setNameCode(xmlPullParser.getAttributeValue(null, "code").toUpperCase());
country.setPhoneCode(xmlPullParser.getAttributeValue(null, "phoneCode"));
country.setName(xmlPullParser.getAttributeValue(null, "name"));
countries.add(country);
}
}
event = xmlPullParser.next();
}
}
return countries;
}
public String getNameCode() {
return nameCode;
}
public void setNameCode(String nameCode) {
this.nameCode = nameCode;
}
public String getPhoneCode() {
return phoneCode;
}
public void setPhoneCode(String phoneCode) {
this.phoneCode = phoneCode;
}
}
注意:忽略此代码中的catch块,因为我现在没有这样做。
问题1:如何在onCreate方法的应用启动中获取国家/地区代码或国家/地区名称。 问题2:如果我获得国家/地区名称或国家/地区代码,请假设印度或IN位于XML中
<country code='IN' phoneCode='91' name='India' />
现在如何获取此phoneCode以便我可以使用phoneCode + mobile_number发送otp。
记住目标:用户不会选择国家/地区名称或国家/地区代码,它会自动设置代码,这是我的主要任务或目标。
答案 0 :(得分:1)
首先使用位置监听器或新的融合位置获取用户的当前位置。然后使用下面的代码对坐标进行地理编码,这里的代码行countryName= address.getCountryName();
为您提供该国家/地区的名称。然后在同一方法中,行Log.d("codeee", new CountryCodes().getCode(countryName));
将访问下面提到的类CountryCodes,然后返回相应国家/地区的国家/地区代码。
public void getAddressFromLocation(final double latitude, final double longitude) {
Thread thread = new Thread() {
@Override
public void run() {
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
String countryName= null;
try {
List<Address> addressList = geocoder.getFromLocation(
latitude, longitude, 1);
if (addressList != null && addressList.size() > 0) {
Address address = addressList.get(0);
countryName= address.getCountryName();
}
} catch (IOException e) {
Log.e("Exception Error", "Unable to connect", e);
} finally {
Log.d("codeee", new CountryCodes().getCode(countryName));
}
}
};
thread.start();
}
然后,当您拥有国家/地区名称时,请使用此类获取国家/地区代码:
public class CountryCodes {
final Map<String, String> map = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
public CountryCodes() {
map.put("Andorra, Principality Of", "AD");
map.put("United Arab Emirates", "AE");
map.put("Afghanistan, Islamic State Of", "AF");
map.put("Antigua And Barbuda", "AG");
map.put("Anguilla", "AI");
map.put("Albania", "AL");
map.put("Armenia", "AM");
map.put("Netherlands Antilles", "AN");
map.put("Angola", "AO");
map.put("Antarctica", "AQ");
map.put("Argentina", "AR");
map.put("American Samoa", "AS");
map.put("Austria", "AT");
map.put("Australia", "AU");
map.put("Aruba", "AW");
map.put("Azerbaidjan", "AZ");
map.put("Bosnia-Herzegovina", "BA");
map.put("Barbados", "BB");
map.put("Bangladesh", "BD");
map.put("Belgium", "BE");
map.put("Burkina Faso", "BF");
map.put("Bulgaria", "BG");
map.put("Bahrain", "BH");
map.put("Burundi", "BI");
map.put("Benin", "BJ");
map.put("Bermuda", "BM");
map.put("Brunei Darussalam", "BN");
map.put("Bolivia", "BO");
map.put("Brazil", "BR");
map.put("Bahamas", "BS");
map.put("Bhutan", "BT");
map.put("Bouvet Island", "BV");
map.put("Botswana", "BW");
map.put("Belarus", "BY");
map.put("Belize", "BZ");
map.put("Canada", "CA");
map.put("Cocos (Keeling) Islands", "CC");
map.put("Central African Republic", "CF");
map.put("Congo, The Democratic Republic Of The", "CD");
map.put("Congo", "CG");
map.put("Switzerland", "CH");
map.put("Ivory Coast (Cote D'Ivoire)", "CI");
map.put("Cook Islands", "CK");
map.put("Chile", "CL");
map.put("Cameroon", "CM");
map.put("China", "CN");
map.put("Colombia", "CO");
map.put("Costa Rica", "CR");
map.put("Former Czechoslovakia", "CS");
map.put("Cuba", "CU");
map.put("Cape Verde", "CV");
map.put("Christmas Island", "CX");
map.put("Cyprus", "CY");
map.put("Czech Republic", "CZ");
map.put("Germany", "DE");
map.put("Djibouti", "DJ");
map.put("Denmark", "DK");
map.put("Dominica", "DM");
map.put("Dominican Republic", "DO");
map.put("Algeria", "DZ");
map.put("Ecuador", "EC");
map.put("Estonia", "EE");
map.put("Egypt", "EG");
map.put("Western Sahara", "EH");
map.put("Eritrea", "ER");
map.put("Spain", "ES");
map.put("Ethiopia", "ET");
map.put("Finland", "FI");
map.put("Fiji", "FJ");
map.put("Falkland Islands", "FK");
map.put("Micronesia", "FM");
map.put("Faroe Islands", "FO");
map.put("France", "FR");
map.put("France (European Territory)", "FX");
map.put("Gabon", "GA");
map.put("Great Britain", "UK");
map.put("Grenada", "GD");
map.put("Georgia", "GE");
map.put("French Guyana", "GF");
map.put("Ghana", "GH");
map.put("Gibraltar", "GI");
map.put("Greenland", "GL");
map.put("Gambia", "GM");
map.put("Guinea", "GN");
map.put("Guadeloupe (French)", "GP");
map.put("Equatorial Guinea", "GQ");
map.put("Greece", "GR");
map.put("S. Georgia & S. Sandwich Isls.", "GS");
map.put("Guatemala", "GT");
map.put("Guam (USA)", "GU");
map.put("Guinea Bissau", "GW");
map.put("Guyana", "GY");
map.put("Hong Kong", "HK");
map.put("Heard And McDonald Islands", "HM");
map.put("Honduras", "HN");
map.put("Croatia", "HR");
map.put("Haiti", "HT");
map.put("Hungary", "HU");
map.put("Indonesia", "ID");
map.put("Ireland", "IE");
map.put("Israel", "IL");
map.put("India", "IN");
map.put("British Indian Ocean Territory", "IO");
map.put("Iraq", "IQ");
map.put("Iran", "IR");
map.put("Iceland", "IS");
map.put("Italy", "IT");
map.put("Jamaica", "JM");
map.put("Jordan", "JO");
map.put("Japan", "JP");
map.put("Kenya", "KE");
map.put("Kyrgyz Republic (Kyrgyzstan)", "KG");
map.put("Cambodia, Kingdom Of", "KH");
map.put("Kiribati", "KI");
map.put("Comoros", "KM");
map.put("Saint Kitts & Nevis Anguilla", "KN");
map.put("North Korea", "KP");
map.put("South Korea", "KR");
map.put("Kuwait", "KW");
map.put("Cayman Islands", "KY");
map.put("Kazakhstan", "KZ");
map.put("Laos", "LA");
map.put("Lebanon", "LB");
map.put("Saint Lucia", "LC");
map.put("Liechtenstein", "LI");
map.put("Sri Lanka", "LK");
map.put("Liberia", "LR");
map.put("Lesotho", "LS");
map.put("Lithuania", "LT");
map.put("Luxembourg", "LU");
map.put("Latvia", "LV");
map.put("Libya", "LY");
map.put("Morocco", "MA");
map.put("Monaco", "MC");
map.put("Moldavia", "MD");
map.put("Madagascar", "MG");
map.put("Marshall Islands", "MH");
map.put("Macedonia", "MK");
map.put("Mali", "ML");
map.put("Myanmar", "MM");
map.put("Mongolia", "MN");
map.put("Macau", "MO");
map.put("Northern Mariana Islands", "MP");
map.put("Martinique (French)", "MQ");
map.put("Mauritania", "MR");
map.put("Montserrat", "MS");
map.put("Malta", "MT");
map.put("Mauritius", "MU");
map.put("Maldives", "MV");
map.put("Malawi", "MW");
map.put("Mexico", "MX");
map.put("Malaysia", "MY");
map.put("Mozambique", "MZ");
map.put("Namibia", "NA");
map.put("New Caledonia (French)", "NC");
map.put("Niger", "NE");
map.put("Norfolk Island", "NF");
map.put("Nigeria", "NG");
map.put("Nicaragua", "NI");
map.put("Netherlands", "NL");
map.put("Norway", "NO");
map.put("Nepal", "NP");
map.put("Nauru", "NR");
map.put("Neutral Zone", "NT");
map.put("Niue", "NU");
map.put("New Zealand", "NZ");
map.put("Oman", "OM");
map.put("Panama", "PA");
map.put("Peru", "PE");
map.put("Polynesia (French)", "PF");
map.put("Papua New Guinea", "PG");
map.put("Philippines", "PH");
map.put("Pakistan", "PK");
map.put("Poland", "PL");
map.put("Saint Pierre And Miquelon", "PM");
map.put("Pitcairn Island", "PN");
map.put("Puerto Rico", "PR");
map.put("Portugal", "PT");
map.put("Palau", "PW");
map.put("Paraguay", "PY");
map.put("Qatar", "QA");
map.put("Reunion (French)", "RE");
map.put("Romania", "RO");
map.put("Russian Federation", "RU");
map.put("Rwanda", "RW");
map.put("Saudi Arabia", "SA");
map.put("Solomon Islands", "SB");
map.put("Seychelles", "SC");
map.put("Sudan", "SD");
map.put("Sweden", "SE");
map.put("Singapore", "SG");
map.put("Saint Helena", "SH");
map.put("Slovenia", "SI");
map.put("Svalbard And Jan Mayen Islands", "SJ");
map.put("Slovak Republic", "SK");
map.put("Sierra Leone", "SL");
map.put("San Marino", "SM");
map.put("Senegal", "SN");
map.put("Somalia", "SO");
map.put("Suriname", "SR");
map.put("Saint Tome (Sao Tome) And Principe", "ST");
map.put("Former USSR", "SU");
map.put("El Salvador", "SV");
map.put("Syria", "SY");
map.put("Swaziland", "SZ");
map.put("Turks And Caicos Islands", "TC");
map.put("Chad", "TD");
map.put("French Southern Territories", "TF");
map.put("Togo", "TG");
map.put("Thailand", "TH");
map.put("Tadjikistan", "TJ");
map.put("Tokelau", "TK");
map.put("Turkmenistan", "TM");
map.put("Tunisia", "TN");
map.put("Tonga", "TO");
map.put("East Timor", "TP");
map.put("Turkey", "TR");
map.put("Trinidad And Tobago", "TT");
map.put("Tuvalu", "TV");
map.put("Taiwan", "TW");
map.put("Tanzania", "TZ");
map.put("Ukraine", "UA");
map.put("Uganda", "UG");
map.put("United Kingdom", "UK");
map.put("USA Minor Outlying Islands", "UM");
map.put("United States", "US");
map.put("Uruguay", "UY");
map.put("Uzbekistan", "UZ");
map.put("Holy See (Vatican City State)", "VA");
map.put("Saint Vincent & Grenadines", "VC");
map.put("Venezuela", "VE");
map.put("Virgin Islands (British)", "VG");
map.put("Virgin Islands (USA)", "VI");
map.put("Vietnam", "VN");
map.put("Vanuatu", "VU");
map.put("Wallis And Futuna Islands", "WF");
map.put("Samoa", "WS");
map.put("Yemen", "YE");
map.put("Mayotte", "YT");
map.put("Yugoslavia", "YU");
map.put("South Africa", "ZA");
map.put("Zambia", "ZM");
map.put("Zaire", "ZR");
map.put("Zimbabwe", "ZW");
}
public String getCode(String country) {
String countryFound = map.get(country);
if (countryFound == null) {
countryFound = "NP";
}
return countryFound;
}
}
在此之后,如果您需要国家/地区电话代码,请创建一个与此类似的类别,该类别将返回与国家/地区代码相关的电话代码。
public class CountryPhoneCodes {
final Map<String, String> map = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
public CountryPhoneCodes() {
map.put("NP", "977");
map.put("US", "01");
map.put("CN", "86");
}
public String getPhnCode(String countryCode) {
String phnCountryFound = map.get(countryCode);
if (phnCountryFound == null) {
phnCountryFound = "977";
}
return phnCountryFound;
}
}
这有效,我已经过测试。