Android 手机联系人是不是有唯一标识符?

Posted

技术标签:

【中文标题】Android 手机联系人是不是有唯一标识符?【英文标题】:Is there a unique identifier for Android phone contacts?Android 手机联系人是否有唯一标识符? 【发布时间】:2021-12-18 16:33:40 【问题描述】:

我有一个应用程序可以读取手机的详细联系信息。当通过 Email.ContentUri 访问时,此代码返回 744 作为特定联系人行的 ID。

                            var uriEmail = ContactsContract.CommonDataKinds.Email.ContentUri;
                            string[] projectionEmail =  ContactsContract.Contacts.InterfaceConsts.Id, ContactsContract.Contacts.InterfaceConsts.DisplayName, ContactsContract.Contacts.InterfaceConsts.PhotoUri, ContactsContract.CommonDataKinds.Email.Address ;
                            var cursorEmail = this.Activity.ContentResolver.Query(uriEmail, projectionEmail, null, null, null);

                            // var contactList = new List<string>();
                            contacts = new ObservableCollection<Contact>();

                            if (cursorEmail.MoveToFirst())
                            
                                do
                                
                                    //contactList.Add(cursor.GetString(cursor.GetColumnIndex(projection[2])));
                                    contacts.Add(new Contact()
                                    
                                        Id = cursorEmail.GetInt(cursorEmail.GetColumnIndex(projectionEmail[0])),
                                        Name = cursorEmail.GetString(cursorEmail.GetColumnIndex(projectionEmail[1])),
                                        Photo = cursorEmail.GetString(cursorEmail.GetColumnIndex(projectionEmail[2])),
                                        Email = cursorEmail.GetString(cursorEmail.GetColumnIndex(projectionEmail[3])),
                                    );
                                
                                while (cursorEmail.MoveToNext());
                            

                            ListView listEmail = v.FindViewById<ListView>(Resource.Id.listViewSelect);

                            listEmail.Adapter = new ContactAdapter(v.Context, contacts);
                            listEmail.ItemClick += OnClientListClick;

此代码在通过 StructuredPostal.ContentUri 访问时返回 752 作为同一联系人的 id。

                        var uriAddress = ContactsContract.CommonDataKinds.StructuredPostal.ContentUri;
                        string[] projectionAddress =  ContactsContract.Contacts.InterfaceConsts.Id, ContactsContract.Contacts.InterfaceConsts.DisplayName, ContactsContract.Contacts.InterfaceConsts.PhotoUri, ContactsContract.CommonDataKinds.StructuredPostal.Street, ContactsContract.CommonDataKinds.StructuredPostal.Postcode ;
                        var cursorAddress = this.Activity.ContentResolver.Query(uriAddress, projectionAddress, null, null, null);

                        // var contactList = new List<string>();
                        properties = new ObservableCollection<Property>();

                        if (cursorAddress.MoveToFirst())
                        
                            do
                            
                                int n = cursorAddress.GetInt(cursorAddress.GetColumnIndex(projectionAddress[0]));
                                string str = cursorAddress.GetString(cursorAddress.GetColumnIndex(projectionAddress[1]));
                                if (n == nId)
                                
                                    //contactList.Add(cursor.GetString(cursor.GetColumnIndex(projection[2])));
                                    properties.Add(new Property()
                                    
                                        Id = cursorAddress.GetInt(cursorAddress.GetColumnIndex(projectionAddress[0])),
                                        Name = cursorAddress.GetString(cursorAddress.GetColumnIndex(projectionAddress[1])),
                                        Photo = cursorAddress.GetString(cursorAddress.GetColumnIndex(projectionAddress[2])),
                                        Street = cursorAddress.GetString(cursorAddress.GetColumnIndex(projectionAddress[3])),
                                        Postcode = cursorAddress.GetString(cursorAddress.GetColumnIndex(projectionAddress[4])),
                                    );
                                
                            
                            while (cursorAddress.MoveToNext());
                        

                        ListView listAddress = v.FindViewById<ListView>(Resource.Id.listViewSelect);

                        listAddress.Adapter = new PropertyAdapter(v.Context, properties);
                        listAddress.ItemClick += OnPropertyListClick;

是否有为 android 手机中的联系人分配的唯一标识符?

【问题讨论】:

【参考方案1】:

如果您想要一个唯一的 id,您可以使用 CONTACT_ID,它是对每个联系人的 _ID 的引用。

CONTACT_ID: https://developer.android.com/reference/android/provider/ContactsContract.RawContactsColumns.html#CONTACT_ID

_ID:https://developer.android.com/reference/android/provider/BaseColumns#_ID

如果你想使用唯一ID跨设备,你可以尝试使用LOOKUP_KEY。

LOOKUP_KEY: https://developer.android.google.cn/reference/android/provider/ContactsContract.ContactsColumns.html#LOOKUP_KEY

有关更多代码详细信息,您可以查看下面的链接。 Get cross-device unique ID for Android phone contacts

【讨论】:

谢谢温迪。我将从那里开始。

以上是关于Android 手机联系人是不是有唯一标识符?的主要内容,如果未能解决你的问题,请参考以下文章

如何获取Android唯一标识

如何获取Android唯一标识(唯一序列号)

Android 设备唯一标识

如何获取Android唯一标识

如何获取Android唯一标识

获取 Android 手机联系人的跨设备唯一 ID