2011-06-22 29 views
0

連絡先から複数の電話番号(ホーム、セル、仕事)を取得する方法を検索して検索していますが、私は以下で使用しているコードを追加します。私は助けてくれることを願っています。おかげ ジェフアンドロイドの連絡先から複数の番号を取得

case CONTACT_PICKER_RESULT: 
      Log.w("+DEBUG_TAG+","Got the Info"); 

      //handle contact results 
      Cursor cursor = null; 
      String number = ""; 
      String number2 = ""; 
      try{ 
      Uri result = data.getData();      
      //get the content id 
      String id = result.getLastPathSegment(); 

      //ask for the phone number 
      cursor = getContentResolver().query(Phone.CONTENT_URI, 
        null, Phone.CONTACT_ID + "=?", new String[] {id}, 
        null); 
      int phoneIdx = cursor.getColumnIndex(Phone.DATA); 

      //take the phone number 
      if(cursor.moveToFirst()){ 
       number = cursor.getString(phoneIdx); 
       Log.v("+DEBUG_TAG+","Got number " + number); 
      }else if(cursor.moveToNext()){ 
       number2 = cursor.getString(phoneIdx); 
       Log.v("+DEBUG_TAG+","GOT NumbEr2 "+ number2); 
      } 
      else{ 
       Log.e("+DEBUG_TAG","FAILED TO GET NUMBER!"); 
      } 
      } 

      finally{ 
       if(cursor != null){ 
        cursor.close(); 
       } 
       EditText phNumberEditText = (EditText) findViewById(R.id.number1); 
       phNumberEditText.setText(number); 
       if (number.length() == 0){ 
        Toast.makeText(this, "No Phone Number For This Contact", 
          Toast.LENGTH_LONG).show(); 
      } 
+0

** barmaley **、文字列R.string.home_phone、R.string.mobile_phone、R.stringの値は何ですか。勤務先の電話...? – iosDev

+0

このリンクを試すと、ヘルプが表示されます。http://www.higherpass.com/Android/Tutorials/Working-With-Android-Contacts/ – kannappan

答えて

0

使用この種のコード:

public void readContacts(Context context) 
{ 
    String contactId, hasPhone, phoneNumber; 
    ContentResolver cr=context.getContentResolver(); 
    Cursor phones, cc = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null); 
    while (cc.moveToNext()) 
    { 
     contactId = cc.getString(cc.getColumnIndex(ContactsContract.Contacts._ID)); 
     hasPhone = cc.getString(cc.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
     int nameFieldColumnIndex = cc.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME); 
     String contactName = cc.getString(nameFieldColumnIndex); 
     Log.v(TAG, "Contact id="+contactId+" name="+contactName); 
     if (Integer.parseInt(hasPhone)==1) 
     { 
      // You know it has a number so now query it like this 
      phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
        null, 
        ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null); 
      while (phones.moveToNext()) 
      { 
       phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
       String label=getPhoneLabel(context, phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)), 
         phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL))); 
       Log.v(TAG, "Phone"+phoneNumber+" with label="+label); 
      } 
      phones.close(); 
     } 
    } 
    cc.close(); 
} 

private String getPhoneLabel(Context context, int type, String label) 
{ 
    String s; 
    switch(type) 
    { 
     case ContactsContract.CommonDataKinds.Phone.TYPE_HOME: 
      s = context.getString(R.string.home_phone); 
      break; 
     case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE: 
      s = context.getString(R.string.mobile_phone); 
      break; 
     case ContactsContract.CommonDataKinds.Phone.TYPE_WORK: 
      s = context.getString(R.string.work_phone); 
      break; 
     case ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK: 
      s = context.getString(R.string.fax_work_phone); 
      break; 
     case ContactsContract.CommonDataKinds.Phone.TYPE_FAX_HOME: 
      s = context.getString(R.string.fax_home_phone); 
      break; 
     case ContactsContract.CommonDataKinds.Phone.TYPE_PAGER: 
      s = context.getString(R.string.pager_phone); 
      break; 
     case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER: 
      s = context.getString(R.string.other_phone); 
      break; 
     case ContactsContract.CommonDataKinds.Phone.TYPE_CALLBACK: 
      s = context.getString(R.string.callback_phone); 
      break; 
     case ContactsContract.CommonDataKinds.Phone.TYPE_CAR: 
      s = context.getString(R.string.car_phone); 
      break; 
     case ContactsContract.CommonDataKinds.Phone.TYPE_COMPANY_MAIN: 
      s = context.getString(R.string.company_main_phone); 
      break; 
     case ContactsContract.CommonDataKinds.Phone.TYPE_ISDN: 
      s = context.getString(R.string.isdn_phone); 
      break; 
     case ContactsContract.CommonDataKinds.Phone.TYPE_MAIN: 
      s = context.getString(R.string.main_phone); 
      break; 
     case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER_FAX: 
      s = context.getString(R.string.other_fax_phone); 
      break; 
     case ContactsContract.CommonDataKinds.Phone.TYPE_RADIO: 
      s = context.getString(R.string.radio_phone); 
      break; 
     case ContactsContract.CommonDataKinds.Phone.TYPE_TELEX: 
      s = context.getString(R.string.telex_phone); 
      break; 
     case ContactsContract.CommonDataKinds.Phone.TYPE_TTY_TDD: 
      s = context.getString(R.string.tty_tdd_phone); 
      break; 
     case ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE: 
      s = context.getString(R.string.work_mobile_phone); 
      break; 
     case ContactsContract.CommonDataKinds.Phone.TYPE_WORK_PAGER: 
      s = context.getString(R.string.work_pager_phone); 
      break; 
     case ContactsContract.CommonDataKinds.Phone.TYPE_ASSISTANT: 
      s = context.getString(R.string.assistant_phone); 
      break; 
     case ContactsContract.CommonDataKinds.Phone.TYPE_MMS: 
      s = context.getString(R.string.mms_phone); 
      break; 
     case ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM: 
      if(label == null) 
       s = context.getString(R.string.phone); 
      else 
       s = label; 
      break; 
     default: 
      s = context.getString(R.string.phone); 
    } 
    return s; 
} 
関連する問題