获取android所有联系人信息

转自http://blog.csdn.net/sky181772733/article/details/7221431

这个是代码

  1 import android.app.Activity;
  2 import android.database.Cursor;
  3 import android.os.Bundle;
  4 import android.provider.ContactsContract;
  5 import android.provider.ContactsContract.Data;
  6 import android.provider.ContactsContract.CommonDataKinds.Im;
  7 import android.provider.ContactsContract.CommonDataKinds.Nickname;
  8 import android.provider.ContactsContract.CommonDataKinds.Note;
  9 import android.provider.ContactsContract.CommonDataKinds.Organization;
 10 import android.util.Log;
 11
 12 public class ContactActivity extends Activity {
 13     /** Called when the activity is first created. */
 14     @Override
 15     public void onCreate(Bundle savedInstanceState) {
 16         super.onCreate(savedInstanceState);
 17         setContentView(R.layout.main);
 18
 19         // 获得所有的联系人
 20         Cursor cur = getContentResolver().query(
 21                 ContactsContract.Contacts.CONTENT_URI,
 22                 null,
 23                 null,
 24                 null,
 25                 ContactsContract.Contacts.DISPLAY_NAME
 26                         + " COLLATE LOCALIZED ASC");
 27         // 循环遍历
 28         if (cur.moveToFirst()) {
 29             int idColumn = cur.getColumnIndex(ContactsContract.Contacts._ID);
 30
 31             int displayNameColumn = cur
 32                     .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
 33
 34             do {
 35                 // 获得联系人的ID号
 36                 String contactId = cur.getString(idColumn);
 37                 // 获得联系人姓名
 38                 String disPlayName = cur.getString(displayNameColumn);
 39
 40                 // 查看该联系人有多少个电话号码。如果没有这返回值为0
 41                 int phoneCount = cur
 42                         .getInt(cur
 43                                 .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
 44                 Log.i("username", disPlayName);
 45                 if (phoneCount > 0) {
 46                     // 获得联系人的电话号码
 47                     Cursor phones = getContentResolver().query(
 48                             ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
 49                             null,
 50                             ContactsContract.CommonDataKinds.Phone.CONTACT_ID
 51                                     + " = " + contactId, null, null);
 52                     if (phones.moveToFirst()) {
 53                         do {
 54                             // 遍历所有的电话号码
 55                             String phoneNumber = phones
 56                                     .getString(phones
 57                                             .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
 58                             String phoneType = phones
 59                                     .getString(phones
 60                                             .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
 61                             Log.i("phoneNumber", phoneNumber);
 62                             Log.i("phoneType", phoneType);
 63                         } while (phones.moveToNext());
 64                     }
 65                 }
 66
 67                 // 获取该联系人邮箱
 68                 Cursor emails = getContentResolver().query(
 69                         ContactsContract.CommonDataKinds.Email.CONTENT_URI,
 70                         null,
 71                         ContactsContract.CommonDataKinds.Phone.CONTACT_ID
 72                                 + " = " + contactId, null, null);
 73                 if (emails.moveToFirst()) {
 74                     do {
 75                         // 遍历所有的电话号码
 76                         String emailType = emails
 77                                 .getString(emails
 78                                         .getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
 79                         String emailValue = emails
 80                                 .getString(emails
 81                                         .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
 82
 83                         Log.i("emailType", emailType);
 84                         Log.i("emailValue", emailValue);
 85                     } while (emails.moveToNext());
 86                 }
 87
 88                 // 获取该联系人IM
 89                 Cursor IMs = getContentResolver().query(
 90                         Data.CONTENT_URI,
 91                         new String[] { Data._ID, Im.PROTOCOL, Im.DATA },
 92                         Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "=‘"
 93                                 + Im.CONTENT_ITEM_TYPE + "‘",
 94                         new String[] { contactId }, null);
 95                 if (IMs.moveToFirst()) {
 96                     do {
 97                         String protocol = IMs.getString(IMs
 98                                 .getColumnIndex(Im.PROTOCOL));
 99                         String date = IMs
100                                 .getString(IMs.getColumnIndex(Im.DATA));
101                         Log.i("protocol", protocol);
102                         Log.i("date", date);
103                     } while (IMs.moveToNext());
104                 }
105
106                 // 获取该联系人地址
107                 Cursor address = getContentResolver()
108                         .query(
109                                 ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,
110                                 null,
111                                 ContactsContract.CommonDataKinds.Phone.CONTACT_ID
112                                         + " = " + contactId, null, null);
113                 if (address.moveToFirst()) {
114                     do {
115                         // 遍历所有的地址
116                         String street = address
117                                 .getString(address
118                                         .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
119                         String city = address
120                                 .getString(address
121                                         .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
122                         String region = address
123                                 .getString(address
124                                         .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
125                         String postCode = address
126                                 .getString(address
127                                         .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
128                         String formatAddress = address
129                                 .getString(address
130                                         .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));
131                         Log.i("street", street);
132                         Log.i("city", city);
133                         Log.i("region", region);
134                         Log.i("postCode", postCode);
135                         Log.i("formatAddress", formatAddress);
136                     } while (address.moveToNext());
137                 }
138
139                 // 获取该联系人组织
140                 Cursor organizations = getContentResolver().query(
141                         Data.CONTENT_URI,
142                         new String[] { Data._ID, Organization.COMPANY,
143                                 Organization.TITLE },
144                         Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "=‘"
145                                 + Organization.CONTENT_ITEM_TYPE + "‘",
146                         new String[] { contactId }, null);
147                 if (organizations.moveToFirst()) {
148                     do {
149                         String company = organizations.getString(organizations
150                                 .getColumnIndex(Organization.COMPANY));
151                         String title = organizations.getString(organizations
152                                 .getColumnIndex(Organization.TITLE));
153                         Log.i("company", company);
154                         Log.i("title", title);
155                     } while (organizations.moveToNext());
156                 }
157
158                 // 获取备注信息
159                 Cursor notes = getContentResolver().query(
160                         Data.CONTENT_URI,
161                         new String[] { Data._ID, Note.NOTE },
162                         Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "=‘"
163                                 + Note.CONTENT_ITEM_TYPE + "‘",
164                         new String[] { contactId }, null);
165                 if (notes.moveToFirst()) {
166                     do {
167                         String noteinfo = notes.getString(notes
168                                 .getColumnIndex(Note.NOTE));
169                         Log.i("noteinfo", noteinfo);
170                     } while (notes.moveToNext());
171                 }
172
173                 // 获取nickname信息
174                 Cursor nicknames = getContentResolver().query(
175                         Data.CONTENT_URI,
176                         new String[] { Data._ID, Nickname.NAME },
177                         Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "=‘"
178                                 + Nickname.CONTENT_ITEM_TYPE + "‘",
179                         new String[] { contactId }, null);
180                 if (nicknames.moveToFirst()) {
181                     do {
182                         String nickname_ = nicknames.getString(nicknames
183                                 .getColumnIndex(Nickname.NAME));
184                         Log.i("nickname_", nickname_);
185                     } while (nicknames.moveToNext());
186                 }
187
188             } while (cur.moveToNext());
189
190         }
191
192     }
193
194 }

权限

    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <uses-permission android:name="android.permission.WRITE_CONTACTS"/>

以上的是获取所有的联系人,在获取的时候会获取 sim卡 和手机本地中的所有的信息,如果在实际操作中要区分出是要求 sim卡中号码 还是 手机本地中的号码 则可以用下面的方法

 1 首先是手机本地:Cursor cursor = getContentResolver().query(People.CONTENT_URI, null,
 2      null, null, null);
 3    while (cursor.moveToNext()) {
 4     ContactInfo cci = new ContactInfo();
 5     //取得联系人名字
 6     int nameFieldColumnIndex = cursor.getColumnIndex(People.NAME);
 7     cci.contactName = cursor.getString(nameFieldColumnIndex);
 8     //取得电话号码
 9     int numberFieldColumnIndex = cursor.getColumnIndex(People.NUMBER);
10     cci.userNumber = cursor.getString(numberFieldColumnIndex);
11     cci.userNumber = GetNumber(cci.userNumber);
12     cci.isChecked = false;
13     if (IsUserNumber(cci.userNumber)) {
14      if (!IsContain(contactList, cci.userNumber)) {
15       if(IsAlreadyCheck(wNumStr, cci.userNumber)){
16        cci.isChecked = true;
17        numberStr += "," + cci.userNumber;
18       }
19       contactList.add(cci);
20       //Log.i("eoe", "*********"+cci.userNumber);
21      }
22     }
23    }
24    cursor.close();
25 }

 1 下面是获取SIM卡:
 2 //从SIM卡中取号
 3 private void GetSimContact(String add){
 4    //读取SIM卡手机号,有两种可能:content://icc/adn与content://sim/adn
 5    try {
 6     Intent intent = new Intent();
 7     intent.setData(Uri.parse(add));
 8     Uri uri = intent.getData();
 9     mCursor = getContentResolver().query(uri, null, null, null, null);
10     if (mCursor != null) {
11      while (mCursor.moveToNext()) {
12       ContactInfo sci = new ContactInfo();
13       // 取得联系人名字
14       int nameFieldColumnIndex = mCursor.getColumnIndex("name");
15       sci.contactName = mCursor.getString(nameFieldColumnIndex);
16       // 取得电话号码
17       int numberFieldColumnIndex = mCursor
18         .getColumnIndex("number");
19       sci.userNumber = mCursor.getString(numberFieldColumnIndex);
20       sci.userNumber = GetNumber(sci.userNumber);
21       sci.isChecked = false;
22
23       if (IsUserNumber(sci.userNumber)) {
24        if (!IsContain(contactList, sci.userNumber)) {
25         if(IsAlreadyCheck(wNumStr, sci.userNumber)){
26          sci.isChecked = true;
27          numberStr += "," + sci.userNumber;
28         }
29         contactList.add(sci);
30         //Log.i("eoe", "*********"+sci.userNumber);
31        }
32       }
33      }
34      mCursor.close();
35     }
36    } catch (Exception e) {
37     Log.i("eoe", e.toString());
38    }
39 }

时间: 2024-10-04 23:48:44

获取android所有联系人信息的相关文章

如何获取android手机联系人并按字母展示(二)

下面写怎么让每个首字母相同的联系人归类展示: 在adapter implement SectionIndexer 这样adapter里必须实现以下3个接口: @Override public Object[] getSections() { //section的集合 } @Override public int getPositionForSection(int section) {//当前section的位置 } @Override public int getSectionForPositi

如何获取android手机联系人并按字母展示(三)

如果获取contact的头像信息并展示: 如何根据photoId来获取bitmap: public static Bitmap getContactPhoto(Context context, long photoId, BitmapFactory.Options options) { if (photoId < 0) { return null; } Cursor cursor = null; try { cursor = context.getContentResolver().query(

如何获取android手机联系人并按字母展示(一)

android提供了本地数据库的查询uri,可以查询出数据: 采用一个AsyncQueryHandler来进行查询, AsyncQueryHandler自己开启了线程来进行数据查询,很方便 protected AsyncQueryHandler mQueryHandler; protected final void queryPersonal() { mQueryHandler.startQuery(QUERY_TOKEN, null, ContactsContract.Contacts.CON

[android] 获取系统的联系人信息

内容提供是实质上是个接口,后门,他给别人提供数据,系统联系人是个比较复杂的内容通过者. 找到/data/data/com.android.providers.contacts/contacts2.db 这个目录下还有个文件contacts2.db.-journal,这个文件和数据库的事务相关 联系人应用数据库的主要结构 raw_contacts 联系人表 contact_id 联系人id data数据表  raw_contact_id联系人id,mimetype_id数据类id,data1数据

Android实例-手机安全卫士(二十六)—获取手机内联系人信息

一.目标. 通过内容解析器获取手机联系人信息,并采用自定义的样式显示. 为了便于介绍和重复使用,重新建立一个”读取联系人“工程. 二.代码实现. 1.新建工程,取名为”读取联系人“.在布局文件(activity_main.xml)中,采用ListView组件(其ID为select_contact). 布局文件代码: 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmln

怎样获取android手机联系人并按字母展示(三)

假设获取contact的头像信息并展示: 怎样依据photoId来获取bitmap: public static Bitmap getContactPhoto(Context context, long photoId, BitmapFactory.Options options) { if (photoId < 0) { return null; } Cursor cursor = null; try { cursor = context.getContentResolver().query(

IOS获取系统通讯录联系人信息

先导入AddressBook.framework先 然后引用  #import <AddressBook/AddressBook.h> 一.权限注册 随着apple对用户隐私的越来越重视,IOS系统的权限设置也更加严格,在获取系统通讯录之前,我们必须获得用户的授权.权限申请代码示例如下: #pragma mark - 注册权限 - (void)contacts { //这个变量用于记录授权是否成功,即用户是否允许我们访问通讯录 int __block tip = 0; //声明一个通讯簿的引用

获取Android崩溃crash信息并写入日志发送邮件

一.实现Thread.UncaughtExceptionHandlerUnChecked异常发生时,由于没有相应的try…catch处理该异常对象,所以Java运行环境将会终止,程序将退出,也就是我们所说的Crash.Java API提供了一个全局异常捕获处理器,Android应用在Java层捕获Crash依赖的就是Thread.UncaughtExceptionHandler处理器接口,通常我们只需实现这个接口,并重写其中的uncaughtException方法,在该方法中可以读取Crash的

代码获取Android版本等信息

我手机的关于手机界面: 说明: 其中手机型号.Android版本.软件版本通过系统Build类得到,处理器信息.内核版本通过读取系统文件得到,基带版本信息通过反射得到. 源码: package com.example.shen.phoneinfo; import android.app.Activity; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.util.L