Android 获取随机联系人
我试图从手机的联系人列表中获取随机联系人,但不会明显减慢手机速度。这意味着我不能只抓取所有联系人并将它们放入一个数组中,然后从该数组中随机选择一个。我希望能够获得随机联系人,而不必先获得所有联系人。 这可能吗?如果可以,我将如何去做?
I'm trying to get a random contact from a phone's contact list, but without noticeably slowing down the phone. That means that I can't just grab all the contacts and stick them into an array and pick a random one from that array. I'd like to be able to get a random contact without having to get all of the contacts first.
Is this possible, and if so, how would I go about doing it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新为使用未弃用的代码。基于此答案的查询:如何在Android上读取联系人2.0
然后只需获取光标的大小:
随机获取一个光标,读取它并检查它是否有电话号码。如果没有,请选择另一个:
否则我不知道如何随机选择一个。除非联系人列表非常大,否则这不会减慢手机的速度。
现在,它会检查电话号码是否存在,并在找到时读取所有电话号码。如果没有,它会选择另一个条目。
Updated to use non-deprecated code. Query based on this answer: How to read contacts on Android 2.0
Then it's just a matter of getting the size of the Cursor:
get a random one, read it and check if it has phone numbers. If not, choose another one:
I don't see how you could pick a random one otherwise. And this should not slow the phone down unless it's a really large contacts list.
Now it checks for the presence of phone numbers and reads all of them if found. If not, it chooses another entry.