1

콘텐츠 검색기를 사용하여 휴대 전화 주소록에서 전화 번호를 가져 오는 코드가 있습니다. 휴대 전화 2 대에서이를 테스트했습니다. 국가 코드를 가져오고 다른 국가에서 가져옵니다. 국가 코드는 휴대 전화 번호에있는 경우 방법은 알아하고 있습니다 나는 number.Here에서 분리 할있는 경우 내 code.Please 나를존재하는 경우 휴대 전화 번호에서 국가 코드를 분리합니다.

ContentResolver cr = getContentResolver(); 
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, 
      null, null, null, null); 

    if ((cur != null ? cur.getCount() : 0) > 0) { 
     while (cur != null && cur.moveToNext()) { 
      String id = cur.getString(
        cur.getColumnIndex(ContactsContract.Contacts._ID)); 
      String name = cur.getString(cur.getColumnIndex(
        ContactsContract.Contacts.DISPLAY_NAME)); 
      phoneContactName.add(name); 

      if (cur.getInt(cur.getColumnIndex(
        ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) { 
       Cursor pCur = cr.query(
         ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
         null, 
         ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", 
         new String[]{id}, null); 
       while (pCur.moveToNext()) { 
        String phoneNo = pCur.getString(pCur.getColumnIndex(
          ContactsContract.CommonDataKinds.Phone.NUMBER)); 
        String countryCode = pCur.getString(pCur.getColumnIndex(
          ContactsContract.CommonDataKinds.Phone.NUMBER)); 
        phoneContactNos.add(phoneNo); 
        dup_phoneContactNos.add(phoneNo); 
        /*if (registeredContactNos.contains(phoneNo)) { 
         Toast.makeText(getApplicationContext(), "match found", Toast.LENGTH_SHORT).show(); 
         selectedContactName.add(name); 
         selectedContactNos.add(phoneNo); 
         populateList(name, phoneNo); 


        }*/ 
        Log.i(TAG, "Name: " + name); 
        Log.i(TAG, "Phone Number: " + phoneNo); 
       } 


       pCur.close(); 
      } 
     } 
     /*for(int i=0;i<selectedContactName.size();i++) 
     { 
      Toast.makeText(getApplicationContext(),selectedContactName.get(i)+","+selectedContactNos.get(i),Toast.LENGTH_SHORT).show(); 
     }*/ 
    } 
    if (cur != null) { 
     cur.close(); 
    } 
+0

https://stackoverflow.com/questions/2543938/how-to-split-mobile-number-into-country-code-area-code-and-local- 번호 –

+0

나는 이미 국가 번호와 함께 가끔 전화 번호를 얻고있다. 나는 그것을 – user8601021

답변

0

당신은 안드로이드의 PhoneNumberUtils.formatNumber을 사용할 수 있습니다 도움이됩니다.

은 만 롤리팝 이상 기기를 대상으로하는 경우 :

String formattedPhone = PhoneNumberUtils.formatNumber(phone, Locale.getDefault().getCountry())); 

당신은뿐만 아니라 이전 버전을 대상으로하는 경우 :

String formattedPhone; 
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
    formattedPhone = PhoneNumberUtils.formatNumber(phone, Locale.getDefault().getCountry())); 
} else { 
    formattedPhone = PhoneNumberUtils.formatNumber(phone)); 
} 
+0

과 분리하고 전화는 무엇인가? 숫자를 가져온 문자열은 무엇입니까? – user8601021

+0

국가 코드가없는 no.with 및 no를 보여줍니다. – user8601021

+0

'formattedPhone'은 국가 코드없이 현재 국가 서식 규칙에 따라 번호를 형식화해야합니다. 'formattedPhone'에 대한 국가 코드를 받고 있습니까? – marmor

관련 문제