Forms Android Contacts

  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,"[email protected]");
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 }

时间: 2024-10-13 18:58:45

Forms Android Contacts的相关文章

Android Contacts (android通讯录读取)-content provider

Content Provider 在数据处理中,Android通常使用Content Provider的方式.Content Provider使用Uri实例作为句柄的数据封装的,很方便地访问地进行数据的增.删.改.查的操作.Android并不提供所有应用共享的数据存储,采用content Provider,提供简单便捷的接口来保持和获取数据,也可以实现跨应用的数据访问.简单地说,Android通过content Provider从数据的封装中获取信息. Content provider使用Uri

Xamarin.Forms Android PopupMenu问题二

Xamarin.Forms Android PopupMenu问题二 在上一篇文章Xamarin.Android 使用PopupMenu遇到的问题文章中讲到了兼容Android 5.0及以下版本,但又带了一个新的问题.这个问题在所有Android版本App都会遇到,此时会抛出一个异常: Java.Lang.RuntimeException: Failed to resolve attribute at index 6 经过多番尝试(在Xamarin.Android中调试,原生Android中调试

关于android contacts 数据库操作

Android中的联系人都保存在一个SQLite数据库中,有兴趣的可以使用adb直接pull 出来看一下里面的表和视图的结构, 它的路径为:adb pull /data/data/com.android.providers.contacts/databases/contacts2.db 待续--

Forms Android Toast

1 forms定义接口 2 3 using System; 4 5 namespace 6 { 7 public interface ISystemToast 8 { 9 void DisplayToast(string message); 10 } 11 } 12 13 14 平台实现 15 16 using System; 17 18 using Android.App; 19 using Android.OS; 20 using Android.Views; 21 using Androi

Forms Android Bitmap 处理

1 forms 接口定义 2 3 using System; 4 using System.Collections.Generic; 5 using System.IO; 6 7 namespace 8 { 9 public interface IUtilPicture 10 { 11 List<string> ResizeBitmap(string imagePath); //图片采样缩小 12 //删除图片 13 void DeletePicture(string imagePath);

Forms Android System Download

1 不用自己写下载文件的功能,直接调用系统下载服务 2 forms 接口 3 4 using System; 5 using System.Collections; 6 using System.Collections.Generic; 7 8 namespace 9 { 10 public interface ISystemDownloadFile 11 { 12 //下载文件 13 void DownloadThread (string strUrl,string str1); 14 } 1

Forms Android WebView Renderer

1 forms 代码 2 3 using System; 4 using Xamarin.Forms; 5 6 namespace.Core 7 { 8 public class ContentWebView:WebView 9 { 10 //private ProgressBar progressbar; 11 12 public ContentWebView () 13 { 14 15 } 16 17 public string Url{ get; set;} 18 19 public st

Forms Android SMS

1 forms 定义接口 2 3 using System; 4 using System.Collections; 5 using System.Collections.Generic; 6 7 namespace 8 { 9 public interface ISystemSms 10 { 11 //发送短信 12 void SendSms (string num, string context); 13 } 14 } 15 16 平台实现 17 using System; 18 using

关于xamarin.forms Android创建文件与写文件 (ftp)

现在读写文件在Android原生态应该不在话下了.但是xamarin.forms应该如何用呢 1 //获取文件的名称含有后缀 2 string strName = Path.GetFileName(strPath); 3 strPath = "ftp://" + builder.UserName + ":" + builder.Password + "@" + builder.Host + strPath; 4 builder.AllPath =