码迷,mamicode.com
首页 > 移动开发 > 详细

Forms Android Contacts

时间:2015-08-01 00:46:07      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
  1   2 forms定义接口
  3 
  4 using System;
  5 using System.Collections;
  6 using System.Collections.Generic;
  7 
  8 namespace
  9 {
 10     public interface ISystemContact
 11     {
 12         //获取所有联系人
 13         List<String> GetSystemContact ();
 14         //插入联系人
 15         bool InsertContact(string name,string number);
 16         //根据姓名查找联系人电话
 17         List<string> GetPhoneByName(string name);
 18     }
 19 }
 20 
 21 平台实现
 22  using System;
 23 using Android.Content;
 24 using Android.Provider;
 25 using Xamarin.Forms;
 26 using Android.App;
 27 using System.Collections;
 28 using System.Collections.Generic;
 29 using.TDroid;
 30 
 31 [assembly:Dependency(typeof(SystemContact))]
 32 namespace.TDroid
 33 {
 34     public class SystemContact:Java.Lang.Object,ISystemContact
 35     {
 36         public SystemContact()
 37         {
 38 
 39         }
 40         public List<String> GetSystemContact()
 41         {
 42 //            Hashtable hshTable = new Hashtable ();
 43 //            var uri=ContactsContract.Contacts.ContentUri;
 44 //            string[] projection = { 
 45 //                ContactsContract.Contacts.InterfaceConsts.Id,
 46 //                ContactsContract.Contacts.InterfaceConsts.DisplayName,
 47 //                ContactsContract.Contacts.InterfaceConsts.PhotoThumbnailUri,
 48 //                ContactsContract.Contacts.InterfaceConsts.HasPhoneNumber,
 49 //            };
 50 //            var cursor = ((Activity)Forms.Context).ManagedQuery (uri,projection,null,null,null);
 51 //            if (cursor.MoveToFirst ()) {
 52 //                do{
 53 //                    string _id=cursor.GetString(cursor.GetColumnIndex(projection[0]));
 54 //                    string _name=cursor.GetString(cursor.GetColumnIndex(projection[1]));
 55 //                    List<string> _number=new List<string> ();
 56 //                    int fla=cursor.GetInt(cursor.GetColumnIndex(projection[3]));
 57 //                    if(fla>0){
 58 //                        var cur_phone = ((Activity)Forms.Context).ManagedQuery (ContactsContract.CommonDataKinds.Phone.ContentUri,null,null, null, null);
 59 //                        if(cur_phone.MoveToFirst()){    
 60 //                            do{    
 61 //                                //遍历所有的电话号码    
 62 //                                String phoneNumber= cur_phone.GetString(cur_phone.GetColumnIndex(ContactsContract.CommonDataKinds.Phone.Number));     
 63 //                                _number.Add(phoneNumber);
 64 //                            }while(cur_phone.MoveToNext());    
 65 //                        } 
 66 //                    }
 67 //                    hshTable.Add(_name,_number);
 68 //                }while(cursor.MoveToNext());
 69 //            }
 70             List<string> hshtable = new List<string> ();
 71             var cur_phone = ((Activity)Forms.Context).ManagedQuery 
 72                 (ContactsContract.CommonDataKinds.Phone.ContentUri,null,null, null, null);
 73             if (cur_phone != null&&cur_phone.MoveToFirst()) {
 74                 do {
 75                     string[] _names = cur_phone.GetColumnNames ();
 76                     var _number = cur_phone.GetString (cur_phone.GetColumnIndex ("data1"));
 77                     var _name = cur_phone.GetString (cur_phone.GetColumnIndex ("display_name"));
 78                     hshtable.Add(_name+":"+_number);
 79                 } while(cur_phone.MoveToNext ());
 80             }
 81             return hshtable;
 82         }
 83 
 84         //添加联系人,号码 姓名 重复则覆盖
 85         public bool InsertContact(string name,string number){
 86             try{
 87             ContentValues values = new ContentValues ();
 88             Android.Net.Uri rawContactUri=((Activity)Forms.Context).ContentResolver
 89                     .Insert (Android.Provider.ContactsContract.RawContacts.ContentUri,values);
 90             long rawContactId= ContentUris.ParseId (rawContactUri);
 91             //添加姓名
 92             values.Clear ();
 93             values.Put (Android.Provider.ContactsContract.Data.InterfaceConsts.RawContactId,rawContactId);
 94             values.Put (Android.Provider.ContactsContract.Data.InterfaceConsts.Mimetype,Android.Provider
 95                     .ContactsContract.CommonDataKinds.StructuredName.ContentItemType);
 96             values.Put (Android.Provider.ContactsContract.CommonDataKinds.StructuredName.GivenName,name);
 97             ((Activity)Forms.Context).ContentResolver.Insert (Android.Provider.ContactsContract.Data.ContentUri,values);
 98             //添加号码
 99             values.Clear ();
100             values.Put (Android.Provider.ContactsContract.Data.InterfaceConsts.RawContactId,rawContactId);
101             values.Put (Android.Provider.ContactsContract.Data.InterfaceConsts.Mimetype,Android.Provider
102                     .ContactsContract.CommonDataKinds.Phone.ContentItemType);
103             values.Put (Android.Provider.ContactsContract.CommonDataKinds.Phone.Number,number);
104             values.Put (Android.Provider.ContactsContract.CommonDataKinds.Phone.InterfaceConsts
105                     .Type,Android.Provider.ContactsContract.CommonDataKinds.Phone.InterfaceConsts.TypeCustom);
106             ((Activity)Forms.Context).ContentResolver.Insert (Android.Provider.ContactsContract.Data.ContentUri,values);
107 
108             //添加email
109             /*
110             values.Clear ();
111             values.Put (Android.Provider.ContactsContract.Data.InterfaceConsts.RawContactId,rawContactId);
112             values.Put (Android.Provider.ContactsContract.Data.InterfaceConsts.Mimetype,Android.Provider.ContactsContract.CommonDataKinds.Email.ContentItemType);
113             values.Put (Android.Provider.ContactsContract.CommonDataKinds.Email.InterfaceConsts.Data,"111@qq.com");
114             values.Put (Android.Provider.ContactsContract.CommonDataKinds.Email.InterfaceConsts.Type,Android.Provider.ContactsContract.CommonDataKinds.Email.InterfaceConsts.TypeCustom);
115             ((Activity)Forms.Context).ContentResolver.Insert (Android.Provider.ContactsContract.Data.ContentUri,values);
116             */
117                 return true;
118             }
119             catch(Exception ex){
120                 return false;
121             }
122         }
123 
124         public List<string> GetPhoneByName(string name){
125             List<string> listPhone = new List<string> ();
126             string id = GetContactId (name);
127             if (id.Equals ("0")) {
128                 System.Diagnostics.Debug.WriteLine (name + " not exist");
129             } else {
130                 try{
131                     var cursor = ((Activity)Forms.Context).ManagedQuery 
132                         (ContactsContract.CommonDataKinds.Phone.ContentUri,
133                             new string[]{Android.Provider.ContactsContract.CommonDataKinds.Phone.Number,
134                                 Android.Provider.ContactsContract.CommonDataKinds.Phone.InterfaceConsts.Type},
135                             Android.Provider.ContactsContract.CommonDataKinds.Phone.InterfaceConsts.ContactId+ "=‘" + id + "", 
136                             null, 
137                             null);
138                     while(cursor.MoveToNext()){
139                         string phone = cursor.GetString(
140                             cursor.GetColumnIndex(Android.Provider.ContactsContract.CommonDataKinds.Phone.Number));
141                         listPhone.Add(phone);
142                     }
143                 }catch(Exception e){
144 
145                 }
146             }
147             return listPhone;
148         }
149 
150 
151         private string GetContactId(string name){
152             string id = "0";
153             try{
154                 var cursor = ((Activity)Forms.Context).ManagedQuery 
155                     (Android.Provider.ContactsContract.Contacts.ContentUri,
156                         new string[]{Android.Provider.ContactsContract.Contacts.InterfaceConsts.Id},
157                         Android.Provider.ContactsContract.Contacts.InterfaceConsts.DisplayName + "=‘" + name + "", 
158                         null, 
159                         null);
160                 if(cursor!=null){
161                     int count = 0;
162                     while(cursor.MoveToNext()){
163                         //string phone;
164                         //id = cursor.GetString(cursor.GetColumnIndex(Android.Provider.ContactsContract.Contacts.InterfaceConsts.Id));
165                         //System.Diagnostics.Debug.WriteLine(name + ":" + id);
166                         count ++;
167                     }
168                     if(count>1){
169                         return id;
170                     }else{
171                         if(cursor.MoveToFirst()){
172                             id = cursor.GetString(cursor.GetColumnIndex(Android.Provider.ContactsContract.Contacts.InterfaceConsts.Id));
173                             System.Diagnostics.Debug.WriteLine(name + ":" + id);
174                         }
175                     }
176                 }
177             }catch(Exception e){
178 
179             }
180             return id;
181         }
182 
183     }
184 }
View Code

 

Forms Android Contacts

标签:

原文地址:http://www.cnblogs.com/techidea/p/4693397.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!