黑马程序员-----安卓常用uri

content://com.android.contacts/contacts

对应数据库表中contacts表 用于获得联系人Id  关键字"_id"

技术分享


content://com.android.contacts/contacts/"+ contactid+ "/data

对应data表  通过contactId来查找联系人的各项属性 (data1属性值 data2属性的类型(如家庭电话 手机)) 关键字 "mimetype","data1","data2"

技术分享


"content://com.android.contacts/data/phones/filter/"+ number

通过手机号来查找联系人姓名 关键字"display_name"


content://com.android.contacts/raw_contacts

添加联系人

示例:

//添加姓名
values.put("raw_contact_id", contactid);
values.put("mimetype", "vnd.android.cursor.item/name");
values.put("data2", "张小小");
resolver.insert(uri, values);
//添加电话
values.clear();
values.put("raw_contact_id", contactid);
values.put("mimetype", "vnd.android.cursor.item/phone_v2");
values.put("data2", "2");
values.put("data1", "13671323507");
resolver.insert(uri, values);
//添加Email
values.clear();
values.put("raw_contact_id", contactid);
values.put("mimetype", "vnd.android.cursor.item/email_v2");
values.put("data2", "2");
values.put("data1", "[email protected]");
resolver.insert(uri, values);



package cn.itcast.test;

import java.util.ArrayList;

import android.content.ContentProviderOperation;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.test.AndroidTestCase;
import android.util.Log;

public class ContactsTest extends AndroidTestCase {
	private static final String TAG = "ContactsTest";
	//获取所有联系人
	public void testContacts() throws Exception{
		Uri uri = Uri.parse(<strong>"content://com.android.contacts/contacts</strong>");
		ContentResolver resolver = getContext().getContentResolver();
		Cursor cursor = resolver.query(uri, new String[]{"_id"}, null, null, null);
		while(cursor<strong>.moveToNext()</strong>){
			int contactid = cursor.getInt(0);
			StringBuilder sb = new StringBuilder("contactid=");
			sb.append(contactid);
			uri = Uri.parse("<strong>content://com.android.contacts/contacts/"+ contactid+ "/data"</strong>);
			<span style="color:#ff0000;">//因为contacts中采用了关联查询 所以可以直接获得mimetype</span>
			Cursor datacursor = resolver.query(uri, new String[]{<strong>"mimetype","data1","data2"</strong>}, null, null, null);
			while(datacursor.<strong>moveToNext()</strong>){
				String data = datacursor.getString(datacursor.<strong>getColumnIndex</strong>("data1"));
				String type = datacursor.getString(datacursor.getColumnIndex("mimetype"));
				//mimetype表中对象的值
				if("vnd.android.cursor.item/name".equals(type)){//姓名
					sb.append(",name="+ data);
				}else if("vnd.android.cursor.item/email_v2".equals(type)){//email
					sb.append(",email="+ data);
				}else if("vnd.android.cursor.item/phone_v2".equals(type)){//phone
					sb.append(",phone="+ data);
				}
			}
			Log.i(TAG, sb.toString());
		}
	}
	<span style="color:#ff0000;">//根据号码获取联系人的姓名</span>
	public void testContactNameByNumber() throws Exception{
		String number = "18601025011";
		Uri uri = Uri.parse(<strong>"content://com.android.contacts/data/phones/filter/"+ number</strong>);
		ContentResolver resolver = getContext().getContentResolver();
		Cursor cursor = resolver.query(uri, new String[]{<strong>"display_name"</strong>}, null, null, null);
		if(cursor.moveToFirst()){
			String name = cursor.getString(0);
			Log.i(TAG, name);
		}
		cursor.close();
	}
	<span style="color:#ff0000;">//添加联系人</span>
	public void testAddContact() throws Exception{
		Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
		ContentResolver resolver = getContext().getContentResolver();
		ContentValues values = new ContentValues();
		//<strong><span style="color:#ff0000;">通过执行insert()空操作来返回一个contactId</span></strong>
		long contactid = ContentUris.parseId(resolver.insert(uri, values));
		uri = Uri.parse("content://com.android.contacts/data");
		//添加姓名
		values.put("raw_contact_id", contactid);
		values.put("mimetype", "vnd.android.cursor.item/name");
		values.put("data2", "张小小");
		resolver.<strong>insert(uri, values);</strong>
		//添加电话
		values.clear();
		values.put("raw_contact_id", contactid);
		values.put("mimetype", "vnd.android.cursor.item/phone_v2");
		values.put("data2", "2");
		values.put("data1", "13671323507");
		resolver.insert(uri, values);
		//添加Email
		values.clear();
		values.put("raw_contact_id", contactid);
		values.put("mimetype", "vnd.android.cursor.item/email_v2");
		values.put("data2", "2");
		values.put("data1", "[email protected]");
		resolver.insert(uri, values);
	}
	<span style="color:#ff0000;"><strong>//在同一个事务中完成联系人各项数据的添加</strong></span>
	public void testAddContact2() throws Exception{
		Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
		ContentResolver resolver = getContext().getContentResolver();
		ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
<span style="white-space:pre">	</span><strong><span style="color:#ff6666;">//第0次操作 往</span><span style="color: rgb(255, 102, 102); font-family: Arial, Helvetica, sans-serif;">raw_contacts表中插入一条数据</span></strong><span style="color: rgb(255, 102, 102); font-family: Arial, Helvetica, sans-serif;"><strong>	</strong>	</span>
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-size:14px; white-space: pre;">			</span>     <span style="font-size:12px;">ContentProviderOperation op1 = ContentProviderOperation.newInsert(uri)</span></span><span style="color:#ff6666;font-size:14px;">
</span><span style="font-size:14px;">			.withValue("account_name", null)
			.build();
		operations.add(op1);
		
		uri = Uri.parse("content://com.android.contacts/data");
		ContentProviderOperation op2 = ContentProviderOperation.newInsert(uri)</span>
<span style="font-size:14px; white-space: pre;">			</span><strong><span style="color:#ff0000;">!!!!<span style="font-size:14px;">//根据第0次操作的返回id 作为</span><span style="font-family: Arial, Helvetica, sans-serif; font-size: 14px;">raw_contact_id字段的值</span><span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-size:14px;">	</span></span></span></strong>
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-size:12px;"><span style="white-space:pre">					</span><strong><span style="color:#ff0000;">.withValueBackReference("raw_contact_id", 0)</span></strong></span></span><span style="font-size:14px;">
</span><span style="font-size:14px;">			.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", "13560650505")
			.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", "[email protected]")
			.withValue("data2", "2")
			.build();
		operations.add(op4);
		
		resolver.applyBatch("com.android.contacts", operations);
	}
	
}</span><strong style="font-size:14px;">
</strong>

withValueBackReference(String key, int previousResult)方法

第一个参数key对应于数据库中的列名,第二个参数previousResult代表着:回引数据库批量操作中的第previousResult个数据库操作的返回值。
总的来说就是说把批量数据库操作中的第previousResult个数据库操作的返回值作为列名为key的记录的值







郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。