Android HAS_PHONE_NUMBER

发布于 2024-09-13 08:25:29 字数 664 浏览 6 评论 0原文

我正在尝试编写一种方法来确定联系人是否至少有一个电话号码,目前我有这样的情况:

public boolean hasPhone() {
 Cursor phones = this.map.getContentResolver().query(
  ContactsContract.Contacts.CONTENT_URI,
  null,
  ContactsContract.Contacts._ID + "=" + this.contactId,
  null,
  null
 );
 boolean has = false;
 if(phones.moveToFirst()) {
  do {        
    if(Integer.parseInt(phones.getString(phones.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
      has = true;
      break;
    }
  } while(phones.moveToNext());
 }
 return has;
}

该方法总是返回 false,即使我知道相关联系人有电话号码。我也知道 contactId 是正确的,因为我也用它来获取邮政地址等。

任何帮助将不胜感激,因为我快要抓狂了:p

谢谢。

I am trying to write a method that determines if a contact has at least one phone number, at the moment I have this:

public boolean hasPhone() {
 Cursor phones = this.map.getContentResolver().query(
  ContactsContract.Contacts.CONTENT_URI,
  null,
  ContactsContract.Contacts._ID + "=" + this.contactId,
  null,
  null
 );
 boolean has = false;
 if(phones.moveToFirst()) {
  do {        
    if(Integer.parseInt(phones.getString(phones.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
      has = true;
      break;
    }
  } while(phones.moveToNext());
 }
 return has;
}

the method always returns false, even though I know the contact in question has a phone number. Also I know the contactId is correct as I also use it to get the postal address, etc.

Any help would be greatly appreciated, as I am about to tear my hair out :p

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

樱桃奶球 2024-09-20 08:25:29

试试这个。将以下行替换

ContactsContract.Contacts._ID + "=" + this.contactId,

为这一行

ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + this.contactId,

看看是否有效。

Try this. Replace the following line

ContactsContract.Contacts._ID + "=" + this.contactId,

with this line

ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + this.contactId,

See if that works.

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