您好,登錄后才能下訂單哦!
本文實例講述了Android編程實現通訊錄中聯系人的讀取,查詢,添加功能。分享給大家供大家參考,具體如下:
先加二個讀和寫權限:
<uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_CONTACTS" />
具體代碼:
package com.eboy.test; import java.util.ArrayList; import android.content.ContentProviderOperation; import android.content.ContentResolver; import android.database.Cursor; import android.net.Uri; import android.test.AndroidTestCase; import android.util.Log; public class ContectTest extends AndroidTestCase { private static final String TAG = "TestContact"; //查詢所有聯系人的姓名,電話,郵箱 public void TestContact() throws Exception { Uri uri = Uri.parse("content://com.android.contacts/contacts"); ContentResolver resolver = getContext().getContentResolver(); Cursor cursor = resolver.query(uri, new String[]{"_id"}, null, null, null); while (cursor.moveToNext()) { int contractID = cursor.getInt(0); StringBuilder sb = new StringBuilder("contractID="); sb.append(contractID); uri = Uri.parse("content://com.android.contacts/contacts/" + contractID + "/data"); Cursor cursor1 = resolver.query(uri, new String[]{"mimetype", "data1", "data2"}, null, null, null); while (cursor1.moveToNext()) { String data1 = cursor1.getString(cursor1.getColumnIndex("data1")); String mimeType = cursor1.getString(cursor1.getColumnIndex("mimetype")); if ("vnd.android.cursor.item/name".equals(mimeType)) { //是姓名 sb.append(",name=" + data1); } else if ("vnd.android.cursor.item/email_v2".equals(mimeType)) { //郵箱 sb.append(",email=" + data1); } else if ("vnd.android.cursor.item/phone_v2".equals(mimeType)) { //手機 sb.append(",phone=" + data1); } } cursor1.close(); Log.i(TAG, sb.toString()); } cursor.close(); } //查詢指定電話的聯系人姓名,郵箱 public void testContactNameByNumber() throws Exception { String number = "18052369652"; Uri uri = Uri.parse("content://com.android.contacts/data/phones/filter/" + number); ContentResolver resolver = getContext().getContentResolver(); Cursor cursor = resolver.query(uri, new String[]{"display_name"}, null, null, null); if (cursor.moveToFirst()) { String name = cursor.getString(0); Log.i(TAG, name); } cursor.close(); } //添加聯系人,使用事務 public void testAddContact() throws Exception { Uri uri = Uri.parse("content://com.android.contacts/raw_contacts"); ContentResolver resolver = getContext().getContentResolver(); ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(); ContentProviderOperation op1 = ContentProviderOperation.newInsert(uri) .withValue("account_name", null) .build(); operations.add(op1); uri = Uri.parse("content://com.android.contacts/data"); ContentProviderOperation op2 = ContentProviderOperation.newInsert(uri) .withValueBackReference("raw_contact_id", 0) .withValue("mimetype", "vnd.android.cursor.item/name") .withValue("data2", "龔小永") .build(); operations.add(op2); ContentProviderOperation op3 = ContentProviderOperation.newInsert(uri) .withValueBackReference("raw_contact_id", 0) .withValue("mimetype", "vnd.android.cursor.item/phone_v2") .withValue("data1", "13539777967") .withValue("data2", "2") .build(); operations.add(op3); ContentProviderOperation op4 = ContentProviderOperation.newInsert(uri) .withValueBackReference("raw_contact_id", 0) .withValue("mimetype", "vnd.android.cursor.item/email_v2") .withValue("data1", "asdfasfad@163.com") .withValue("data2", "2") .build(); operations.add(op4); resolver.applyBatch("com.android.contacts", operations); } }
完整實例代碼點擊此處本站下載。
更多關于Android相關內容感興趣的讀者可查看本站專題:《Android開發入門與進階教程》、《Android調試技巧與常見問題解決方法匯總》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。