Xamarin Forms: Android Phone contacts is not listing

Sreejith Sree 1,251 Reputation points
2021-06-01T08:54:39.487+00:00

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.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,377 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 81,191 Reputation points Microsoft External Staff
    2021-06-01T09:51:47.28+00:00

    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.