Android 获取随机联系人

发布于 2024-11-03 02:28:08 字数 124 浏览 0 评论 0原文

我试图从手机的联系人列表中获取随机联系人,但不会明显减慢手机速度。这意味着我不能只抓取所有联系人并将它们放入一个数组中,然后从该数组中随机选择一个。我希望能够获得随机联系人,而不必先获得所有联系人。 这可能吗?如果可以,我将如何去做?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兰花执着 2024-11-10 02:28:08

更新为使用未弃用的代码。基于此答案的查询:如何在Android上读取联系人2.0

Cursor managedCursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null); 

然后只需获取光标的大小:

int size = managedCursor.getCount();

随机获取一个光标,读取它并检查它是否有电话号码。如果没有,请选择另一个:

boolean found = false;
Random rnd = new Random();
while(!found) {
  int index = rnd.nextInt(size);    
  managedCursor.moveToPosition(index);
  String name = managedCursor.getString(people.getColumnIndex(PhoneLookup.DISPLAY_NAME));
  found = Boolean.parseBoolean(managedCursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))); 
  if (found) {
    Cursor phones = getContentResolver().query( 
    ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 
    ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null); 
    while (phones.moveToNext()) { 
     String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));
     Log.d("Phone found:", phoneNumber);                 
    } 
    phones.close();
  }
}

否则我不知道如何随机选择一个。除非联系人列表非常大,否则这不会减慢手机的速度。

现在,它会检查电话号码是否存在,并在找到时读取所有电话号码。如果没有,它会选择另一个条目。

Updated to use non-deprecated code. Query based on this answer: How to read contacts on Android 2.0

Cursor managedCursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null); 

Then it's just a matter of getting the size of the Cursor:

int size = managedCursor.getCount();

get a random one, read it and check if it has phone numbers. If not, choose another one:

boolean found = false;
Random rnd = new Random();
while(!found) {
  int index = rnd.nextInt(size);    
  managedCursor.moveToPosition(index);
  String name = managedCursor.getString(people.getColumnIndex(PhoneLookup.DISPLAY_NAME));
  found = Boolean.parseBoolean(managedCursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))); 
  if (found) {
    Cursor phones = getContentResolver().query( 
    ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 
    ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null); 
    while (phones.moveToNext()) { 
     String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));
     Log.d("Phone found:", phoneNumber);                 
    } 
    phones.close();
  }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文