【问题标题】:How do you retrieve an ID from a phone number?如何从电话号码中检索 ID?
【发布时间】:2011-05-14 10:03:12
【问题描述】:

我正在尝试检索 电话 号码,因此使用

String addrWhere = Contacts.Phones.NUMBER + " = " + userNumber;
  String id = "";
  Cursor c = mContext.getContentResolver().query(
    Contacts.Phones.CONTENT_URI,
    new String[] { Contacts.Phones._ID }, addrWhere, null, null);
  try {
   if (c.getCount() > 0) {
    c.moveToFirst();
    id = c.getString(0);
    Log.i("IDS", id);
   }
  } finally {
   c.close();
  }
  return id;

谁能告诉我我在这方面的错误?

【问题讨论】:

    标签: android android-contacts phone-number


    【解决方案1】:

    尝试How to query ContactsContract.CommonDataKinds.Phone on Android? 的解决方案,即ContactsContract.PhoneLookup 提供者的用法:

    Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
    resolver.query(uri, new String[]{PhoneLookup.DISPLAY_NAME,...
    

    【讨论】:

    【解决方案2】:

    大家好... 谢谢你的答复!!! @索塔帕纳 好吧,我找到了 Sotapanna 指出的答案

    为需要的人粘贴工作代码!

    private String findID(String userNumber) {
            Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri
                    .encode(userNumber));
            int id = 0;
            String[] returnVals = new String[] { PhoneLookup._ID };
            Cursor pCur = mContext.getContentResolver().query(uri, returnVals,
                    PhoneLookup.NUMBER + " = \"" + userNumber + "\"", null, null);
            if (pCur.getCount() > 0) {
                pCur.moveToFirst();
                id = pCur.getColumnCount();
                if (id >= 0) {
                    id = pCur.getInt(0);
                }
            }
    
            Log.i("Contacts", "" + id);
            return String.valueOf(id);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-21
      • 1970-01-01
      相关资源
      最近更新 更多