2012-02-18 102 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(); 
相關問題