Xamarin android阅读联系人和数字

时间:2016-10-29 22:48:06

标签: android c#-4.0 xamarin.android

我知道要检索下面代码的联系人姓名。但是,我应该在此代码中更改哪些内容还包含与联系人列表相关联的电话号码?

  var uri = ContactsContract.Contacts.ContentUri;

            string[] projection = { ContactsContract.Contacts.InterfaceConsts.Id,
   ContactsContract.Contacts.InterfaceConsts.DisplayName };

            var cursor = ManagedQuery(uri, projection, null, null, null);

            var contactList = new List<string>();

            if (cursor.MoveToFirst())
            {
                do
                {
                    String phoneNumber = cursor.GetString(cursor.GetColumnIndex(ContactsContract.CommonDataKinds.Phone.Number));

                contactList.Add(cursor.GetString(
                        cursor.GetColumnIndex(projection[1])));
            } while (cursor.MoveToNext());
        }                

1 个答案:

答案 0 :(得分:0)

我最近写了类似的代码时遇到了你的问题。检索电话号码唯一需要的是:

  1. 向Uri提出另一个查询 ContactsContract.CommonDataKinds.Phone.ContentUri;
  2. 将投影设置为包括ContactsContract.CommonDataKinds.Phone.Number;和
  3. 根据id选择,即选择参数应包括&#34; _id =&#34; + 的ContactID
  4. 您的查询应如下所示:

    private List<Contact> GetContactList()
    {
        List<Contact> contacts = new List<Contact>();
        string[] projection = {
                    ContactsContract.Contacts.InterfaceConsts.Id,
                    ContactsContract.Contacts.InterfaceConsts.DisplayName,
                    ContactsContract.Contacts.InterfaceConsts.PhotoUri
                };
        var uri = ContactsContract.Contacts.ContentUri;
        ICursor cursor = ContentResolver.Query(uri, projection, null, null, null);
    
        if (cursor.MoveToFirst())
        {
            do
            {
                string id = cursor.GetString(cursor.GetColumnIndex(projection[0]));
                string name = cursor.GetString(cursor.GetColumnIndex(projection[1]));
                string photoUri = cursor.GetString(cursor.GetColumnIndex(projection[2]));
                contacts.Add(new Contact() { Id = long.Parse(id), DisplayName = name, PhotoUri = photoUri });
            } while (cursor.MoveToNext());
    
            GetContactPhoneNumber(contacts);
        }
    
        return contacts;
    }
    
    private async void GetContactPhoneNumber(List<Contact> list)
    {
        string[] projection =
            {
                        ContactsContract.CommonDataKinds.Phone.Number,
                        ContactsContract.CommonDataKinds.Phone.InterfaceConsts.ContactId
                    };
    
        var cursor = ContentResolver.Query(ContactsContract.CommonDataKinds.Phone.ContentUri, projection, null, null, null);
        if (cursor.Count > 0)
        {
            await Task.Factory.StartNew(() =>
            {
                do
                {
                    try
                    {
                        string id = cursor.GetString(cursor.GetColumnIndex(projection[1]));
                        string phoneNumber = cursor.GetString(cursor.GetColumnIndex(projection[0]));
                        Contact contact = list.Where(c => c.Id == long.Parse(id)).FirstOrDefault();
                        contact.PhoneNumber = phoneNumber;
                    }
                    catch
                    {
                    }
    
                } while (cursor.MoveToNext());
                cursor.Close();
            });
        }
    }
    

    此查询将为您提供联系人ID的电话号码,但在下面的代码中,由于性能问题,我只拨打了一个电话号码,然后为每个合同分配了号码。希望它有所帮助。

    procedure TDropboxSaveFilterForm.TestButtonClick(Sender: TObject);
    const
      AccessToken = 'Hq7XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
    var
      IdHTTP: TIdHTTP;
      Source: TStringList;
      Res, URL: WideString;
    begin
      Source := TStringList.Create;
      IdHTTP := TIdHTTP.Create(nil);
      IdHTTP.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(IdHTTP);
      URL := 'https://api.dropboxapi.com/2/files/list_folder' + '?' +
             'Authorization=Bearer ' + AccessToken;
      Res := IdHTTP.Post(URL, Source);
      Source.Free;
    end;