【问题标题】:How to add a contact with multiple phone numbers in android via xamarin c#?如何通过xamarin c#在android中添加具有多个电话号码的联系人?
【发布时间】:2017-12-11 14:02:46
【问题描述】:

我正在尝试通过 xamarin c# 在 android 中添加具有多个电话号码的联系人。这是我要添加联系人的按钮。

我尝试了从传递数组到尝试单独添加第二个数字的所有方法,但后一种解决方案只是在实际保存之前覆盖第一个数字(如怀疑的那样)。

我在某处(可能在 xamarin 论坛上)读到,在 Xamarin.ios 中,我们可以一次性将所有电话号码添加为数组。

        private void test_Click(object sender, EventArgs e)
    {
        List<ContentProviderOperation> ops = new List<ContentProviderOperation>();

        ContentProviderOperation.Builder builder =
            ContentProviderOperation.NewInsert(RawContacts.ContentUri);
        builder.WithValue(RawContacts.InterfaceConsts.AccountType, null);
        builder.WithValue(RawContacts.InterfaceConsts.AccountName, null);
        ops.Add(builder.Build());

        //Name
        builder = ContentProviderOperation.NewInsert(Data.ContentUri);
        builder.WithValueBackReference(Data.InterfaceConsts.RawContactId, 0);
        builder.WithValue(Data.InterfaceConsts.Mimetype,
                          StructuredName.ContentItemType);
        builder.WithValue(StructuredName.FamilyName, "Added fname");
        builder.WithValue(StructuredName.GivenName, "firstName");
        ops.Add(builder.Build());

        List<string> a = new List<string>();
        a.Add("89892302398");
        a.Add("93823239232");
        //Number
        builder = ContentProviderOperation.NewInsert(Data.ContentUri);
        builder.WithValueBackReference(Data.InterfaceConsts.RawContactId, 0);
        builder.WithValue(Data.InterfaceConsts.Mimetype,
                          cont.ContentItemType);
        builder.WithValue(cont.Number,"83293982" ); //tried to pass array here a.ToArray()

        builder.WithValue(cont.InterfaceConsts.Type,
                          cont.InterfaceConsts.TypeCustom);
        builder.WithValue(cont.InterfaceConsts.Label, "Work");

        builder.WithValue(cont.InterfaceConsts.Number, "808004038");
        builder.WithValue(cont.InterfaceConsts.Type,
                          cont.InterfaceConsts.TypeCustom);
        builder.WithValue(cont.InterfaceConsts.Label, "Home");

        ops.Add(builder.Build());

        //Add the new contact
        ContentProviderResult[] res;
        try
        {
            res = ContentResolver.ApplyBatch(Authority, ops);

            Toast.MakeText(this, "contact saved !", ToastLength.Short).Show();
        }
        catch
        {
            Toast.MakeText(this, "contact not saved_message !", ToastLength.Long).Show();
        }

    }

在我的项目中,我试图从我之前创建的 csv 文件中恢复联系人,但唯一的问题是我无法保存具有多个号码的联系人。我应该看起来像:

How I want the contact to look like after it's added

更新: 这就是我的完整功能的样子:

        //Restore Contacts
        private void CreateCon_Click(object sender, EventArgs e)
    {
        try
        {
            Toast.MakeText(this, "Create Contact Clicked", ToastLength.Short).Show();
            //  TextView txtv = FindViewById<TextView>(Resource.Id.txt);

            List<Contact> contacts = new List<Contact>();
            List<Contact> ex_cons = new List<Contact>();
            List<Contact> new_cons = new List<Contact>();

            CursorLoader loader;
            PUMservice.Service1 s = new PUMservice.Service1();

            double loop;
            Boolean boo;

            s.GetLoopTimes("contacts", PhoneNumbers[0], out loop, out boo);
            Toast.MakeText(this, "loops : " + loop, ToastLength.Short).Show();

            for (int ij = 0; ij < loop; ij++)
            {
                ex_cons = ex_cons.Union(s.GetContacts(PhoneNumbers[0])).ToList();
            }

            Android.Net.Uri uri = contact.ContentUri;
            Android.Net.Uri uri2 = cont.ContentUri;

            string[] projection =
                        {
            contact.InterfaceConsts.Id,
            contact.InterfaceConsts.DisplayName,
            contact.InterfaceConsts.ContactLastUpdatedTimestamp
        };

            string[] projection2 =
            {
            cont.InterfaceConsts.ContactId,
            cont.Number,
            cont.InterfaceConsts.Type,
        };

            loader = new CursorLoader(this, uri, projection, null, null, "contact_last_updated_timestamp Asc");

            var cursor = (ICursor)loader.LoadInBackground();

            if (cursor.MoveToFirst())
            {
                do
                {
                    //Add call Logs
                    string id = cursor.GetLong(cursor.GetColumnIndex(projection[0])).ToString();
                    string name = cursor.GetString(cursor.GetColumnIndex(projection[1]));
                    string date = cursor.GetString(cursor.GetColumnIndex(projection[2]));

                    PUMservice.Contact c_temp = new PUMservice.Contact
                    {
                        Id = id,
                        Name = name,

                    };

                    var l2 = new CursorLoader(this, uri2, projection2, cont.InterfaceConsts.ContactId + " = ?", new string[] { id }, null);

                    var cursor2 = (ICursor)l2.LoadInBackground();
                    if (cursor2.MoveToFirst())
                    {
                        List<string> temp_no = new List<string>();
                        List<string> temp_type_list = new List<string>();
                        List<string> temp_numbers_list = new List<string>();

                        do
                        {
                            string number = cursor2.GetString(cursor2.GetColumnIndex(projection2[1]));
                            //string numb = number; // used so that original spaces and all for unique contacts can be maintained...just replace numb with number if you don't want it. for eg. 95-9223-5321 can be stored as it is 
                            int typ = cursor2.GetInt(cursor2.GetColumnIndex(projection2[2]));
                            string temp_type = typ.ToString();

                            number = (number.Replace("-", "")).Replace(" ", "");
                            if (!(temp_no.Contains(number)))
                            {           
                                temp_type_list.Add(temp_type);
                                temp_numbers_list.Add(number);
                                temp_no.Add(number);
                            }
                        } while (cursor2.MoveToNext());
                            c_temp.Type = temp_type_list.ToArray();
                            c_temp.Number = temp_numbers_list.ToArray();
                    }
                                              contacts.Add(c_temp);
                } while (cursor.MoveToNext());
            }
            int i = 1;
            foreach(var single_con in ex_cons)
            {
                if(!(contacts.Exists(c=> c.Name == single_con.Name && c.Number.SequenceEqual(single_con.Number) && c.Type.SequenceEqual(single_con.Type))))
                {
                    i++;
                    //add the contact
                    // new_cons.Add(single_con);
                    List<ContentProviderOperation> ops = new List<ContentProviderOperation>();

                    ContentProviderOperation.Builder builder =
                    ContentProviderOperation.NewInsert(RawContacts.ContentUri);
                    builder.WithValue(RawContacts.InterfaceConsts.AccountType, null);
                    builder.WithValue(RawContacts.InterfaceConsts.AccountName, null);
                    ops.Add(builder.Build());

                    //Name
                    builder = ContentProviderOperation.NewInsert(Data.ContentUri);
                    builder.WithValueBackReference(Data.InterfaceConsts.RawContactId, Convert.ToInt32(single_con.Id));
                    builder.WithValue(Data.InterfaceConsts.Mimetype,
                                      StructuredName.ContentItemType);
                    builder.WithValue(StructuredName.DisplayName, "Added "+single_con.Name);
                    ops.Add(builder.Build());

                    //Number
                    for( int ix=0;i<single_con.Type.Length;i++)
                    {
                        builder = ContentProviderOperation.NewInsert(Data.ContentUri);
                        builder.WithValueBackReference(Data.InterfaceConsts.RawContactId, Convert.ToInt32(single_con.Id));
                        builder.WithValue(Data.InterfaceConsts.Mimetype,
                                          cont.ContentItemType);
                        builder.WithValue(cont.Number, single_con.Number[ix]);
                        builder.WithValue(cont.InterfaceConsts.Type,single_con.Type[ix]);
                        ops.Add(builder.Build());
                    }

                    //Add the new contact
                    ContentProviderResult[] res;
                    try
                    {
                        res = ContentResolver.ApplyBatch(Authority, ops);

                        Toast.MakeText(this, "Hahaahahaha contact saved ! "+i, ToastLength.Short).Show();
                    }
                    catch
                    {
                        Toast.MakeText(this, "contact not saved_message ! " + i, ToastLength.Long).Show();
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Toast.MakeText(this, ex.Message, ToastLength.Short).Show();
        }
    }

【问题讨论】:

    标签: android xamarin xamarin.android contacts phone-number


    【解决方案1】:

    如何通过xamarin c#在android中添加多个电话号码的联系人?

    像这样修改您的NewContact 代码:

    public void NewContact(ref List<ContentProviderOperation> ops, string displayName, string Number1, string Number2, string Number3, string Number4)
        {
            ContentProviderOperation.Builder builder =
                ContentProviderOperation.NewInsert(ContactsContract.RawContacts.ContentUri);
            builder.WithValue(ContactsContract.RawContacts.InterfaceConsts.AccountType, null);
            builder.WithValue(ContactsContract.RawContacts.InterfaceConsts.AccountName, null);
            ops.Add(builder.Build());
    
            //Name  
            builder = ContentProviderOperation.NewInsert(ContactsContract.Data.ContentUri);
            builder.WithValueBackReference(ContactsContract.Data.InterfaceConsts.RawContactId, 0);
            builder.WithValue(ContactsContract.Data.InterfaceConsts.Mimetype,
                              ContactsContract.CommonDataKinds.StructuredName.ContentItemType);
            builder.WithValue(ContactsContract.CommonDataKinds.StructuredName.DisplayName, displayName);
            //builder.WithValue(ContactsContract.CommonDataKinds.StructuredName.GivenName, firstName);  
            ops.Add(builder.Build());
    
            //Number1  
            builder = ContentProviderOperation.NewInsert(ContactsContract.Data.ContentUri);
            builder.WithValueBackReference(ContactsContract.Data.InterfaceConsts.RawContactId, 0);
            builder.WithValue(ContactsContract.Data.InterfaceConsts.Mimetype,
                              ContactsContract.CommonDataKinds.Phone.ContentItemType);
            builder.WithValue(ContactsContract.CommonDataKinds.Phone.Number, Number1);
            builder.WithValue(ContactsContract.CommonDataKinds.Phone.InterfaceConsts.Type,
                              ContactsContract.CommonDataKinds.Phone.InterfaceConsts.TypeCustom);
            builder.WithValue(ContactsContract.CommonDataKinds.Phone.InterfaceConsts.Data2, (int)PhoneDataKind.Mobile);
    
            ops.Add(builder.Build());
            //Number2  
            if (!string.IsNullOrEmpty(Number2))
            {
                builder = ContentProviderOperation.NewInsert(ContactsContract.Data.ContentUri);
                builder.WithValueBackReference(ContactsContract.Data.InterfaceConsts.RawContactId, 0);
                builder.WithValue(ContactsContract.Data.InterfaceConsts.Mimetype,
                                  ContactsContract.CommonDataKinds.Phone.ContentItemType);
                builder.WithValue(ContactsContract.CommonDataKinds.Phone.Number, Number2);
                builder.WithValue(ContactsContract.CommonDataKinds.Phone.InterfaceConsts.Type,
                                  ContactsContract.CommonDataKinds.Phone.InterfaceConsts.TypeCustom);
                builder.WithValue(ContactsContract.CommonDataKinds.Phone.InterfaceConsts.Data2, (int)PhoneDataKind.Mobile);
                ops.Add(builder.Build());
            }
    
            if (!string.IsNullOrEmpty(Number3))
            {
                builder = ContentProviderOperation.NewInsert(ContactsContract.Data.ContentUri);
                builder.WithValueBackReference(ContactsContract.Data.InterfaceConsts.RawContactId, 0);
                builder.WithValue(ContactsContract.Data.InterfaceConsts.Mimetype,
                                  ContactsContract.CommonDataKinds.Phone.ContentItemType);
                builder.WithValue(ContactsContract.CommonDataKinds.Phone.Number, Number3);
                builder.WithValue(ContactsContract.CommonDataKinds.Phone.InterfaceConsts.Type,
                                  ContactsContract.CommonDataKinds.Phone.InterfaceConsts.TypeCustom);
                builder.WithValue(ContactsContract.CommonDataKinds.Phone.InterfaceConsts.Data2, (int)PhoneDataKind.Mobile);
                ops.Add(builder.Build());
            }
    
            if (!string.IsNullOrEmpty(Number4))
            {
                builder = ContentProviderOperation.NewInsert(ContactsContract.Data.ContentUri);
                builder.WithValueBackReference(ContactsContract.Data.InterfaceConsts.RawContactId, 0);
                builder.WithValue(ContactsContract.Data.InterfaceConsts.Mimetype,
                                  ContactsContract.CommonDataKinds.Phone.ContentItemType);
                builder.WithValue(ContactsContract.CommonDataKinds.Phone.Number, Number4);
                builder.WithValue(ContactsContract.CommonDataKinds.Phone.InterfaceConsts.Type,
                                  ContactsContract.CommonDataKinds.Phone.InterfaceConsts.TypeCustom);
                builder.WithValue(ContactsContract.CommonDataKinds.Phone.InterfaceConsts.Data2, (int)PhoneDataKind.Mobile);
                ops.Add(builder.Build());
            }
        }
    

    然后当你使用这个方法添加联系人:

    new_Contact_Button.Click += (sender, args) =>
    {
    
        List<ContentProviderOperation> ops = new List<ContentProviderOperation>();
        NewContact(ref ops, "lizi", "1234" , "2234", "3234", "4234");
    
        //Add the new contact
        ContentProviderResult[] res;
        try
        {
            res = ContentResolver.ApplyBatch(Authority, ops);
            ops.Clear();//Add this line   
            Toast.MakeText(this, "contact saved !", ToastLength.Short).Show();
        }
        catch
        {
            Toast.MakeText(this, "contact not saved_message !", ToastLength.Long).Show();
        }
    }
    

    效果在我的Mi 5s,在我的Huawei Nexus 6P

    更新:

    保存联系人时添加 ops.Clear()

    public void SaveContacts(string filename)  
    {  
        var documentsPath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads);  
        var filePath = Path.Combine(documentsPath.AbsolutePath, filename);  
        var fileContent = File.ReadAllLines(filePath);  
    
        List<ContentProviderOperation> ops = new List<ContentProviderOperation>();  
    
        foreach (var strLine in fileContent)  
        {  
            if (string.IsNullOrEmpty(strLine))  
                continue;  
            var array = strLine.Split(new string[] { "\t", ":" }, StringSplitOptions.RemoveEmptyEntries);  
    
            NewContact(...);  
    
            //Add the new contact  
            ContentProviderResult[] res;  
            res = ContentResolver.ApplyBatch(ContactsContract.Authority, ops);  
            ops.Clear();   
        }  
    }  
        
    

    【讨论】:

    • @kashishjhaveri,当您添加单个联系人时,它有效吗?
    • 我昨天也在修改代码以达到类似效果时也做了一些事情。我按照您的建议进行了循环,并且能够输入单个联系人;但是当我尝试使用此代码恢复多个联系人时,我失败了。吐司说联系人已保存但没有任何反应。你知道为什么会这样吗?在恢复联系人之前,我将使用我检查的 if 条件更新我的问题。谢谢你的回答这是我原来问题的完美解决方案,所以我接受它。如果你能检查我的 if 条件是否适用于检查,我将不胜感激。
    • if(!(contacts.Exists(c=> c.Name == single_con.Name && c.Number.SequenceEqual(single_con.Number) && c.Type.SequenceEqual(single_con.Type)) ))
    • 在上述条件下Contacts是设备上的联系人列表; single_contact 来自从 csv 获取的联系人列表的 foreach 循环;并且 Type 和 Number 是联系人拥有的号码及其类型的列表,因此 number[0] 是 single_contact 的类型 [0]。提前致谢
    • @kashishjhaveri,您需要在 foreach 循环中添加多少联系人?
    猜你喜欢
    • 2014-05-22
    • 2017-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    • 2011-07-30
    • 1970-01-01
    • 2023-03-22
    相关资源
    最近更新 更多