3

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);
            }
        }
     });
erdomester
  • 11,789
  • 32
  • 132
  • 234

1 Answers1

4

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);
}
Cristian
  • 42,563
  • 25
  • 88
  • 99
  • Thanks. I haven't tried this but does this jump to the calllog of a certain contact? 'Cos that's what i want. Btw, it says "CallLog cannot be resolved to a variable". I'm using Android 2.1 – erdomester Jun 23 '11 at 05:46
  • I figured it out :). Looped through all contacts and the key line was final String selection = android.provider.CallLog.Calls.NUMBER + "=" + "'" + contactnumbers.get(i) + "'"; – erdomester Oct 18 '11 at 19:00
  • Cristian, Can you please kindly help me with similar thing. I am facing issue with getting call logs for dual sim android devices. Here is SO link.. http://stackoverflow.com/questions/23115782/call-logs-for-dual-sim-android-device – Scorpion Apr 17 '14 at 02:45
  • @Scorpion Unfortunately, I don't have nor have worked with a dual sim android device. I don't know how to get that to work. – Cristian Apr 23 '14 at 20:16