Android People.Number 和 People.Number_key 返回 null

标签 android

我正在尝试从电话中获取联系人,但我只能获取姓名,电话号码返回空值。

       Cursor cursor = getContentResolver().query(People.CONTENT_URI, null, null, null,People.NAME + " ASC");
  startManagingCursor(cursor);
  cursor.moveToFirst();
  if(cursor.getCount() > 0){
  while(cursor.moveToNext()){
    int idCol = cursor.getColumnIndex(People._ID);
          int nameCol = cursor.getColumnIndex(People.NAME);
          int numCol = cursor.getColumnIndex(People.NUMBER_KEY);

          String name = cursor.getString(nameCol);
          String number = cursor.getString(numCol);
    //mDbHelper.addContact(managedCursor.getString(1),managedCursor.getString(2));
    Log.v("contact.add",name+" - "+number);
  }

有什么想法吗?

大约3个小时后,我想通了:

   ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);
        if (cur.getCount() > 0) {
         cur.move(-1);
     while (cur.moveToNext()) {
         String id = cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts._ID));
  String name = cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
   if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
     if (Integer.parseInt(cur.getString(
                     cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                  Cursor pCur = cr.query(
         ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
         null, 
         ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
         new String[]{id}, null);
            while (pCur.moveToNext()) {

         Log.v("Phone",""+pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))+" - "+name);
         i++;
            } 
            pCur.close();
        }

          }
            }
  }

最佳答案

电话号码列是 People.NUMBER,而不是 People.NUMBER_KEY。

关于Android People.Number 和 People.Number_key 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3126219/

相关文章:

android - 用于打开带有越野地标显示的本地 Android 或 IOS map 的 HTML 链接

android - 是否可以通过网络浏览器通过 url 方案启动 android wifi 设置?

android - Android 的 webview 和 Chrome 可以使用 Google 的原生客户端吗?

java - 如何跳过唯一的用户 ID 并访问回收者 View 的子值

android - 加载 390k md2 模型时内存不足

android - 在不改变我的位置的情况下获取当前位置的经度和纬度

android - AsyncTask 从不执行 onPostExecute

android - Android JNI 信号/异常后发送报告

java - 在 Android 7 上显示 AlertDialog 时出现 WindowManager$BadTokenException

android - QR 配置后系统应用程序丢失