android分頁查詢擷取系統連絡人資訊

來源:互聯網
上載者:User

標籤:android   資料庫分頁查詢   系統連絡人   

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;}/** * 擷取系統連絡人資訊 * @return */public  List<ContactInfo> getSystemContactInfos(){List<ContactInfo> infos=new ArrayList<ContactInfo>();// 使用ContentResolver尋找連絡人資料Cursor cursor = mContext.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null,null, null);// 遍曆查詢結果,擷取系統中所有連絡人while (cursor.moveToNext()){ContactInfo info=new ContactInfo();// 擷取連絡人IDString contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));// 擷取連絡人的名字String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));info.setContactName(name);// 使用ContentResolver尋找連絡人的電話號碼Cursor phones = mContext.getContentResolver().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));info.setPhoneNumber(phoneNumber);}phones.close();infos.add(info);info=null;}cursor.close();return infos;}/** * 分頁查詢系統連絡人資訊 * @param pageSize 每頁最大的數目 * @param currentOffset 當前的位移量 * @return */public List<ContactInfo> getContactsByPage(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;}/** * 獲得系統連絡人的所有記錄數目 * @return */public int getAllCounts(){int num=0;// 使用ContentResolver尋找連絡人資料Cursor cursor = mContext.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null,null, null);// 遍曆查詢結果,擷取系統中所有連絡人while (cursor.moveToNext()){num++;}cursor.close();return num;}}

android分頁查詢擷取系統連絡人資訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.