【问题标题】:How can I dump ALL of a contact's phone numbers?如何转储联系人的所有电话号码?
【发布时间】:2013-02-13 15:11:22
【问题描述】:

我想不通...我使用的是 Android 2.1 SDK

错误: *这将转储所有联系人的电话号码除了自定义标签电话号码...*

如何让它转储具有自定义标签的电话号码?

因此,例如,我的 1 个联系人有 3 个电话号码... 2 个带有自定义标签.. 所以对于该联系人,只有 1 个电话号码将被转储到日志中。

运行,只需从任何 Activity 调用 DumpContacts.readContacts(this);

package com.abc.debug;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.util.Log;

public class DumpContacts {
        private static final String TAG = "Dump Contacts";

        static 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();
        }

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

}

http://developer.android.com/reference/android/provider/ContactsContract.CommonDataKinds.Phone.html

【问题讨论】:

  • 您的示例代码可以很好地为我转储自定义电话号码。值得我使用的是 Android 2.3.7,CyanogenMod-7.1.0-Desire(在 HTC Desire 上)。
  • 很有趣...我只是在手机上尝试过...它们都可以工作...但是它们都像您一样有姜饼。所以要么是虚拟机的问题(可疑),要么是 Android 2.1(更有可能)(我的虚拟机运行 2.1)。好吧,至少我知道不是每个使用我的应用程序的人都会遇到这个问题。

标签: android


【解决方案1】:

试试这个。我已经测试了这段代码。

Uri uri = ContactsContract.Contacts.CONTENT_URI;
    ContentResolver cr = getContentResolver();
    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor cur=cr.query(uri, null, null, null, sortOrder);
    if(cur.getCount()>0){
    while(cur.moveToNext()){


        if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)))> 0) {
//get the phone number
                Cursor phoneCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                                  ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id}, null);
                while (phoneCur.moveToNext()) {
               String phoneNumber= phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                int phonetype = phoneCur.getInt(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                String customLabel = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL));
               String phoneLabel = (String) ContactsContract.CommonDataKinds.Email.getTypeLabel(this.getResources(), phonetype, customLabel);                       
                Log.e(TAG, "Phone Number: " + phoneNumber + " Selected Phone Label: " + phoneLabel);
                                    }phoneCur.close();
}              
        }
    } cur.close();  

【讨论】:

    【解决方案2】:

    试试这个

        static 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);
            if (cc.moveToFirst()) {
            do
            {
                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();
                }
            } while (cc.moveToNext());
            }cc.close();
        }
    

    【讨论】:

    • moveToFirst 或 getCount 或任何其他方法调用是游标初始化所必需的,因为 resolver.query 仅在内部构造游标查询,它是游标上的第一个操作调用,例如 moveToFirst 或 getCount获取数据
    【解决方案3】:

    我会先创建几个帮助类:

    /**
    * Represents a RawContact (NOTE: This is not the same as a Contact)
    * Think of this as an outer join of the raw_contacts table with the data table
    */
    public class RawContact {
        public String Name;
        public int ContactId;
        public List<PhoneNumber> Phones;
    }
    
    /**
    * Represents a high level PhoneNumber from the phone table
    */
    public class PhoneNumber {
        public String Number;
        public int Type;
        public String Label;
    }
    

    这些允许我们存储我们正在寻找的相关信息。然后,我会使用android.provider.ContactsContract.RawContactsEntity 来查找所有原始联系人及其电话号码。注意:一个联系人可以有多个 RawContact,因此请进行相应的计划(此示例未考虑到这一点)。

    package com.contactsample.android;
    
    import android.content.ContentResolver;
    import android.content.Context;
    import android.database.Cursor;
    import android.provider.ContactsContract;
    import android.provider.ContactsContract.Contacts;
    import android.provider.ContactsContract.RawContactsEntity;
    import android.provider.ContactsContract.CommonDataKinds.Phone;
    import android.provider.ContactsContract.CommonDataKinds.StructuredName;
    import android.provider.ContactsContract.Data;
    import android.net.Uri;
    import android.util.Log;
    
    import java.util.List;
    import java.util.ArrayList;
    
    public class DumpContacts {
    
        public static final String TAG = "DumpContacts";
    
        public static void readContacts(Context context) {
            ContentResolver resolver = context.getContentResolver();
            String[] projection = new String[] {
                RawContactsEntity._ID, // this is the Raw Contacts ID
                RawContactsEntity.CONTACT_ID,
                RawContactsEntity.DATA_ID, // the data id
                Data.MIMETYPE,
                Data.DATA1,
                Data.DATA2,
                Data.DATA3,
                Data.DATA4,
                Data.DATA5
            };
            String selection = Data.MIMETYPE + " = ? OR " + Data.MIMETYPE + " = ?";
            String[] selectionArgs = new String[] {
                Phone.CONTENT_ITEM_TYPE,
                StructuredName.CONTENT_ITEM_TYPE
            };
            Uri uri = RawContactsEntity.CONTENT_URI;
            Log.v(TAG, "Fetching from " + uri);
            Cursor cursor = resolver.query(uri, projection, selection, selectionArgs, null);
            int oldContactId = -1;
            RawContact current = null;
            List<RawContact> contacts = new ArrayList<RawContact>();
            try {
                while (cursor.moveToNext()) {
                    final int rawContactId = cursor.getInt(0);
                    final int contactId = cursor.getInt(1);
                    final int dataId = cursor.getInt(2);
                    final String mimeType = cursor.getString(3);
                    if (oldContactId != contactId) {
                        current = new RawContact();
                        current.ContactId = contactId;
                        oldContactId = contactId;
                        contacts.add(current);
                    }
                    Log.v(TAG, "ContactID: " + contactId);
                    if (mimeType.equals(StructuredName.CONTENT_ITEM_TYPE)) {
                        current.Name = cursor.getString(4);
                        Log.v(TAG, "Name: " + current.Name);
                    } else if (mimeType.equals(Phone.CONTENT_ITEM_TYPE)) {
                        if (current.Phones == null) {
                            current.Phones = new ArrayList<PhoneNumber>();
                        }
                        PhoneNumber phone = new PhoneNumber();
                        phone.Number = cursor.getString(4);
                        phone.Type = cursor.getInt(5);
                        phone.Label = cursor.getString(6);
                        current.Phones.add(phone);
                        Log.v(TAG, "Number: " + phone.Number + " Type: " + phone.Type + " Label: " + phone.Label);
                    }
                }
            } finally {
                cursor.close();
            }
            Log.v(TAG, "Found " + contacts.size() + " contacts");
        }
    }
    

    这适用于我在使用 Android 2.1 AVD 的模拟器上。

    它的作用是获取每个 RawContact 的数据行,其中只有 PhoneStructuredName mimetypes(因为这就是我们所关心的)。对于每个 RawContact(我们关心的),我们将至少获得 1 行。一行将是StructuredNamePhone 行,这就是我们在循环结果时检查mimetype 的原因以及我们检查联系人ID 的原因(以查看我们是否仍在读取当前联系人的数据)。

    如果 RawContact 同时具有 PhoneStructuredName,那么我们将为 RawContact 具有的 Phone 的每个 StructuredNamen 获得 1 行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-29
      • 1970-01-01
      • 2014-04-22
      • 1970-01-01
      • 2011-01-22
      相关资源
      最近更新 更多