Android Paging query get system contact information

Source: Internet
Author: User

Tag:android    Database paging query     system contact    

Package Com.example.yqqmobilesafe.contactprovider;import Java.util.arraylist;import java.util.list;import android. R.integer;import Android.content.context;import Android.database.cursor;import Android.net.Uri;import Android.provider.contactscontract;import Com.example.yqqmobilesafe.domain.contactinfo;public Class Contactinfoprovider {Private Context Mcontext;public Contactinfoprovider (context context) {Mcontext=context;} /** * Get System Contact information * @return */public list<contactinfo> getsystemcontactinfos () {list<contactinfo> infos=new Arraylist<contactinfo> ();//use Contentresolver to find contact data cursor cursor = mcontext.getcontentresolver (). Query ( ContactsContract.Contacts.CONTENT_URI, NULL, null,null, NULL);//Iterate through the results of the query, get all the contacts in the system while (Cursor.movetonext ()) { ContactInfo info=new ContactInfo ();//Get contact idstring contactId = cursor.getstring (Cursor.getcolumnindex ( contactscontract.contacts._id));//Gets the name of the contact string name = Cursor.getstring (Cursor.getcolumnindex ( ContactsContract.Contacts.DISPLAY_NAME)); Info.setcontactname (name);//Use Contentresolver to find the phone number of the contact cursor phones = mcontext.getcontentresolver (). Query ( Contactscontract.commondatakinds.phone.content_uri,null,contactscontract.commondatakinds.phone.contact_id+ "=" + ContactId, NULL, NULL);//Iterate through the results of the query, get multiple phone numbers for that contact while (Phones.movetonext ()) {//Get the data in the phone number column in the query results. String PhoneNumber = phones.getstring (Phones.getcolumnindex (ContactsContract.CommonDataKinds.Phone.NUMBER)); Info.setphonenumber (PhoneNumber);} Phones.close (); Infos.add (info); info=null;} Cursor.close (); return infos;} /** * Paging Query system Contact information * @param pageSize Maximum number per page * @param currentoffset Current offset * @return */public list<contactinfo> Getco Ntactsbypage (int pagesize,int currentoffset) {list<contactinfo> infos=new arraylist<contactinfo> (); Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; string[] projection = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.DATA1, "Sort_key"}; cursor cursor = Mcontext.getcontentresolVer (). Query (URI, projection, null, NULL, "Sort_key COLLATE localized ASC limit" + pageSize + "offset" + currentoffset); if (cursor! = NULL) {while (Cursor.movetonext ()) {ContactInfo info=new contactinfo (); String contactName = cursor.getstring (0); String PhoneNumber = cursor.getstring (1); Info.setcontactname (contactName); Info.setphonenumber (PhoneNumber); Infos.add (info); info=null;} Cursor.close ();} return infos;} /** * Total number of records for system contacts * @return */public int getallcounts () {int num=0;//use Contentresolver to find contact data cursor cursor = MCONTEXT.G Etcontentresolver (). query (ContactsContract.Contacts.CONTENT_URI, NULL, null,null, NULL);//traverse the query results to get all the contacts in the system while ( Cursor.movetonext ()) {num++;} Cursor.close (); return num;}}

Android Paging query get system contact information

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.