在android中读取联系人信息的程序,包括读取联系人姓名、联系方式和邮箱等

来源:互联网 发布:免费移动办公软件 编辑:程序博客网 时间:2024/04/30 07:25

 1:androidmanifest.xml的内容

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="cn.itcast.contacts"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  7.      <uses-library android:name="android.test.runner" />  
  8.         <activity android:name=".MainActivity"  
  9.                   android:label="@string/app_name">  
  10.             <intent-filter>  
  11.                 <action android:name="android.intent.action.MAIN" />  
  12.                 <category android:name="android.intent.category.LAUNCHER" />  
  13.             </intent-filter>  
  14.         </activity>  
  15.   
  16.     </application>  
  17.     <uses-sdk android:minSdkVersion="8" />  
  18.     <uses-permission android:name="android.permission.READ_CONTACTS" />  
  19.     <uses-permission android:name="android.permission.WRITE_CONTACTS" />  
  20.       
  21.      <instrumentation android:name="android.test.InstrumentationTestRunner"  
  22.      android:targetPackage="cn.itcast.contacts" android:label="Tests for My App" />  
  23. </manifest>   

里面重要的是搭建测试环境和添加<uses-permission>,即读取和写入联系人信息的权限

2:读取的主要方法

[java] view plaincopy
  1.   
[java] view plaincopy
  1. /* 
  2.      * 读取联系人的信息 
  3.      */  
  4.     public void testReadAllContacts() {  
  5.         Cursor cursor = this.getContext().getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,   
  6.                  nullnullnullnull);  
  7.         int contactIdIndex = 0;  
  8.         int nameIndex = 0;  
  9.           
  10.         if(cursor.getCount() > 0) {  
  11.             contactIdIndex = cursor.getColumnIndex(ContactsContract.Contacts._ID);  
  12.             nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);  
  13.         }  
  14.         while(cursor.moveToNext()) {  
  15.             String contactId = cursor.getString(contactIdIndex);  
  16.             String name = cursor.getString(nameIndex);  
  17.             Log.i(TAG, contactId);  
  18.             Log.i(TAG, name);  
  19.               
  20.             /* 
  21.              * 查找该联系人的phone信息 
  22.              */  
  23.             Cursor phones = this.getContext().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,   
  24.                     null,   
  25.                     ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId,   
  26.                     nullnull);  
  27.             int phoneIndex = 0;  
  28.             if(phones.getCount() > 0) {  
  29.                 phoneIndex = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);  
  30.             }  
  31.             while(phones.moveToNext()) {  
  32.                 String phoneNumber = phones.getString(phoneIndex);  
  33.                 Log.i(TAG, phoneNumber);  
  34.             }  
  35.               
  36.             /* 
  37.              * 查找该联系人的email信息 
  38.              */  
  39.             Cursor emails = this.getContext().getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,   
  40.                     null,   
  41.                     ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=" + contactId,   
  42.                     nullnull);  
  43.             int emailIndex = 0;  
  44.             if(emails.getCount() > 0) {  
  45.                 emailIndex = emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);  
  46.             }  
  47.             while(phones.moveToNext()) {  
  48.                 String email = emails.getString(emailIndex);  
  49.                 Log.i(TAG, email);  
  50.             }  
  51.               
  52.         }  
  53.     }  


3:目前手机中的联系人信息,有两个联系人的信息,如图所示,


这是Hellen的联系信息

这是Mike的联系信息


4:测试结果,在控制台输出的内容为:

 可以看到在控制台输出了两个联系人的信息

0 0
原创粉丝点击