Content providers (get contact information and insert contacts)

Source: Internet
Author: User

A. Get contact information

Click to get contact information

public void Click (View view) {
Get Content Parser
Contentresolver resolver = Getcontentresolver ();
Set the URI of the access (System native)
Uri uri = uri.parse ("content://com.android.contacts/raw_contacts");//ID of the contact
Uri Datauri = Uri.parse ("Content://com.android.contacts/data");//Contact information
Query the ID of all contacts
Cursor idcursor = Resolver.query (URI, new string[]{"contact_id"}, NULL, NULL, NULL);
while (Idcursor.movetonext ()) {
String id = idcursor.getstring (0);
Go to data table by ID
Note: When querying the data is actually a view of the query, in the view of the field mimetype_id changed to MimeType, in the query when the attention to change
Cursor Datacurcor = Resolver.query (Datauri, New string[]{"MimeType", "Data1"}, "Raw_contact_id=?", New String[]{id}, NULL);
Contacts C = new contact ();//declare and instantiate a person information object
while (Datacurcor.movetonext ()) {
String type = datacurcor.getstring (0);
String data = datacurcor.getstring (1);

Encapsulates a contact's specific information based on the value of MimeType
if ("Vnd.android.cursor.item/email_v2". Equals (Type))
C.setemail (data);
if ("Vnd.android.cursor.item/im". Equals (Type))
C.SETQQ (data);
if ("Vnd.android.cursor.item/phone_v2". Equals (Type))
C.setphone (data);
if ("Vnd.android.cursor.item/name". Equals (Type))
C.setname (data);
if ("Vnd.android.cursor.item/postal-address_v2". Equals (Type))
C.setaddress (data);
}
Datacurcor.close ();
List.add (c);
}
Idcursor.close ();
Data Encapsulation Complete

Display in ListView

Lv.setadapter (New Myadapter ());
}
Private class Myadapter extends baseadapter{

@Override
public int GetCount () {
return List.size ();
}

@Override
Public Object getItem (int position) {
return null;
}

@Override
public long getitemid (int position) {
return 0;
}

@Override
Public View GetView (int position, View Convertview, ViewGroup parent) {
TextView TV = null;
if (Convertview = = null)
TV = new TextView (mainactivity.this);
Else
TV = (TextView) Convertview;

Tv.settextsize (15);
StringBuilder sb = new StringBuilder ();
Contact C = list.get (position);
Sb.append ("Name:" + c.getname () + "Phone:" + c.getphone () + "Address:" + c.getaddress () + "Email:" + c.getemail () + "QQ:" + c.getqq ()) ;
Tv.settext (Sb.tostring ());
return TV;
}
}

Note: To read contact permissions in the manifest file, configure permissions

two , Insert a contact information

public void Click (View view) {
Get the information
String name = Etname.gettext (). toString ();
String phone = Etphone.gettext (). toString ();
String email = etemail.gettext (). toString ();

if (textutils.isempty (name) | | Textutils.isempty (phone)
|| Textutils.isempty (email)) {
Toast.maketext (This, "All information must be filled in", 0). Show ();
Return
}

/**
* Idea of inserting data:
* 1. Query the number of records in the Raw_contact table
* 2. Add 1 to the number is the ID of the new record.
3. Inserting records into the data table
*/

Get Content Parser
Contentresolver resolver = Getcontentresolver ();
Set the URI of the access
Uri uri = uri.parse ("content://com.android.contacts/raw_contacts");
Uri Datauri = Uri.parse ("Content://com.android.contacts/data");
Query the ID of all contacts//Get the number of all records

Cursor idcursor = Resolver.query (URI, new string[] {"contact_id"},
NULL, NULL, NULL);
int count = Idcursor.getcount ();
Idcursor.close ();
ID of the inserted record
int id = count + 1;
Insert ID to Raw_contact
Contentvalues values = new Contentvalues ();
Values.put ("contact_id", id);
Resolver.insert (URI, values); The ID is inserted into the table raw_contact.

Inserting data into the database table
Insert Name Record
Contentvalues namevalues = new Contentvalues ();
Namevalues.put ("MimeType", "vnd.android.cursor.item/name");
Namevalues.put ("Data1", name);
Namevalues.put ("raw_contact_id", id);
Resolver.insert (Datauri, namevalues);

Insert Phone
Contentvalues phonevalues = new Contentvalues ();
Phonevalues.put ("MimeType", "vnd.android.cursor.item/phone_v2");
Phonevalues.put ("Data1", phone);
Phonevalues.put ("raw_contact_id", id);
Resolver.insert (Datauri, phonevalues);

Insert Mailbox
Contentvalues emailvalues = new Contentvalues ();
Emailvalues.put ("MimeType", "vnd.android.cursor.item/email_v2");
Emailvalues.put ("data1", email);
Emailvalues.put ("raw_contact_id", id);
Resolver.insert (Datauri, emailvalues);

Toast.maketext (This, "Insert succeeded", 0). Show ();
}

This article is from the "Android Notes" blog, so be sure to keep this source http://2585211.blog.51cto.com/10044233/1665223

Content providers (get contact information and insert contacts)

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.