資料是應用的核心(該話不是我說的,某本書上看到的),在Android的
應用程式中我們經常需要調用通信錄,比如給聯系人發送賀卡,發送Email。
我們已經知道可以通過ContentProvider去拿到資料,但是其uri如何得到呢。
這就需要我們去檢視文檔,但是Android開發者指南已經很久沒有更新過了,
上面的URI有很多也是錯誤的,我們必須通過自己檢視源檔案來找到uri,
然後才可以拿到正确的資料。
以下代碼是通過測試類來測試該方法,注釋裡的部分是通過ContactsContract.Contacts
這個類裡的常量擷取uri以及id。
而方法中确實通過檢視SQLite來擷取字段的
package com.sina.contacts;
import java.util.ArrayList;
import android.content.ContentProviderOperation<wbr>;<br> import android.content.ContentProviderResult;<br> import android.content.ContentResolver;<br> import android.content.ContentUris;<br> import android.content.ContentValues;<br> import android.database.Cursor;<br> import android.net.Uri;<br> import android.provider.ContactsContract;<br> import android.provider.ContactsContract.RawContacts;<br> import android.provider.ContactsContract.CommonDataKinds.Email;<br> import android.provider.ContactsContract.CommonDataKinds.Phone;<br> import android.provider.ContactsContract.CommonDataKinds.StructuredName;<br> import android.provider.ContactsContract.Contacts.Data;<br> import android.test.AndroidTestCase;<br> import android.util.Log;</wbr>
public class ContactsTest extends AndroidTestCase {
<wbr>public static final String TAG = "ContactsTest";</wbr>
<wbr><br><wbr><wbr> StringBuffer sb=new StringBuffer();<br><wbr><wbr><br><wbr><wbr> public void testGetAllContacts()throws Exception{<br><wbr><wbr> ContentResolver cr =this.getContext().getContentResolver();<br><wbr><wbr> Uri uri=ContactsContract.Contacts.CONTENT_URI;<br><wbr><wbr> Cursor cursor = cr.query(uri,null, null, null, null);<br><wbr><wbr> while(cursor.moveToNext()){<br><wbr><wbr> String contactId =cursor.getString(cursor.getColumnIndex("_id"));<br><wbr><wbr> String contactName =cursor.getString(cursor.getColumnIndex("display_name"));<br><wbr><wbr> sb.append(contactId).append(","+contactName+",");<br><wbr><wbr><br><wbr><wbr> Uri phoneUri=Uri.parse("content://com.android.contacts/data/phones");<br><wbr><wbr> Cursor phoneCursor = cr.query(phoneUri, null, "raw_contact_id=?", new String[]{contactId}, null);<br><wbr><wbr> while(phoneCursor.moveToNext()){<br><wbr><wbr><wbr> String phoneNumber=phoneCursor.getString(phoneCursor.getColumnIndex("data1"));<br><wbr><wbr><wbr> sb.append(phoneNumber+",");<br><wbr><wbr> }<br><wbr><wbr><br><wbr><wbr> Uri emailUri=Uri.parse("content://com.android.contacts/data/emails");<br><wbr><wbr> Cursor emailCursor=cr.query(emailUri, null, "raw_contact_id="+contactId, null, null);<br><wbr><wbr> while(emailCursor.moveToNext()){<br><wbr><wbr><wbr> String email = emailCursor.getString(emailCursor.getColumnIndex("data1"));<br><wbr><wbr><wbr> sb.append(email+". ");<br><wbr><wbr> }<br><wbr><wbr> }<br><wbr><wbr> Log.i(TAG, sb.toString());<br><wbr><wbr> }<br><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><br><wbr><br><wbr><br><wbr>public void testInsert() {<br><wbr> ContentValues values = new ContentValues();<br><wbr><br><wbr><br><wbr> Uri rawContactUri = this.getContext().getContentResolver().insert(<br><wbr><wbr><wbr> RawContacts.CONTENT_URI, values);<br><wbr> long rawContactId = ContentUris.parseId(rawContactUri);<br><wbr> // 往data表入姓名資料<br><wbr> values.clear();<br><wbr> values.put(Data.RAW_CONTACT_ID, rawContactId);<br><wbr> values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);// 内容類型<br><wbr> values.put(StructuredName.GIVEN_NAME, "李天山");<br><wbr> this.getContext().getContentResolver().insert(<br><wbr><wbr><wbr> android.provider.ContactsContract.Data.CONTENT_URI, values);<br><wbr> // 往data表入電話資料<br><wbr> values.clear();<br><wbr> values.put(Data.RAW_CONTACT_ID, rawContactId);<br><wbr> values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);<br><wbr> values.put(Phone.NUMBER, "13921009789");<br><wbr> values.put(Phone.TYPE, Phone.TYPE_MOBILE);<br><wbr> this.getContext().getContentResolver().insert(<br><wbr><wbr><wbr> android.provider.ContactsContract.Data.CONTENT_URI, values);<br><wbr> // 往data表入Email資料<br><wbr> values.clear();<br><wbr> values.put(Data.RAW_CONTACT_ID, rawContactId);<br><wbr> values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);<br><wbr> values.put(Email.DATA, "<a href="mailto:[email protected]" target="_blank" rel="external nofollow" >[email protected]</a>");<br><wbr> values.put(Email.TYPE, Email.TYPE_WORK);<br><wbr> this.getContext().getContentResolver().insert(<br><wbr><wbr><wbr> android.provider.ContactsContract.Data.CONTENT_URI, values);<br><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr>// 批量添加,處于同一個事務中<br><wbr>public void testSave() throws Throwable {<br><wbr> // 文檔位置:reference\android\provider\ContactsContract.RawContacts.html<br><wbr> ArrayList<ContentProviderOperation<wbr>> ops = new ArrayList<ContentProviderOperation<wbr>>();<br><wbr> int rawContactInsertIndex = ops.size();<br><wbr> ops.add(ContentProviderOperation<wbr>.newInsert(RawContacts.CONTENT_URI)<br><wbr><wbr><wbr> .withValue(RawContacts.ACCOUNT_TYPE, null).withValue(<br><wbr><wbr><wbr><wbr><wbr> RawContacts.ACCOUNT_NAME, null).build());<br><wbr> // 文檔位置:reference\android\provider\ContactsContract.Data.html<br><wbr> ops.add(ContentProviderOperation<wbr>.newInsert(<br><wbr><wbr><wbr> android.provider.ContactsContract.Data.CONTENT_URI)<br><wbr><wbr><wbr> .withValueBackReference(Data.RAW_CONTACT_ID,<br><wbr><wbr><wbr><wbr><wbr> rawContactInsertIndex).withValue(Data.MIMETYPE,<br><wbr><wbr><wbr><wbr><wbr> StructuredName.CONTENT_ITEM_TYPE).withValue(<br><wbr><wbr><wbr><wbr><wbr> StructuredName.GIVEN_NAME, "趙薇").build());<br><wbr> ops.add(ContentProviderOperation<wbr>.newInsert(<br><wbr><wbr><wbr> android.provider.ContactsContract.Data.CONTENT_URI)<br><wbr><wbr><wbr> .withValueBackReference(Data.RAW_CONTACT_ID,<br><wbr><wbr><wbr><wbr><wbr> rawContactInsertIndex).withValue(Data.MIMETYPE,<br><wbr><wbr><wbr><wbr><wbr> Phone.CONTENT_ITEM_TYPE).withValue(Phone.NUMBER,<br><wbr><wbr><wbr><wbr><wbr> "13671323809").withValue(Phone.TYPE, Phone.TYPE_MOBILE)<br><wbr><wbr><wbr> .withValue(Phone.LABEL, "手機号").build());<br><wbr> ops.add(ContentProviderOperation<wbr>.newInsert(<br><wbr><wbr><wbr> android.provider.ContactsContract.Data.CONTENT_URI)<br><wbr><wbr><wbr> .withValueBackReference(Data.RAW_CONTACT_ID,<br><wbr><wbr><wbr><wbr><wbr> rawContactInsertIndex).withValue(Data.MIMETYPE,<br><wbr><wbr><wbr><wbr><wbr> Email.CONTENT_ITEM_TYPE).withValue(Email.DATA,<br><wbr><wbr><wbr><wbr><wbr> "<a href="mailto:[email protected]').withValue(Email.TYPE" target="_blank" rel="external nofollow" >[email protected]").withValue(Email.TYPE</a>,<br><wbr><wbr><wbr><wbr><wbr> Email.TYPE_WORK).build());<br><wbr> ContentProviderResult[] results = this.getContext()<br><wbr><wbr><wbr> .getContentResolver().applyBatch(ContactsContract.AUTHORITY,<br><wbr><wbr><wbr><wbr><wbr> ops);<br><wbr> for (ContentProviderResult result : results) {<br><wbr><wbr> Log.i(TAG, result.uri.toString());<br><wbr> }<br><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
}