注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

悠游自在

似水流年,悠游天地,自在我心

 
 
 

日志

 
 
 
 

mono android:获取联系人,解析联系人信息  

2013-10-19 21:43:43|  分类: IT技术 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
android的联系人其实是一个比较复杂的数据、文件、数据库结构,很灵活。
首先要记得在AndroidManifest.xml中加上权限配置
<uses-permission android:name="android.permission.READ_CONTACTS" />
下面代码可以解析联系人的所有主要字段,其中Organization信息的获取比较特殊,我调试了很久才调试出来

              public static void ImpContact(Activity ctx)

              {

                     try {

                            var uri = ContactsContract.Contacts.ContentUri;

                            var cursor = ctx.ManagedQuery (uri, null, null, null, null);

                            if (cursor.Count > 0) {

                                   while (cursor.MoveToNext()) {

                                          var contactId = cursor.GetString (cursor.GetColumnIndex(BaseColumns.Id));

                                          var Name = cursor.GetString (cursor.GetColumnIndex(ContactsContract.ContactsColumns.DisplayName));

                                          Log.Info("i","Id = {0}"+ contactId);

                                          Log.Info("i","Name = "+ Name);

                                          if (cursor.GetInt (cursor.GetColumnIndex(ContactsContract.ContactsColumns.HasPhoneNumber)) == 1) {

                                                 var phoneCursor = ctx.ManagedQuery (ContactsContract.CommonDataKinds.Phone.ContentUri, null, "CONTACT_ID" + " = " + contactId, null, null);

                                                 while (phoneCursor.MoveToNext()) {

                                                        var number = phoneCursor.GetString (phoneCursor.GetColumnIndex(ContactsContract.CommonDataKinds.Phone.Number));

                                                        var type = (PhoneDataKind)phoneCursor.GetInt (phoneCursor.GetColumnIndex("DATA2"));

                                                        Log.Info("i","Telephone:"+ number+"-"+ type.ToString());

                                                 }

                                                 phoneCursor.Close ();

                                          } 

 

                                          var uri1 = ContactsContract.CommonDataKinds.Email.ContentUri;

                                          var emailCursor = ctx.ManagedQuery (ContactsContract.CommonDataKinds.Email.ContentUri, null, "CONTACT_ID" + " = " + contactId, null, null);

                                          while (emailCursor.MoveToNext()) {

                                                 var email = emailCursor.GetString (emailCursor.GetColumnIndex("DATA1"));

                                                 Log.Info("i","E-mail: "+ email);

                                          }

                                          emailCursor.Close ();

                                          var uri2 = ContactsContract.Data.ContentUri;

                                          var strWhere = "CONTACT_ID" + " = " + contactId + " AND MIMETYPE='" + ContactsContract.CommonDataKinds.Organization.ContentItemType + "'";

                                          var orgCursor = ctx.ManagedQuery (ContactsContract.Data.ContentUri, null, strWhere, null, null);

                                          if (orgCursor.MoveToNext ()) {

                                                 var org = orgCursor.GetString (orgCursor.GetColumnIndex(ContactsContract.CommonDataKinds.Organization.Company));

                                                 colcard.Company = org;

                                                 Log.Info("i","org: " + org);

                                          }

                                          orgCursor.Close ();

 

                                          var uri3 = ContactsContract.CommonDataKinds.StructuredPostal.ContentUri;

                                          var address = ctx.ManagedQuery (ContactsContract.CommonDataKinds.StructuredPostal.ContentUri, null, "CONTACT_ID" + " = " + contactId, null, null);

                                          while (address.MoveToNext()) {

                                                 String poBox = address.GetString (address.GetColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.Pobox ));

                                                 String street = address.GetString (address.GetColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.Street));

                                                 String city = address.GetString (address.GetColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.City));

                                                 String state = address.GetString (address.GetColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.Region));

                                                 String postalCode = address.GetString (address.GetColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.Postcode));

                                                 String country = address.GetString (address.GetColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.Country));

                                                 string strAddr = "poBox=" + poBox + ",street=" + street + ",city=" + city + ",state=" + state + ",postalCode=" + postalCode + ",country=" + country;

                                                 colcard.Addr = strAddr;

                                                 Log.Info("i",strAddr);

                                          }

 

                                          Log.Info("i","==============");

                                   }

                                   cursor.Close ();

                            } else {

                                   Log.Info("i","No contacts found");

                            }

                     } catch (Exception e1) {

                     }

              }

 

  评论这张
 
阅读(141)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018