Android's mobile address contacts app

Source: Internet
Author: User

Android development about mobile phone contacts: Add contacts, delete contacts, find contacts, etc., on the Internet to find some information, as follows:

public class Lianxiren {
Context context;
Public Lianxiren (Context context) {
This.context = context;
}
/**
* Add designated Contacts
* @param fulname
* @param Tel
* @return
*/
Protected Boolean Addlinkman (String fulname,string Tel) {
Uri uri = uri.parse ("content://com.android.contacts/raw_contacts");
Contentresolver resolver = Context.getcontentresolver ();
Contentvalues values = new Contentvalues ();
Long ContactID = Contenturis.parseid (Resolver.insert (URI, values));
URI = Uri.parse ("Content://com.android.contacts/data");
Add Name
Values.put ("raw_contact_id", ContactID);
Values.put (Data.mimetype, "vnd.android.cursor.item/name");
Values.put ("Data1", fulname);
Resolver.insert (URI, values);
Values.clear ();

Add phone
Values.put ("raw_contact_id", ContactID);
Values.put (Data.mimetype, "vnd.android.cursor.item/phone_v2");
Values.put ("Data1", tel);
Resolver.insert (URI, values);
Values.clear ();
return true;

}
Protected String Alllinkman () {
StringBuilder Sbbuilder = new StringBuilder ();
Contentresolver resolver = Context.getcontentresolver ();
cursor cursor = resolver.query (ContactsContract.Contacts.CONTENT_URI, NULL,
NULL, NULL, NULL);
if (Cursor.movetofirst ()) {
String contactId =cursor.getstring (cursor
. Getcolumnindex (contactscontract.contacts._id));
String name = cursor
. getString (cursor
. Getcolumnindex (ContactsContract.Contacts.DISPLAY_NAME));
if (sbbuilder.length () = = 0) {
Sbbuilder.append (name);
}
else{
Sbbuilder.append ("\ n" +name);
}
Cursor phones = ((contentresolver) cursor). 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));
Add Phone Info
Sbbuilder.append ("\ T"). Append (PhoneNumber);

}
Phones.close ();

}
while (Cursor.movetonext ());
Cursor.close ();
return sbbuilder.tostring ();

}
Protected Boolean Deletelinkman (String fulname,string Tel) {
Uri uri = uri.parse ("content://com.android.contacts/raw_contacts");
Contentresolver resolver =context.getcontentresolver ();
cursor cursor = resolver.query (URI, new string[]{data._id}, "Display_name=?", New String[]{fulname}, NULL);
if (Cursor.movetofirst ()) {
int id = cursor.getint (0);
Resolver.delete (URI, "Display_name=?", New String[]{fulname});
URI = Uri.parse ("Content://com.android.contacts/data");
Resolver.delete (URI, "Raw_contact_id=?", New String[]{id + ""});
}

return true;
}
/**
* Find phone number, return contact name
* @throws Exception
*/
Public String Testcontactnamebynumber () throws exception{
String number = "18381668317";
String name = NULL;
Uri uri = uri.parse ("content://com.android.contacts/data/phones/filter/" +number);
Contentresolver resolver =context.getcontentresolver ();
cursor cursor = resolver.query (URI, new string[]{android.provider.contactscontract.data.display_name}, NULL, NULL, NULL);
if (Cursor.movetofirst ()) {
Name = cursor.getstring (0);
LOG.I ("name", name);
}
Cursor.close ();
return name;
}


}

Recommended Blog: http://www.2cto.com/kf/201409/336307.html

Android's mobile address contacts app

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.