从android中的联系人中检索电话号码

时间:2011-11-18 12:26:42

标签: android android-manifest

我需要从联系人处获取电话号码,但光标未进入此循环。请帮帮我....

for (phoneCursor.moveToFirst(); !phoneCursor.isAfterLast(); phoneCursor.moveToNext()) {
// Get a phone number
String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));

Toast.makeText(this, "Phone  =  "+phoneNumber, Toast.LENGTH_LONG).show();

3 个答案:

答案 0 :(得分:7)

使用下面的代码,你肯定会得到一个电话号码。

ContentResolver cr = getContentResolver();
        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
            null, null, null);
        if (cursor.moveToFirst()) {
            String contactId =
                cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            //
            //  Get all phone numbers.
            //
            Cursor phones = cr.query(Phone.CONTENT_URI, null,
                Phone.CONTACT_ID + " = " + contactId, null, null);
            while (phones.moveToNext()) {
                String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
                int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
                switch (type) {
                    case Phone.TYPE_HOME:
                       TextView textView=(TextView)findViewById(R.id.textView1);
                       textView.setText(number);
                        break;
                    case Phone.TYPE_MOBILE:
                        // do something with the Mobile number here...
                        break;
                    case Phone.TYPE_WORK:
                        // do something with the Work number here...
                        break;
                    }
            }
            phones.close(); 

答案 1 :(得分:0)

你可以试试这个:

ContentResolver cr = getContentResolver();
    Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
        "DISPLAY_NAME = '" + NAME + "'", null, null);
    if (cursor.moveToFirst()) {
        String contactId =
            cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
        //
        //  Get all phone numbers.
        //
        Cursor phones = cr.query(Phone.CONTENT_URI, null,
            Phone.CONTACT_ID + " = " + contactId, null, null);
        while (phones.moveToNext()) {
            String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
            int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
            switch (type) {
                case Phone.TYPE_HOME:
                    // do something with the Home number here...
                    break;
                case Phone.TYPE_MOBILE:
                    // do something with the Mobile number here...
                    break;
                case Phone.TYPE_WORK:
                    // do something with the Work number here...
                    break;
                }
        }
        phones.close();
}

是的不要忘记在AndroidManifest.xml文件中添加READ_CONTACTS权限

<uses-permission android:name="android.permission.READ_CONTACTS" />

答案 2 :(得分:0)

我使用这样的函数:  (该函数从OnActivityResult中调用,来自Activity类:

@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
      super.onActivityResult(reqCode, resultCode, data);
      switch (reqCode) {
        case (Constants.REQ_CODE_PICK_CONTACT) :
          if (resultCode == Activity.RESULT_OK) {
          savePickedContact(data, this);                
          }
        break;
      }
}


public static void savePickedContact(Intent data, Activity from){
        Uri contactData = data.getData();
        Cursor contactCur =  from.managedQuery(contactData, null, null, null, null);
        if (contactCur.moveToFirst()) {
            for(int i=0;i<contactCur.getColumnCount();i++){
              String columnName = contactCur.getColumnName(i);
              String value = contactCur.getString(i);
          }
            ContentResolver cr = from.getContentResolver();
            Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
                                        null, 
                                        ContactsContract.CommonDataKinds.Email.CONTACT_ID + " =?",  
                                        new String[]{contactCur.getString(contactCur.getColumnIndex("_id"))}, 
                                        ContactsContract.CommonDataKinds.Email.IS_PRIMARY + " DESC");
            Boolean hasData = emailCur.moveToFirst();
            while(hasData){
                String  email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                hasData = emailCur.moveToNext();
                }
            GlobalData.pickedContact.put("email_addresses", emails);
            emailCur.close();

            Cursor phonesCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                                            null, 
                                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " =?", 
                                            new String[]{contactCur.getString(contactCur.getColumnIndex("_id"))}, 
                                            ContactsContract.CommonDataKinds.Phone.IS_PRIMARY + " DESC");
            hasData = phonesCur.moveToFirst();
            while(hasData){
                String phoneNumber = phonesCur.getString(phonesCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
                hasData = phonesCur.moveToNext();
            }
            phonesCur.close();

            Cursor eventCur = cr.query(Data.CONTENT_URI, 
                    new String[]{ ContactsContract.CommonDataKinds.Event.DATA}, 
                    ContactsContract.CommonDataKinds.Event.CONTACT_ID + " =? AND " + ContactsContract.CommonDataKinds.Event.TYPE + " =" + ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY +" AND "+ Data.MIMETYPE + " =?", 
                    new String[]{contactCur.getString(contactCur.getColumnIndex("_id")), ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE}, 
                    null);
            hasData = eventCur.moveToFirst();
            if(hasData){                        
                String birthday = "birthday", Utils.dbDateToDisplayDate(eventCur.getString(0));
            }

            Cursor structNameCur = cr.query(Data.CONTENT_URI, 
                    new String[]{ ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME}, 
                    ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID + " =? AND " + Data.MIMETYPE + " =?", 
                    new String[]{contactCur.getString(contactCur.getColumnIndex("_id")), ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE}, 
                    null);
            hasData = structNameCur.moveToFirst();
            if(hasData){                        
                String givenName = structNameCur.getString(0);
                String familyName = "family_name", structNameCur.getString(1);
            }
            structNameCur.close();
        }
      contactCur.close();
}