2012-02-18 103 views
2

我不能算出这个...我采用了Android 2.1 SDK如何转储联系人的所有电话号码?

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

我怎样才能得到它转储有自定义标签的电话号码呢?

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

运行,只需从任何活动调用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

+0

您的示例代码为我转储自定义电话号码就好了。为什么它值得我使用Android 2.3.7,CyanogenMod-7.1.0-Desire(在HTC Desire上)。 – 2012-02-26 10:42:20

+0

有趣......我只是在手机上试了一下......他们都工作......但他们都有像你一样的姜饼。所以无论是VM(可疑)还是Android 2.1(更可能)(我的VM运行2.1)都有问题。至少我知道不是每个使用我的应用程序的人都会遇到这个问题。 – ycomp 2012-02-27 02:16:06

+0

2.1的错误?决不! ;) – 2012-02-27 02:35:48

回答

0

与此

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

我相信moveToFirst()从我读的地方不是必需的... – ycomp 2012-02-27 02:28:26

+0

moveToFirst或getCount或任何其他方法调用是必要的,因为游标需要初始化为resolver.query只在内部构造游标查询,它是第一次操作调用像moveToFirst或getCount这样的实际获取数据的游标 – iago 2012-02-28 14:52:18

0

尝试我会首先创建了几个辅助类:

/** 
* 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查找所有原始联系人及其电话号码。注意:一个联系人可以有多个RawContacts,以便相应地进行规划(本示例不考虑这一点)。

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那么我们将获得1排为StructuredNamen行针对每个Phone的的RawContact了。

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(); 
相关问题