2011-06-22 99 views
12

我是Android應用程序的新開發者。當我通過國家代碼的手機號碼時,我想獲得ISO國家/地區代碼。如果我通過手機號碼1-319-491-6338,我可以在android中獲得美國/美國的國家ISO代碼嗎?如何在android應用程序中獲取ISO國家代碼?

我寫的代碼如下:

 TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); 
     String countryCode = tm.getSimCountryIso(); 
     String mobileno="1-319-491-6338"; 

在這裏,我在那裏可以通過手機號碼?

任何人都可以幫我嗎?

在此先感謝

+0

它可能不是你想象的那樣簡單。看看這個網站:http://countrycode.org/。他們列出至少兩個不同的國家,電話代碼爲'1'。你如何決定在這種情況下哪一個? – bdares

+1

String countryCode = tm.getSimCountryIso(); 這條線本身給ISO代碼爲美國 – ingsaurabh

回答

7

你可能不能夠通過標準的API編程方式查詢國家代碼,但你可以在應用程式的表。這樣的表格很容易通過Google找到(例如http://countrycode.org/)。

危險威爾羅賓遜!:但是,你應該問自己你想回答什麼問題。隱含在你的問題是,假設國際撥號代碼和ISO國家代碼之間存在一對一映射。這是不是是真的。例如,美國和加拿大都有國際撥號代碼'1'。

也許考慮重新構建應用程序的界面。允許用戶選擇一個與電話號碼關聯的國家,但使用http://countrycode.org/中的表格排列最有可能的候選人名單?

2

第1步 您可以在以下網址獲得country calling code以及其ISO name http://en.wikipedia.org/wiki/List_of_country_calling_codes

http://www.unc.edu/~rowlett/units/codes/country.htm

步驟2你可以得到的頁面的源代碼該文件使用java程序。您將以HTMl格式獲得文件

第3步您可以使用任何可用的解析器將這些HTML文件轉換爲XML格式。見Open Source HTML Parsers in Java

第4步形成電話號碼,你可以得到調用代碼。例如,如果數字是「1-319-491-6338」然後調用代碼是1

步驟-5比賽反對您從XML解析器得到了調用代碼和國家名單這個調用代碼。這樣你就可以得到iso國

2

有同樣的問題。最後我把所有的數據放在excel中,然後閱讀excel表格。 這裏是實現:

  1. 複製過去從http://countrycode.org/國家代碼表到Microsoft Excel文件。
  2. 將Excel文件另存爲97-2003兼容(.xls)在\ res \ raw \ countrycode_org中。XLS
  3. 下載JExcelApihere
  4. 使用下面的類來讀取文件:

    公共類CountryCodes { 私人的HashMap mCountryByName =新的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; 
    
    } 
    

    }

相關問題