Android: get call history of contact

女生的网名这么多〃 提交于 2019-12-18 07:02:49

问题


i am using this code the get to the contact info of a contact (i am using it also - with a few modifications - to call, send sms or email the contact). I wonder if there is a way to show the call history of a certain contact:

String name3 is the contact name.

     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:


There is a class called CallLog.Calls that contains the CONTENT_URI to query for call history.

Here's an example listing the phone numbers in the call log:

@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);
}


来源:https://stackoverflow.com/questions/6446580/android-get-call-history-of-contact

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!