question

SreejithSree-2948 avatar image
0 Votes"
SreejithSree-2948 asked SreejithSree-2948 commented

Xamarin Forms: Android Phone contacts is not listing

I am referring to this blog for listing the phone contacts. It is working fine on the ios part but on android part, the contacts are not listing. There are no exceptions or errors but the UI is blank.

As per the blog I have done the below things on the android platform:
1. created the model class Contact and interface IContactsService.
2. Added READ_CONTACTS permission and added ContactsService implementation.
3. Installed Plugin.CurrentActivity and Acr.UserDialogs packages.
4. Added Permission.Util class into the Android project.
5. Added required things on the MainActivity and ContactPage files on the Main project.

Don't know what I am missing on the android part, on ios it is working fine. On android, the contact permission is not asking during runtime. I manually add the permission from the app settings, but no luck. My Xamarin forms version: 4.8.0.1821

I am uploading a sample project here for reference.

Thanks in advance.


dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LeonLu-MSFT avatar image
1 Vote"
LeonLu-MSFT answered SreejithSree-2948 commented

Hello,​

Welcome to our Microsoft Q&A platform!

Because android 10 do not use Android.Support.V4.Content.ContextCompat to request permission, so please use Xamarin.Essentials: Permissions to request runtime permission.

Please open the ContactsViewModel.cs, add CheckAndContactsReadPermission()method in LoadContacts method like following code.

async Task LoadContacts()
        {
            try
            {
                await CheckAndContactsReadPermission();
                await _contactService.RetrieveContactsAsync();
            }
            catch (TaskCanceledException)
            {
                Console.WriteLine("Task was cancelled");
            }
        }

        public async Task<PermissionStatus> CheckAndContactsReadPermission()
        {
            var status = await Permissions.CheckStatusAsync<Permissions.ContactsRead>();

            if (status == PermissionStatus.Granted)
                return status;

            if (status == PermissionStatus.Denied && DeviceInfo.Platform == DevicePlatform.iOS)
            {
                // Prompt the user to turn on in settings
                // On iOS once a permission has been denied it may not be requested again from the application
                return status;
            }

           

            status = await Permissions.RequestAsync<Permissions.ContactsRead>();

            return status;
        }


Then open ContactsService.cs, please change the LoadContactsAsync method like following code.

async Task<IList<Contact>> LoadContactsAsync()
        {
            IList<Contact> contacts = new List<Contact>();
            //var hasPermission = await RequestPermissionAsync();
            //if (hasPermission)
            //{
                var uri = ContactsContract.Contacts.ContentUri;
                var ctx = Application.Context;
                await Task.Run(() =>
                {
                    var cursor = ctx.ApplicationContext.ContentResolver.Query(uri, new string[]
                    {
                        ContactsContract.Contacts.InterfaceConsts.Id,
                        ContactsContract.Contacts.InterfaceConsts.DisplayName,
                        ContactsContract.Contacts.InterfaceConsts.PhotoThumbnailUri
                    }, null, null, $"{ContactsContract.Contacts.InterfaceConsts.DisplayName} ASC");
                    if (cursor.Count > 0)
                    {
                        while (cursor.MoveToNext())
                        {
                            var contact = CreateContact(cursor, ctx);

                            if (!string.IsNullOrWhiteSpace(contact.Name))
                            {
                                OnContactLoaded?.Invoke(this, new ContactEventArgs(contact));
                                contacts.Add(contact);
                            }

                            if (stopLoad)
                                break;
                        }
                    }
                });

           // }
            return contacts;

        }

Here is running screenshot.

101339-image.png

Best Regards,

Leon Lu



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



image.png (16.4 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you very much

0 Votes 0 ·