如何将所有电话号码从通话记录中获取到Android中的自定义列表视图?

时间:2015-04-25 05:18:16

标签: android list listview calllog

我想访问Android手机通话记录中的所有电话号码到自定义列表视图。自定义ListView已经创建并且可以正常访问来自电话联系人的所有电话号码。可以解释一下吗?

1 个答案:

答案 0 :(得分:0)

Try this

     ContentResolver cr = getContentResolver();
            Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                    null, null, null, null);
            if (cur.getCount() > 0) {
                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) {
                         Cursor pCur = cr.query(
                                   ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                   null,
                                   ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                                   new String[]{id}, null);
                         while (pCur.moveToNext()) {
                             String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                             Toast.makeText(ArticleActivity.this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();
                         }
                        pCur.close();
                    }
                }
            }