2011-06-22 7 views
12

私はアンドロイドアプリケーションの新しい開発者です。私は、国番号で携帯電話番号を渡すと、ISOの国コードを取得したいと思います。 1-319-491-6338として携帯電話番号を渡すと、アンドロイドで米国/米国の国のISOコードを取得できますか?アンドロイドアプリケーションでISO国コードを取得するには?

次のように私は、コードを書かれている:

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

、どこ私は携帯電話番号を渡すことができますか?

誰でもお手伝いできますか?事前

+0

あなたが考えるほど単純ではないかもしれません。このサイトを見て:http://countrycode.org/。彼らは少なくとも2つの異なる国に電話コード「1」を挙げている。この場合、どのように決定しますか? – bdares

+1

String countryCode = tm.getSimCountryIso(); この行自体がISOコードを米国として提供しています – ingsaurabh

答えて

7

ありがとうございます、標準のAPIを介してプログラムで国コードを照会することができないかもしれないが、あなたは、あなたのアプリのテーブルを含めることができます。このようなテーブルはGoogleを介して簡単に見つけることができます(例:http://countrycode.org/)。

危険ウィルロビンソン!:しかし、あなたは何を答えようとしているのか自分自身に尋ねるべきです。あなたの質問には、国際ダイヤルコードとISO国コードの間に1対1のマッピングがあるという前提が暗示されています。これはではなく、です。たとえば、米国とカナダの両方に国際ダイヤルコード「1」があります。

おそらく、あなたのアプリのインターフェースを再構成することを考えてください。ユーザーが電話番号に関連付ける国を選択できるようにしますが、http://countrycode.org/の表を使用して、最も可能性の高い候補者を一番上に並べます。

2

あなたはcountry calling codeだけでなく、その次のURLでISO name http://en.wikipedia.org/wiki/List_of_country_calling_codes

または

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

ステップ-2を得ることができます。ステップ-1 あなたはのページのソースを取得することができますそのファイルはJavaプログラムを使用しています。

ステップ3これらのHTMLファイルを利用可能なパーサーのいずれかを使用してXML形式に変換できます。 Open Source HTML Parsers in Java

ステップ-4呼び出しコードを取得できる電話番号を作成します。例:数値が「1-319-491-6338」の場合、コードは1

ステップ-5この呼び出しコードを、XMLパーサから取得した呼び出しコードと国名リストと照合します。この方法であなたはisoの国を得ることができます

2

同じ問題がありました。最終的に私はすべてのデータをExcelに入れ、Excelシートを読みます。

  1. コピー、過去のMicrosoft Excelファイルへhttp://countrycode.org/から国別コード表: はここで実装したものです。
  2. \ res \ raw \ countrycode_orgにExcelファイルを97-2003互換(.xls)として保存します。here
  3. からXLS

  4. ダウンロードJExcelApiは、ファイルを読むために以下のクラスを使用します。

    パブリッククラスCountryCodes { プライベートHashMapのmCountryByName =新しいHashMapの(); プライベートHashMap mCountryByCode = new HashMap();; プライベートArrayList mCountries =新しい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; 
    
    } 
    

    }

関連する問題