How to get ISO Country code in android applications?

Viewed 19634

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

3 Answers

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;
    
    }
    

    }

Related