版权申明:http://yarin.javaeye.com/blog/453262

 

当我们将Andorid1.5及其以前的项目放到Android2.0上时,如果代码中有

Java代码 
  1. import android.provider.Contacts;  

Eclipse会提示“建议不使用”,那是因为在Android2.0中,联系人api发生了变化,需要使用ContactsContract。

 

直接看下面一个最简单的例子,读取联系人的姓名和电话号码:

读取联系人的名字很简单,但是在读取电话号码时,就需要先去的联系人的ID,然后在通过ID去查找电话号码!一个联系人可能存在多个电话号码!

Java代码 
  1. //得到ContentResolver对象  
  2.       ContentResolver cr = getContentResolver();    
  3.       //取得电话本中开始一项的光标  
  4. Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, nullnullnullnull);  
  5.   
  6. while (cursor.moveToNext())  
  7. {  
  8.     // 取得联系人名字  
  9.     int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);  
  10.     String name = cursor.getString(nameFieldColumnIndex);  
  11.     string += (name);  
  12.   
  13.     // 取得联系人ID  
  14.     String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));  
  15.     Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "  
  16.             + contactId, nullnull);  
  17.   
  18.     // 取得电话号码(可能存在多个号码)  
  19.     while (phone.moveToNext())  
  20.     {  
  21.         String strPhoneNumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));  
  22.         string += (":" + strPhoneNumber);  
  23.     }  
  24.     string += "\n";  
  25.     phone.close();  
  26. }  
  27. cursor.close();  

 当然,还有得到email等操作!

先写到这里,更多关于Android2.0的内容,有待研究。 

 posted on 2010-01-29 13:53  清水湾  阅读(827)  评论(0编辑  收藏  举报