2012-02-05 77 views
0

我使用了这段代码,当点击联系人时,我可以从联系人中获取号码。但是,某些联系人有多个电话号码。我怎样才能访问另一个手机号码。我已经尝试了一个星期,但它并没有提前如何获得电话号码,如果一个联系人在Android中有多个号码

这里是我的代码工作:( 感谢:

//Choose number from phone book setup 
    btnPhonebook.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent pb = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); 
      startActivityForResult(pb, 1); 
     } 
    }); 

} 

//Listen the result from phone book button 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    //super.onActivityResult(requestCode, resultCode, data); 
    ContentResolver cr = getContentResolver(); 
    if (resultCode == Activity.RESULT_OK) { 
     Uri contactData = data.getData(); 
     Cursor contactCursor = managedQuery(contactData, null, null, null, null); 
     String cellNoFromPhonebook; 
     String phoneNumber = null; 
     if (contactCursor.moveToFirst()) { 
      cellNoFromPhonebook = contactCursor.getString(contactCursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 

      Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null); 
      while (phones.moveToNext()) 
      { 
       String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
       if (name.compareTo(cellNoFromPhonebook) == 0) { 
        phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
      } 

      } 
      phones.close(); 

      etNumber.setText(phoneNumber); 
     } 
    } 
} 

回答

1
private void pickContact() { 
     Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI); 
     startActivityForResult(intent, PICK_CONTACT_REQUEST); 
    } 

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (resultCode == Activity.RESULT_OK && requestCode == PICK_CONTACT_REQUEST) {    
      String [] numbers = null; 
      int i = 0; 

      String phoneNumber = null; 
      String id = null; 
      String name = null; 

      ContentResolver cr = getContentResolver(); 
      Cursor cur = cr.query(data.getData(), null, null, null, null); 

      if (cur.getCount() > 0) { 

       while (cur.moveToNext()) { 
        id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)); 

        name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 

        if (Integer.parseInt(cur.getString(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); 
         numbers = new String[pCur.getCount()]; 
           while (pCur.moveToNext()) { 
            phoneNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
            numbers[i] = phoneNumber; 
            i++;  
           } 
           pCur.close(); 
          }         
        } 

       } 
      cur.close(); 
      AlertDialog.Builder ad=new AlertDialog.Builder(this); 
      ad.setTitle(name); 
      ad.setItems(numbers, new OnClickListener() { 

       public void onClick(DialogInterface dialog, int which) { 

        } 
       }); 
      AlertDialog adb = ad.create(); 
      adb.show(); 
      } 
     } 
+0

没有解释去的代码? – 2012-10-05 08:28:15

相关问题