Android通信录

数据是应用的核心(该话不是我说的,某本书上看到的),在Android的
应用程序中我们经常需要调用通信录,比如给联系人发送贺卡,发送Email。
我们已经知道可以通过ContentProvider去拿到数据,但是其uri如何得到呢。
这就需要我们去查看文档,但是Android开发者指南已经很久没有更新过了,
上面的URI有很多也是错误的,我们必须通过自己查看源文件来找到uri,
然后才可以拿到正确的数据。

以下代码是通过测试类来测试该方法,注释里的部分是通过ContactsContract.Contacts
这个类里的常量获取uri以及id。
而方法中确实通过查看SQLite来获取字段的

package com.sina.contacts;

import java.util.ArrayList;

import android.content.ContentProviderOperation;
import android.content.ContentProviderResult;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.RawContacts;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.Contacts.Data;
import android.test.AndroidTestCase;
import android.util.Log;

public class ContactsTest extends AndroidTestCase {

 public static final String TAG = "ContactsTest";

 
   StringBuffer sb=new StringBuffer();
  
   public void testGetAllContacts()throws Exception{
   ContentResolver cr =this.getContext().getContentResolver();
   Uri uri=ContactsContract.Contacts.CONTENT_URI;
   Cursor cursor = cr.query(uri,null, null, null, null);
   while(cursor.moveToNext()){
   String contactId =cursor.getString(cursor.getColumnIndex("_id"));
   String contactName =cursor.getString(cursor.getColumnIndex("display_name"));
   sb.append(contactId).append(","+contactName+",");
  
   Uri phoneUri=Uri.parse("content://com.android.contacts/data/phones");
   Cursor phoneCursor = cr.query(phoneUri, null, "raw_contact_id=?", new String[]{contactId}, null);
   while(phoneCursor.moveToNext()){
    String phoneNumber=phoneCursor.getString(phoneCursor.getColumnIndex("data1"));
    sb.append(phoneNumber+",");
   }
  
   Uri emailUri=Uri.parse("content://com.android.contacts/data/emails");
   Cursor emailCursor=cr.query(emailUri, null, "raw_contact_id="+contactId, null, null);
   while(emailCursor.moveToNext()){
    String email = emailCursor.getString(emailCursor.getColumnIndex("data1"));
    sb.append(email+". ");
   }
   }
   Log.i(TAG, sb.toString());
   }
 

 
 
 
 public void testInsert() {
  ContentValues values = new ContentValues();
 
 
  Uri rawContactUri = this.getContext().getContentResolver().insert(
    RawContacts.CONTENT_URI, values);
  long rawContactId = ContentUris.parseId(rawContactUri);
  // 往data表入姓名数据
  values.clear();
  values.put(Data.RAW_CONTACT_ID, rawContactId);
  values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);// 内容类型
  values.put(StructuredName.GIVEN_NAME, "李天山");
  this.getContext().getContentResolver().insert(
    android.provider.ContactsContract.Data.CONTENT_URI, values);
  // 往data表入电话数据
  values.clear();
  values.put(Data.RAW_CONTACT_ID, rawContactId);
  values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
  values.put(Phone.NUMBER, "13921009789");
  values.put(Phone.TYPE, Phone.TYPE_MOBILE);
  this.getContext().getContentResolver().insert(
    android.provider.ContactsContract.Data.CONTENT_URI, values);
  // 往data表入Email数据
  values.clear();
  values.put(Data.RAW_CONTACT_ID, rawContactId);
  values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);
  values.put(Email.DATA, "liming@itcast.cn");
  values.put(Email.TYPE, Email.TYPE_WORK);
  this.getContext().getContentResolver().insert(
    android.provider.ContactsContract.Data.CONTENT_URI, values);
 }

 // 批量添加,处于同一个事务中
 public void testSave() throws Throwable {
  // 文档位置:reference\android\provider\ContactsContract.RawContacts.html
  ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
  int rawContactInsertIndex = ops.size();
  ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
    .withValue(RawContacts.ACCOUNT_TYPE, null).withValue(
      RawContacts.ACCOUNT_NAME, null).build());
  // 文档位置:reference\android\provider\ContactsContract.Data.html
  ops.add(ContentProviderOperation.newInsert(
    android.provider.ContactsContract.Data.CONTENT_URI)
    .withValueBackReference(Data.RAW_CONTACT_ID,
      rawContactInsertIndex).withValue(Data.MIMETYPE,
      StructuredName.CONTENT_ITEM_TYPE).withValue(
      StructuredName.GIVEN_NAME, "赵薇").build());
  ops.add(ContentProviderOperation.newInsert(
    android.provider.ContactsContract.Data.CONTENT_URI)
    .withValueBackReference(Data.RAW_CONTACT_ID,
      rawContactInsertIndex).withValue(Data.MIMETYPE,
      Phone.CONTENT_ITEM_TYPE).withValue(Phone.NUMBER,
      "13671323809").withValue(Phone.TYPE, Phone.TYPE_MOBILE)
    .withValue(Phone.LABEL, "手机号").build());
  ops.add(ContentProviderOperation.newInsert(
    android.provider.ContactsContract.Data.CONTENT_URI)
    .withValueBackReference(Data.RAW_CONTACT_ID,
      rawContactInsertIndex).withValue(Data.MIMETYPE,
      Email.CONTENT_ITEM_TYPE).withValue(Email.DATA,
      "liming@itcast.cn").withValue(Email.TYPE,
      Email.TYPE_WORK).build());
  ContentProviderResult[] results = this.getContext()
    .getContentResolver().applyBatch(ContactsContract.AUTHORITY,
      ops);
  for (ContentProviderResult result : results) {
   Log.i(TAG, result.uri.toString());
  }
 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值