Android contact development-Save the contact

Source: Internet
Author: User

 

Recently, the contacts who are developing the Android platform are summarized and shared with you:

 

Parameters:

String name, contact name

String homephone, home phone number

String mobile, mobile phone

String workphone, company phone number

String workfax, Company Fax

String emailaddr, email address

String MSN,
String Skype,

String QQ,

String company, company name

String position, position

String homeaddr, home address

String companyaddr company address

 

When saving contacts, Android should pay special attention to the input formats of phones and mobile phones and the primary private attributes when adding IM.

1) Mobile Phone format: 1-390-123-1122. numbers starting with verification 1 are considered to be displayed in the mobile phone format.

2) telephone and fax formats: 010-123-11111. All numbers not starting with 1 are considered to be displayed in the phone format.

3) add values. Put (contactmethods. isprimary, 0) to im );

 

Let's take a look at the code I wrote:

 

Contentvalues values = new contentvalues ();
Values. Put (people. Name, name );
Values. Put (contacts. People. starred, 0); // very important

Uri newperson = contacts. People. createpersoninmycontactsgroup (getcontentresolver (), values );

 

 

 

Uri numberuri = NULL;
If (! Ifempty (homephone )){
Values. Clear ();
Numberuri = URI. withappendedpath (newperson, people. Phones. content_directory );
Values. Put (contacts. Phones. type, people. Phones. type_home );
Values. Put (people. Number, homephone );
Getcontentresolver (). insert (numberuri, values );
}

If (! Ifempty (mobile )){
Values. Clear ();
Numberuri = URI. withappendedpath (newperson, people. Phones. content_directory );
Values. Put (contacts. Phones. type, people. Phones. type_mobile );
Values. Put (people. Number, mobile );
Getcontentresolver (). insert (numberuri, values );

}

If (! Ifempty (workphone )){
Values. Clear ();
Numberuri = URI. withappendedpath (newperson, people. Phones. content_directory );
Values. Put (contacts. Phones. type, people. Phones. type_work );
Values. Put (people. Number, workphone );
Getcontentresolver (). insert (numberuri, values );
}

 

If (! Ifempty (workfax )){
Values. Clear ();
Numberuri = URI. withappendedpath (newperson, people. Phones. content_directory );
Values. Put (contacts. Phones. type, people. Phones. type_fax_work );
Values. Put (people. Number, workfax );
Getcontentresolver (). insert (numberuri, values );
}

If (! Ifempty (emailaddr )){
Values. Clear ();
Numberuri = URI. withappendedpath (newperson, people. contactmethods. content_directory );
Values. Put (contacts. contactmethods. Kind, contacts. kind_email );
Values. Put (contacts. contactmethods. Data, emailaddr );
Values. Put (contacts. contactmethods. type, contacts. contactmethods. type_home );
Getcontentresolver (). insert (numberuri, values );
}

// Add im tools
If (! Ifempty (MSN )){
Values. Clear ();
Numberuri = URI. withappendedpath (newperson, people. contactmethods. content_directory );
Values. Put (contacts. contactmethods. Kind, contacts. kind_im );
Values. Put (contactmethods. type, contactmethods. type_other );
Values. Put (contacts. contactmethods. Data, MSN );

Values. Put (contactmethods. aux_data, contactmethods. encodepredefinedimprotocol (contactmethods. protocol_msn ));
Values. Put (contactmethods. isprimary, 0); // important; otherwise, an error occurs when adding
Getcontentresolver (). insert (numberuri, values );
}

If (! Ifempty (Skype )){
Values. Clear ();
Numberuri = URI. withappendedpath (newperson, people. contactmethods. content_directory );
Values. Put (contacts. contactmethods. Kind, contacts. kind_im );
Values. Put (contactmethods. type, contactmethods. type_other );
Values. Put (contacts. contactmethods. Data, Skype );

Values. Put (contactmethods. aux_data, contactmethods. encodepredefinedimprotocol (contactmethods. protocol_skype ));
Values. Put (contactmethods. isprimary, 0); // important; otherwise, an error occurs when adding
Getcontentresolver (). insert (numberuri, values );
}

If (! Ifempty (qq )){
Values. Clear ();
Numberuri = URI. withappendedpath (newperson, people. contactmethods. content_directory );
Values. Put (contactmethods. Kind, contacts. kind_im );
Values. Put (contactmethods. type, contactmethods. type_other );
Values. Put (contacts. contactmethods. Data, QQ );

Values. Put (contactmethods. aux_data, contactmethods. encodepredefinedimprotocol (contactmethods. protocol_qq ));
Values. Put (contactmethods. isprimary, 0); // important; otherwise, an error occurs when adding

Getcontentresolver (). insert (numberuri, values );
}

// Add company and title
If (! Ifempty (company) |! Ifempty (position )){
Values. Clear ();
Numberuri = URI. withappendedpath (newperson, contacts. Organizations. content_directory );
If (! Ifempty (company )){
Values. Put (contacts. Organizations. Company, company );
}
If (! Ifempty (position )){
Values. Put (contacts. Organizations. Title, position );
}
Values. Put (contacts. Organizations. type, contacts. Organizations. type_work );
Getcontentresolver (). insert (numberuri, values );
}

// Add address
If (! Ifempty (homeaddr )){
Values. Clear ();
Numberuri = URI. withappendedpath (newperson, people. contactmethods. content_directory );
Values. Put (contacts. contactmethods. Kind, contacts. kind_postal );
Values. Put (contacts. contactmethods. type, contacts. contactmethods. type_home );
Values. Put (contacts. contactmethods. Data, homeaddr );
Getcontentresolver (). insert (numberuri, values );
}

If (! Ifempty (companyaddr )){
Values. Clear ();
Numberuri = URI. withappendedpath (newperson, people. contactmethods. content_directory );
Values. Put (contacts. contactmethods. Kind, contacts. kind_postal );
Values. Put (contacts. contactmethods. type, contacts. contactmethods. type_work );
Values. Put (contacts. contactmethods. Data, companyaddr );
Getcontentresolver (). insert (numberuri, values );
}

 

 

-------------------------------

 

Private Boolean ifempty (string input ){
If (input = NULL | input. Length () = 0 | "". Equals (input )){
Return true;
} Else {
If ("". Equals (input. Trim ())){
Return true;
}
Return false;
}
}

 

-------------------------------------

Phone number Formatting Function

Private string formatphonenumber (string input ){
If (input. startswith ("1 ")){
If (input. Length () = 1 ){
Return input;
} Else if (input. Length ()> 1 & input. Length () <5 ){
Return input. substring (0, 1) + "-" + input. substring (1, input. Length ());
} Else if (input. Length ()> = 5 & input. Length () <8 ){
Return input. substring (0, 1) + "-" + input. substring (1, 4) + "-" + input. substring (4, input. Length ());
} Else if (input. Length ()> = 8 ){
Return input. substring (0, 1) + "-" + input. substring (1, 4) + "-" + input. substring (4, 7) + "-" + input. substring (7, input. length ());
}
} Else {
If (input. Length () <= 3 ){
Return input;
} Else if (input. Length ()> 3 & input. Length () <7 ){
Return input. substring (0, 3) + "-" + input. substring (3, input. Length ());
} Else if (input. Length ()> = 7 ){
Return input. substring (0, 3) + "-" + input. substring (3, 6) + "-" + input. substring (6, input. Length ());
}
}
Return "";
}

 

 

 

You are welcome to discuss the source code of the contact section of the android simulator. There is another way:

Http://gitorious.org/0xdroid/packages_apps_contacts

You need to use the GIT tool to download it!

 

 

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.