Operate the Android mobile phone Address Book Database

Source: Internet
Author: User

// Obtain the contact information entity by ID
Private Static encontactbackup getencontactbackup (string ID ){
Contentresolver resolver = acapplication. getinstance ()
. Getcontentresolver ();
Encontactbackup contact = new encontactbackup ();
Contact. setid (ID );
String value = NULL;
// Obtain the nickname
Cursor cur = resolver. Query (contactscontract. Data. content_uri,
New String [] {nickname. name },
Contactscontract. Data. contact_id + "=? And"
+ Contactscontract. Data. mimetype + "=? ", New string [] {
ID, nickname. content_item_type}, null );
While (cur. movetonext ()){
Contact. setnickname (cur. getstring (0). replaceall ("#","#")
. Replaceall (","). replaceall (";",";")
. Replaceall (":",":"));
}
Cur. Close ();
// Obtain the name sequence string
Cursor namecur = resolver. Query (contactscontract. Data. content_uri,
New String [] {contactscontract. Data. data4,
Contactscontract. Data. data3,
Contactscontract. Data. data2,
Contactscontract. Data. data5,
Contactscontract. Data. data6 },
Contactscontract. Data. contact_id + "=? And"
+ Contactscontract. Data. mimetype + "=? ", New string [] {
ID, structuredname. content_item_type}, null );
While (namecur. movetonext ()){
Value = namecur. getstring (0 );
Contact. setprefix (value! = NULL? Value. replaceall ("#","#")
. Replaceall (","). replaceall (";",";")
. Replaceall (":", ":"). Replace ("", ""): value );
Value = namecur. getstring (1 );
Contact. setfamilyname (value! = NULL? Value. replaceall ("#","#")
. Replaceall (","). replaceall (";",";")
. Replaceall (":", ":"). Replace ("", ""): value );
Value = namecur. getstring (2 );
Contact. setgivenname (value! = NULL? Value. replaceall ("#","#")
. Replaceall (","). replaceall (";",";")
. Replaceall (":", ":"). Replace ("", ""): value );
Value = namecur. getstring (3 );
Contact. setcentername (value! = NULL? Value. replaceall ("#","#")
. Replaceall (","). replaceall (";",";")
. Replaceall (":", ":"). Replace ("", ""): value );
Value = namecur. getstring (4 );
Contact. setsuffix (value! = NULL? Value. replaceall ("#","#")
. Replaceall (","). replaceall (";",";")
. Replaceall (":", ":"). Replace ("", ""): value );
}
Namecur. Close ();
// Query the address book number by ID
Cursor phonecursor = resolver. Query (
Contactscontract. commondatakinds. Phone. content_uri,
New String [] {contactscontract. commondatakinds. Phone. Number,
Contactscontract. commondatakinds. Phone. Type },
Contactscontract. commondatakinds. Phone. contact_id + "=? ",
New String [] {ID}, null );
System. Out. println ("phone count" + phonecursor. getcount ());
While (phonecursor. movetonext ()){
Int type = phonecursor. getint (1 );
Value = phonecursor. getstring (0 );
Switch (type ){
Case phone. type_home:
Contact. sethometel (value! = NULL? Value. replaceall ("#","#")
. Replaceall (","). replaceall (";",";")
. Replaceall (":", ":"). Replace ("", ""): value );
Break;
Case phone. type_mobile:
Contact. setusephone (value! = NULL? Value. replaceall ("#","#")
. Replaceall (","). replaceall (";",";")
. Replaceall (":", ":"). Replace ("", ""): value );
Break;
Case phone. type_work:
Contact. setofficetel (value! = NULL? Value. replaceall ("#","#")
. Replaceall (","). replaceall (";",";")
. Replaceall (":", ":"). Replace ("", ""): value );
Break;
Case phone. type_fax_home:
Contact. sethomefax (value! = NULL? Value. replaceall ("#","#")
. Replaceall (","). replaceall (";",";")
. Replaceall (":", ":"). Replace ("", ""): value );
Break;
Case phone. type_fax_work:
Contact. setofficefax (value! = NULL? Value. replaceall ("#","#")
. Replaceall (","). replaceall (";",";")
. Replaceall (":", ":"). Replace ("", ""): value );
Break;
}
}
Phonecursor. Close (); // close the cursor
// Obtain the QQ number
Cursor imcur = resolver. Query (contactscontract. Data. content_uri,
New String [] {im. Data, Im. Protocol },
Contactscontract. Data. contact_id + "=? And"
+ Contactscontract. Data. mimetype + "=? ", New string [] {
ID, Im. content_item_type}, null );
While (imcur. movetonext ()){
Int type = imcur. getint (1 );
Value = imcur. getstring (0 );
Switch (type ){
Case im. protocol_qq: // match QQ
Contact. setqqnum (value! = NULL? Value. replaceall ("#","#")
. Replaceall (","). replaceall (";",";")
. Replaceall (":", ":"). Replace ("", ""): value );
Break;
}
}
Imcur. Close ();
// Obtain the position and Company
Cursor orgcur = resolver. Query (contactscontract. Data. content_uri,
New String [] {organization. Company, organization. title,
Organization. Type}, contactscontract. Data. contact_id
+ "=? And "+ contactscontract. Data. mimetype +" =? ",
New String [] {ID, organization. content_item_type}, null );
While (orgcur. movetonext ()){
Int type = orgcur. getint (2 );
Switch (type ){
Case organization. type_work:
Value = orgcur. getstring (0 );
Contact. setcompany (value! = NULL? Value. replaceall ("#","#")
. Replaceall (","). replaceall (";",";")
. Replaceall (":", ":"). Replace ("", ""): value );
Value = orgcur. getstring (1 );
Contact. setpisition (value! = NULL? Value. replaceall ("#","#")
. Replaceall (","). replaceall (";",";")
. Replaceall (":", ":"). Replace ("", ""): value );
Break;
}
}
Orgcur. Close ();
// Get remarks
Cursor notecur = resolver. Query (contactscontract. Data. content_uri,
New String [] {Note. Note}, contactscontract. Data. contact_id
+ "=? And "+ contactscontract. Data. mimetype +" =? ",
New String [] {ID, note. content_item_type}, null );
While (notecur. movetonext ()){
Value = notecur. getstring (0 );
Contact. setnote (value! = NULL? Value. replaceall ("#","#")
. Replaceall (","). replaceall (";",";")
. Replaceall (":", ":"). Replace ("", ""): value );
}
Notecur. Close ();
// Obtain email information
Cursor emailcur = resolver. Query (
Contactscontract. commondatakinds. Email. content_uri,
New String [] {email. data1, email. Type },
Contactscontract. commondatakinds. Email. contact_id + "=? And"
+ Contactscontract. commondatakinds. Email. mimetype
+ "=? ", New string [] {ID, email. content_item_type },
Null );
While (emailcur. movetonext ()){
Int type = emailcur. getint (1 );
Value = emailcur. getstring (0 );
Switch (type ){
Case email. type_home:
Contact. sethomemailbox (value! = NULL? Value
. Replaceall ("#", "#"). replaceall (",",",")
. Replaceall (";", ";"). replaceall (":",":")
. Replace ("", ""): value );
Break;
Case email. type_work:
Contact. setofficemailbox (value! = NULL? Value
. Replaceall ("#", "#"). replaceall (",",",")
. Replaceall (";", ";"). replaceall (":",":")
. Replace ("", ""): value );
Break;
Case email. type_other:
Contact. setelecmailbox (value! = NULL? Value
. Replaceall ("#", "#"). replaceall (",",",")
. Replaceall (";", ";"). replaceall (":",":")
. Replace ("", ""): value );
Break;
}
}
Emailcur. Close ();
// Obtain the URL
Cursor webcur = resolver. Query (contactscontract. Data. content_uri,
New String [] {website. url, website. Type },
Contactscontract. commondatakinds. Website. contact_id + "=? And"
+ Contactscontract. commondatakinds. Website. mimetype
+ "=? ", New string [] {ID, website. content_item_type },
Null );
While (webcur. movetonext ()){
Int type = webcur. getint (1 );
Switch (type ){
Case website. type_other:
Value = webcur. getstring (0 );
Contact. setpersonweb (value! = NULL? Value. replaceall ("#","#")
. Replaceall (","). replaceall (";",";")
. Replaceall (":", ":"). Replace ("", ""): value );
Break;
}
}
Webcur. Close ();
Return contact;
}

 

 

 

// Restore the address book and add a contact
Public static long updatecontact (encontactbackup contact ){
// Obtain the query Analyzer
Contentvalues values = new contentvalues ();
Contentresolver res = acapplication. getinstance (). getcontentresolver ();
String value = NULL;
// Insert the address book
Uri rawcontacturi = res. insert (
Contactscontract. rawcontacts. content_uri, values );
Long rawcontactid = contenturis. parseid (rawcontacturi );
Values. Clear ();
Value = contact. getnickname ();
If (value! = NULL &&! Value. Equals ("")&&! Value. Equals ("")){
// Set the nickname
Values. Put (contactscontract. Data. raw_contact_id, rawcontactid );
Values. Put (nickname. Name, value );
Values. Put (data. mimetype, nickname. content_item_type );
Res. insert (contactscontract. Data. content_uri, values );
Values. Clear ();
}

// Set the name information
Values. Put (contactscontract. Data. raw_contact_id, rawcontactid );
Values. Put (data. mimetype, structuredname. content_item_type );
Value = contact. getprefix ();
If (value! = NULL &&! Value. Equals ("")&&! Value. Equals ("")){
Values. Put (structuredname. prefix, value );
}
Value = contact. getfamilyname ();
If (value! = NULL &&! Value. Equals ("")&&! Value. Equals ("")){
Values. Put (structuredname. family_name, value );
}
Value = contact. getgivenname ();
If (value! = NULL &&! Value. Equals ("")&&! Value. Equals ("")){
Values. Put (structuredname. given_name, value );
}
Value = contact. getcentername ();
If (value! = NULL &&! Value. Equals ("")&&! Value. Equals ("")){
Values. Put (structuredname. middle_name, value );
}
Value = contact. getsuffix ();
If (value! = NULL &&! Value. Equals ("")&&! Value. Equals ("")){
Values. Put (structuredname. suffix, value );
}
Res. insert (contactscontract. Data. content_uri, values );
Values. Clear ();

// Add a mobile phone number
Value = contact. getusephone ();
If (value! = NULL &&! Value. Equals ("")){
Values. Put (contactscontract. Data. raw_contact_id, rawcontactid );
Values. Put (data. mimetype, phone. content_item_type );
Values. Put (contactscontract. commondatakinds. Phone. Number, value );
Values. Put (contactscontract. commondatakinds. Phone. type,
Phone. type_mobile );
Res. insert (contactscontract. Data. content_uri, values );
Values. Clear ();
}

// Add a home number
Value = contact. gethometel ();
If (value! = NULL &&! Value. Equals ("")){
Values. Put (contactscontract. Data. raw_contact_id, rawcontactid );
Values. Put (contactscontract. commondatakinds. Phone. Number, value );
Values. Put (data. mimetype, phone. content_item_type );
Values. Put (contactscontract. commondatakinds. Phone. type,
Phone. type_home );
Res. insert (contactscontract. Data. content_uri, values );
Values. Clear ();
}
// Add the Organization number
Value = contact. getofficetel ();
If (value! = NULL &&! Value. Equals ("")){
Values. Put (contactscontract. Data. raw_contact_id, rawcontactid );
Values. Put (data. mimetype, phone. content_item_type );
Values. Put (contactscontract. commondatakinds. Phone. Number, value );
Values. Put (contactscontract. commondatakinds. Phone. type,
Phone. type_work );
Res. insert (contactscontract. Data. content_uri, values );
Values. Clear ();
}
// Add a home fax
Value = contact. gethomefax ();
If (value! = NULL &&! Value. Equals ("")){
Values. Put (contactscontract. Data. raw_contact_id, rawcontactid );
Values. Put (contactscontract. commondatakinds. Phone. Number, value );
Values. Put (data. mimetype, phone. content_item_type );
Values. Put (contactscontract. commondatakinds. Phone. type,
Phone. type_fax_home );
Res. insert (contactscontract. Data. content_uri, values );
Values. Clear ();
}
// Add an office fax
Value = contact. getofficefax ();
If (value! = NULL &&! Value. Equals ("")){
Values. Put (contactscontract. Data. raw_contact_id, rawcontactid );
Values. Put (contactscontract. commondatakinds. Phone. Number, value );
Values. Put (data. mimetype, phone. content_item_type );
Values. Put (contactscontract. commondatakinds. Phone. type,
Phone. type_fax_work );
Res. insert (contactscontract. Data. content_uri, values );
Values. Clear ();
}

// Add a QQ number
Value = contact. getqqnum ();
If (value! = NULL &&! Value. Equals ("")){
Values. Put (contactscontract. Data. raw_contact_id, rawcontactid );
Values. Put (contactscontract. Data. mimetype, Im. content_item_type );
Values. Put (IM. Data, value );
Values. Put (IM. Protocol, Im. protocol_qq );
Res. insert (contactscontract. Data. content_uri, values );
Values. Clear ();
}
// Add company and Position
If (contact. getcompany ()! = NULL &&! Contact. getcompany (). Equals (""))
| (Contact. getpisition ()! = NULL &&! Contact. getpisition ()
. Equals (""))){
Values. Put (contactscontract. Data. raw_contact_id, rawcontactid );
Values. Put (contactscontract. Data. mimetype,
Organization. content_item_type );
If (contact. getcompany ()! = NULL
&&! Contact. getcompany (). Equals ("")){
Values. Put (organization. Company, contact. getcompany ());
}
If (contact. getpisition ()! = NULL
|! Contact. getpisition (). Equals ("")){
Values. Put (organization. Title, contact. getpisition ());
}
Values. Put (organization. type, organization. type_work );
Res. insert (contactscontract. Data. content_uri, values );
Values. Clear ();
}
// Add remarks
Value = contact. getnote ();
If (value! = NULL &&! Value. Equals ("")){
Values. Put (contactscontract. Data. raw_contact_id, rawcontactid );
Values. Put (contactscontract. Data. mimetype, note. content_item_type );
Values. Put (note. Note, value );
Res. insert (contactscontract. Data. content_uri, values );
Values. Clear ();
}
// Add an email address
Value = contact. getelecmailbox ();
If (value! = NULL &&! Value. Equals ("")){
Values. Put (contactscontract. commondatakinds. Email. raw_contact_id,
Rawcontactid );
Values. Put (contactscontract. Data. mimetype, email. content_item_type );
Values. Put (email. data1, value );
Values. Put (email. type, email. type_other );
Res. insert (contactscontract. Data. content_uri, values );
Values. Clear ();
}
// Add a home mailbox
Value = contact. gethomemailbox ();
If (value! = NULL &&! Value. Equals ("")){
Values. Put (contactscontract. commondatakinds. Email. raw_contact_id,
Rawcontactid );
Values. Put (contactscontract. Data. mimetype, email. content_item_type );
Values. Put (email. data1, value );
Values. Put (email. type, email. type_home );
Res. insert (contactscontract. Data. content_uri, values );
Values. Clear ();
}
// Add the Organization email address
Value = contact. getofficemailbox ();
If (value! = NULL &&! Value. Equals ("")){
Values. Put (contactscontract. commondatakinds. Email. raw_contact_id,
Rawcontactid );
Values. Put (contactscontract. Data. mimetype, email. content_item_type );
Values. Put (email. data1, value );
Values. Put (email. type, email. type_work );
Res. insert (contactscontract. Data. content_uri, values );
Values. Clear ();
}
// Add a website
Value = contact. getpersonweb ();
If (value! = NULL &&! Value. Equals ("")){
Values. Put (contactscontract. commondatakinds. Website. raw_contact_id,
Rawcontactid );
Values. Put (contactscontract. Data. mimetype,
Website. content_item_type );
Values. Put (website. url, value );
Values. Put (website. type, website. type_other );
Res. insert (contactscontract. Data. content_uri, values );
Values. Clear ();
}
Return rawcontactid;
}

 

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.