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