Android 連絡人 以及 ContactsContract類 ,連絡人column ‘number’ do not exist 原因

來源:互聯網
上載者:User

From:http://blog.sina.com.cn/s/blog_90cdca4c01010zm4.html

1.加入讀寫權限
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
連絡人資訊Uri:
content://com.android.contacts/contacts
連絡人電話Uri:
content://com.android.contacts/data/phones
連絡人Email Uri:
content://com.android.contacts/data/emails 
(推薦)也可以這樣擷取連絡人資訊Uri:Uri uri = ContactsContract.Contacts.CONTENT_URI; 

2.查詢與新增連絡人...的操作(單元測試用例) 
public class ContactTest extends AndroidTestCase
{
private static final String TAG = "ContactTest";
public void testGetAllContact() throws Throwable
{
//擷取連絡人資訊的Uri
Uri uri = ContactsContract.Contacts.CONTENT_URI;
//擷取ContentResolver
ContentResolver contentResolver = this.getContext().getContentResolver();
//查詢資料,返回Cursor
Cursor cursor = contentResolver.query(uri, null, null, null, null);
while(cursor.moveToNext())
{
StringBuilder sb = new StringBuilder();
//擷取連絡人的ID
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
//擷取連絡人的姓名
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
//構造連絡人資訊
sb.append("contactId=").append(contactId).append(",Name=").append(name);
//查詢電話類型的資料操作
Cursor phones = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,
null, null);
while(phones.moveToNext())
{
String phoneNumber = phones.getString(phones.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));                          // 實際phone number字串為“data1”
//添加Phone的資訊
sb.append(",Phone=").append(phoneNumber);
}
phones.close();
//查詢Email類型的資料操作
Cursor emails = contentResolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId,
null, null);
while (emails.moveToNext())
{
String emailAddress = emails.getString(emails.getColumnIndex(
ContactsContract.CommonDataKinds.Email.DATA));
//添加Email的資訊
sb.append(",Email=").append(emailAddress);
}
emails.close();
Log.i(TAG, sb.toString());
}
cursor.close();
}
public void testInsert()
{
ContentValues values = new ContentValues();
//首先向RawContacts.CONTENT_URI執行一個空值插入,目的是擷取系統返回的rawContactId
Uri rawContactUri = this.getContext().getContentResolver().insert(RawContacts.CONTENT_URI, values);
//擷取id
long rawContactId = ContentUris.parseId(rawContactUri);
//往data表入姓名資料
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId); //添加id
values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);//新增內容類型(MIMETYPE)
values.put(StructuredName.GIVEN_NAME, "凱風自南");//添加名字,添加到first name位置
this.getContext().getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);
//往data表入電話資料
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, "13921009789");
values.put(Phone.TYPE, Phone.TYPE_MOBILE);
this.getContext().getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);
//往data表入Email資料
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);
values.put(Email.DATA, "kesenhoo@gmail.com");
values.put(Email.TYPE, Email.TYPE_WORK);
this.getContext().getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);
}
public void testSave() throws Throwable
{
//官方文檔位置:reference/android/provider/ContactsContract.RawContacts.html
//建立一個ArrayList存放批量的參數
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();
ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
.withValue(RawContacts.ACCOUNT_TYPE, null)
.withValue(RawContacts.ACCOUNT_NAME, null)
.build());
//官方文檔位置:reference/android/provider/ContactsContract.Data.html
//withValueBackReference後退引用前面連絡人的id
ops.add(ContentProviderOperation.newInsert(android.provider.ContactsContract.Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
.withValue(StructuredName.GIVEN_NAME, "小明")
.build());
ops.add(ContentProviderOperation.newInsert(android.provider.ContactsContract.Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
.withValue(Phone.NUMBER, "13671323809")
.withValue(Phone.TYPE, Phone.TYPE_MOBILE)
.withValue(Phone.LABEL, "手機號")
.build());
ops.add(ContentProviderOperation.newInsert(android.provider.ContactsContract.Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE)
.withValue(Email.DATA, "kesen@gmail.com")
.withValue(Email.TYPE, Email.TYPE_WORK)
.build());
ContentProviderResult[] results = this.getContext().getContentResolver()
.applyBatch(ContactsContract.AUTHORITY, ops);
for(ContentProviderResult result : results)
{
Log.i(TAG, result.uri.toString());
}
}
}*******************************************************************************************************
這裡主要使用的是
ContactsContract類
從Android 2.0 SDK開始有關連絡人provider的類變成了ContactsContract,雖然老的android.provider.Contacts能用,但是在SDK中標記為為deprecated將被放棄不推薦的方法,而從Android 2.0及API Level為5開始新增了android.provider.ContactsContract來代替原來的方法。不過Android123表示大家做兩手準備,畢竟目前70%的裝置以及Ophone 1.0和1.5均不支援ContactsContract。
ContactsContract.Contacts中的所有欄位
ContactsContract.Contracts實現了4個介面,並從4個介面中,繼承了不同的欄位,一共有23個如下:
1. ContactsContract.Contacts.TIMES_CONTACTED ="times_contacted"
The number of times a contact has been contacted
2. ContactsContract.Contacts.CONTACT_STATUS ="contact_status"
Contact's latest status update.
3. ContactsContract.Contacts.CUSTOM_RINGTONE ="custom_ringtone"
URI for a custom ringtone associated with the contact. Ifnull or missing, the default ringtone is used.
4. ContactsContract.Contacts.HAS_PHONE_NUMBER ="has_phone_number"
An indicator of whether this contact has at least one phonenumber. "1" if there is at least one phone number, "0"otherwise.
5. ContactsContract.Contacts.PHONETIC_NAME = "phonetic_name"
Pronunciation of the full name in the phonetic alphabetspecified by PHONETIC_NAME_STYLE.
6. ContactsContract.Contacts.PHONETIC_NAME_STYLE ="phonetic_name_style"
The phonetic alphabet used to represent the PHONETIC_NAME.See PhoneticNameStyle.
7. ContactsContract.Contacts.CONTACT_STATUS_LABEL ="contact_status_label"
The resource ID of the label describing the source ofcontact status, e.g. "Google Talk". This resource is scoped by theCONTACT_STATUS_RES_PACKAGE.
8. ContactsContract.Contacts.LOOKUP_KEY = "lookup"
An opaque value that contains hints on how to find thecontact if its row id changed as a result of a sync or aggregation.
9. ContactsContract.Contacts.CONTACT_STATUS_ICON ="contact_status_icon"
The resource ID of the icon for the source of contactstatus. This resource is scoped by the
CONTACT_STATUS_RES_PACKAGE.
10. ContactsContract.Contacts.LAST_TIME_CONTACTED= "last_time_contacted"
The last time a contact was contacted.
11. ContactsContract.Contacts.DISPLAY_NAME= "display_name"
The display name for the contact.
12. ContactsContract.Contacts.SORT_KEY_ALTERNATIVE= "sort_key_alt"
Sort key based on the alternative representation of thefull name, DISPLAY_NAME_ALTERNATIVE. Thus for Western names, it is the oneusing the "family name first" format.
13. ContactsContract.Contacts.IN_VISIBLE_GROUP= "in_visible_group"
Lookup value that reflects the GROUP_VISIBLE state of anyContactsContract.CommonDataKinds.GroupMembership for this contact.
14. ContactsContract.Contacts._ID= "_id"
The unique ID for a row.
15. ContactsContract.Contacts.STARRED= "starred"
Is the contact starred?
16. ContactsContract.Contacts.SORT_KEY_PRIMARY= "sort_key"
Sort key that takes into account locale-based traditionsfor sorting names in address books.
17. ContactsContract.Contacts.DISPLAY_NAME_ALTERNATIVE= "display_name_alt"
An alternative representation of the display name, such as"family name first" instead of "given name first" forWestern names. If an alternative is not available, the values should be thesame as DISPLAY_NAME_PRIMARY
18. ContactsContract.Contacts.CONTACT_PRESENCE= "contact_presence"
Contact presence status. See ContactsContract.StatusUpdatesfor individual status definitions.
19. ContactsContract.Contacts.DISPLAY_NAME_SOURCE= "display_name_source"
The kind of data that is used as the display name for thecontact, such as structured name or email address. See DisplayNameSources.TODO: convert DisplayNameSources to a link after it is un-hidden
20. ContactsContract.Contacts.CONTACT_STATUS_RES_PACKAGE= "contact_status_res_package"
The package containing resources for this status: label andicon.
21. ContactsContract.Contacts.CONTACT_STATUS_TIMESTAMP= "contact_status_ts"
The absolute time in milliseconds when the latest statuswas inserted/updated.
22. ContactsContract.Contacts.PHOTO_ID= "photo_id"
Reference to the row in the data table holding the photo.
23. ContactsContract.Contacts.SEND_TO_VOICEMAIL= "send_to_voicemail"
Whether the contact should always be sent to voicemail. Ifmissing, defaults to false.

可以用以下方法,列出ContactsContract.Contacts中的所有欄位:
 
privatevoidlistColumnNames()
{
private Uri contactUri =ContactsContract.Contacts.CONTENT_URI;
ContentResolver resolver = this.getContentResolver();
Cursor cursor =resolver.query(contactUri, null,null, null,null);
int columnNumber = cursor.getColumnCount();
for(int i = 0; i <columnNumber; i++)
{
String temp =cursor.getColumnName(i);
Log.e("listColumnNames","" + i + "\t" + temp);
}
cursor.close();
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.