Android:获取联系人的通话记录

时间:2011-06-22 21:06:19

标签: android calllog

我正在使用此代码获取联系人的联系信息(我也使用它 - 经过一些修改 - 来电,发送短信或通过电子邮件发送联系人)。我想知道是否有办法显示某个联系人的通话记录:

String name3是联系人姓名。

     contactinfobtn.setOnClickListener(new View.OnClickListener() 
     {
        public void onClick(View v) 
        { 
            ContentResolver cr = getContentResolver();
            Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

            if (cur.getCount() > 0) 
            {

                while (cur.moveToNext()) {
                id_contact = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                name_contact = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    if (name_contact.equals(name3))
                    {
                    Cursor pCur = cr.query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id_contact}, null);
                       id_contact2 = id_contact;

                        while (pCur.moveToNext()){
                        } 
                    pCur.close();
                    }
                }
                Intent intent_contacts = new Intent(Intent.ACTION_VIEW, Uri.parse("content://contacts/people/" + id_contact2));
                startActivity(intent_contacts);
            }
        }
     });

1 个答案:

答案 0 :(得分:3)

有一个名为CallLog.Calls的课程,其中包含CONTENT_URI来查询通话记录。

以下是列出通话记录中电话号码的示例:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.main);

    String[] projection = new String[] {
        CallLog.Calls._ID, CallLog.Calls.NUMBER};
    Cursor query = this.managedQuery(
        CallLog.Calls.CONTENT_URI, projection, null, null, null);

    ListAdapter adapter = new SimpleCursorAdapter(
        this, android.R.layout.simple_list_item_1, query,
        new String[] {CallLog.Calls.NUMBER},
        new int[] {android.R.id.text1});
    this.setListAdapter(adapter);
}