开发者

How to get ISO Country code in android applications?

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 countryCo开发者_StackOverflow社区de = tm.getSimCountryIso();
      String mobileno="1-319-491-6338";

Here, where can I pass the mobile number?

Can anybody please help me ?

Thanks in advance


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?


Had the same problem. Eventually I put all the data in excel and read the excel sheet. Here is the implementation:

  1. copy-past the country code table from http://countrycode.org/ to Microsoft Excel file.
  2. Save the Excel file as 97-2003 compatible (.xls) in \res\raw\countrycode_org.xls
  3. Download JExcelApi from here
  4. 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();

    public void addCountry(String countryName,String ISO_code,String countryCode){
        countryCode = PhoneNumberUtil.normalizeDigitsOnly(countryCode);
        Country country = new Country();
        country.Name = countryName;
        country.Code = countryCode;
        country.ISO_code = ISO_code;
        mCountryByName.put(countryName, country);
        mCountryByCode.put(countryCode, country);
        mCountries.add(country);
    
        return;
    }
    
    public Country getCountryByCode(String countryCode){
        countryCode = PhoneNumberUtil.normalizeDigitsOnly(countryCode);
        return mCountryByCode.get(countryCode);
    }
    
    public Country getCountryByName(String countryName){
        return mCountryByName.get(countryName);
    }
    
    public Country getCountryByIsoCode(String ISO_code){
        ISO_code = ISO_code.toUpperCase();
        for (Country country:mCountries){
            String [] strArr = country.ISO_code.split("/| ");
            for (String s:strArr){
                if (ISO_code.equals(s))
                    return country;
            }
        }
        return null;
    }
    
    
    
    public  String[] getCountryNamesList(){
        String[] res = new String [mCountries.size()];
        int i=0;
        for (Country c:mCountries){
            res[i] = c.Name;
            i++;
        }
        return res;
    }
    
    
    
    public void readCountryCodesFromExcelWorkbook()
    {
        Context context = GlobalData.getInstance().getApp();
        Workbook mWorkbook;
        InputStream myRawResource = context.getResources().openRawResource(R.raw.countrycode_org);
        if (myRawResource == null)
            Toast.makeText(context,"XML file not found",Toast.LENGTH_LONG).show();
        else
            try {
                WorkbookSettings ws = new WorkbookSettings();
                ws.setEncoding("Cp1252");
    
                mWorkbook = Workbook.getWorkbook(myRawResource);
                    //ArrayList<String[]> currentSheet = new ArrayList<String[]>();
                    Sheet sheet = mWorkbook.getSheet(0);
    
                    int rowsNum = sheet.getRows();
                    for (int rowNum = 1; rowNum < rowsNum; rowNum++) {
                        //Log.d("RowNum", ""+rowNum);
                        int colsNum = sheet.getColumns();
                        String[] strArr = new String[colsNum];
                        boolean rowIsFull = true;
                        for (int colNum = 0; colNum < colsNum; colNum++) {
                            strArr[colNum] = sheet.getCell(colNum, rowNum).getContents();
                            if (strArr[colNum].length() == 0)
                                rowIsFull = false;
                        }
                        if (rowIsFull)
                            addCountry(strArr[0],strArr[1],strArr[2]);
                    }
    
    
            } catch (BiffException e) {
                Toast.makeText(context,"Error Reading xml file: BiffException",Toast.LENGTH_LONG).show();
                e.printStackTrace();
                return ;
            } catch (IOException e) {
                Toast.makeText(context,"Error Reading xml file: IOException",Toast.LENGTH_LONG).show();
                e.printStackTrace();
                return ;
            }
    }
    
    
    public Country[] getCountries(){
        return mCountries.toArray(new Country[0]);
    }
    
    
    
    public class Country {
        public String Name;
        public String Code;
        public String ISO_code;
    
    }
    

    }


Step-1 You can get country calling code as well as its ISO name in the following URL http://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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜