Ejemplo n.º 1
0
        public string GetContactName(SmsEntry smsEntry)
        {
            var phoneNumber = smsEntry.Address;
            if (_knownContacts.ContainsKey(phoneNumber))
            {
                return _knownContacts[phoneNumber];
            }

            var contactUri = Uri.WithAppendedPath(ContactsContract.PhoneLookup.ContentFilterUri, Uri.Encode(phoneNumber));
            using (var cursor = _context.ContentResolver.Query(contactUri, new[] {ContactsContract.PhoneLookup.InterfaceConsts.DisplayName}, null, null, null))
            {
                if (!cursor.MoveToFirst())
                {
                    return null;
                }

                var contactName = cursor.GetString(cursor.GetColumnIndex(ContactsContract.PhoneLookup.InterfaceConsts.DisplayName));
                _knownContacts.Add(phoneNumber, contactName);
                return contactName;
            }
        }
Ejemplo n.º 2
0
        public IEnumerable<SmsEntry> GetSmsMessagesFromDevice()
        {
            var inboxUri = Android.Net.Uri.Parse("content://sms");
            var columns = new[] { "_id", "thread_id", "address", "date", "body", "type" };

            using (var cursor = _inboxScreen.ContentResolver.Query(inboxUri, columns, null, null, null, null))
            {
                while (cursor.MoveToNext())
                {
                    var sms = new SmsEntry
                    {
                        Id = cursor.GetString(cursor.GetColumnIndex(columns[0])),
                        ThreadId = cursor.GetString(cursor.GetColumnIndex(columns[1])),
                        Address = cursor.GetString(cursor.GetColumnIndex(columns[2])),
                        Date = cursor.GetString(cursor.GetColumnIndex(columns[3])),
                        Message = cursor.GetString(cursor.GetColumnIndex(columns[4])),
                        Direction = (SmsDirection) int.Parse(cursor.GetString(cursor.GetColumnIndex(columns[5])))
                    };
                    yield return sms;
                }
            }
        }