Android contacts and contactscontract class, contact column 'number' do not exist reason

Source: Internet
Author: User
Tags time in milliseconds

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

1. Add read/write permissions
<Uses-Permission Android: Name = "android. Permission. read_contacts"/>
<Uses-Permission Android: Name = "android. Permission. write_contacts"/>
Contact information URI:
Content: // com. Android. Contacts/contacts
Contact number URI:
Content: // com. Android. Contacts/data/phones
Contact email URI:
Content: // com. Android. Contacts/data/emails
(Recommended) You can also obtain the contact information URI: URI uri = contactscontract. Contacts. content_uri;

2. query and add Contacts (Unit Test Cases)
Public class contacttest extends androidtestcase
{
Private Static final string tag = "contacttest ";
Public void testgetallcontact () throws throwable
{
// Obtain the URI of the contact information
Uri uri = contactscontract. Contacts. content_uri;
// Obtain contentresolver
Contentresolver = This. getcontext (). getcontentresolver ();
// Query data and return cursor
Cursor cursor = contentresolver. Query (Uri, null );
While (cursor. movetonext ())
{
Stringbuilder sb = new stringbuilder ();
// Obtain the contact ID
String contactid = cursor. getstring (cursor. getcolumnindex (contactscontract. Contacts. _ id ));
// Obtain the contact name
String name = cursor. getstring (cursor. getcolumnindex (contactscontract. Contacts. display_name ));
// Construct the contact information
SB. append ("contactid ="). append (contactid). append (", name ="). append (name );
// Query telephone data operations
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); // The actual phone number string is "data1"
// Add phone information
SB. append (", phone ="). append (phonenumber );
}
Phones. Close ();
// Query email-type data operations
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 ));
// Add email information
SB. append (", email ="). append (emailaddress );
}
Emails. Close ();
Log. I (TAG, SB. tostring ());
}
Cursor. Close ();
}
Public void testinsert ()
{
Contentvalues values = new contentvalues ();
// First, execute a null value insert to rawcontacts. content_uri to obtain rawcontactid returned by the system.
Uri rawcontacturi = This. getcontext (). getcontentresolver (). insert (rawcontacts. content_uri, values );
// Obtain the ID
Long rawcontactid = contenturis. parseid (rawcontacturi );
// Input name data to the data table
Values. Clear ();
Values. Put (data. raw_contact_id, rawcontactid); // Add an ID
Values. Put (data. mimetype, structuredname. content_item_type); // Add content type (mimetype)
Values. Put (structuredname. given_name, "Kaifeng from South"); // Add the name to the first name position.
This. getcontext (). getcontentresolver (). insert (Android. provider. contactscontract. Data. content_uri, values );
// Input telephone data to the data table
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 );
// Input email data to the data table
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
{
// Official Document location: Reference/Android/provider/contactscontract.rawcontacts.html
// Create an arraylist to store batch Parameters
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 ());
// Official Document location: Reference/Android/provider/contactscontract.data.html
// Withvaluebackreference the ID of the preceding contact
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, "James ")
. 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, "mobile phone number ")
. 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 ());
}
}
}*************************************** **************************************** ************************
Here we mainly use
Contactscontract class
Since Android 2.0 SDK, the class related to the contact provider has changed to contactscontract, although the old android. provider. contacts can be used, but the method marked as deprecated In the SDK will be abandoned and not recommended, and Android is added since Android 2.0 and API Level 5. provider. contactscontract to replace the original method. However, android123 indicates that you have made preparations. After all, currently, 70% of devices and ophone 1.0 and 1.5 do not support contactscontract.
All fields in contactscontract. Contacts
Contactscontract. Contracts implements four interfaces and inherits different fields from the four interfaces. A total of 23 interfaces are as follows:
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
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 shocould 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 shoshould always be sent to voicemail. ifmissing, defaults to false.

You can use the following method to list all fields in contactscontract. contacts:
 
Privatevoidlistcolumnnames ()
{
Private URI contacturi = contactscontract. Contacts. content_uri;
Contentresolver resolver = This. getcontentresolver ();
Cursor cursor = resolver. Query (contacturi, 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 ();
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.