Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How to get a contact from call log?

I am trying to get contacts from call log. I can get the contact numbers from main contacts using this code :

    public void getContacts(View view) {

    Intent intentContact = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
    startActivityForResult(intentContact, 0);

}

public void onActivityResult(int requestCode, int resultCode, Intent intent)
{

    if (requestCode == 0)
    {
        try {
        to.setText(getContactInfo(intent));
        } catch(NullPointerException e) {
                 // Do nothing ;)
        }

    }

}
protected String getContactInfo(Intent intent)
{
    String phoneNumber = to.getText().toString();
    Cursor cursor =  managedQuery(intent.getData(), null, null, null, null);
    while (cursor.moveToNext())
    {
        String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
       String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
      if(phoneNumber.endsWith(">"))
          phoneNumber += ", "+name;
        else
         phoneNumber += name;
        String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

        if ( hasPhone.equalsIgnoreCase("1"))
            hasPhone = "true";
        else
            hasPhone = "false" ;

        if (Boolean.parseBoolean(hasPhone))


        {
            Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
            while (phones.moveToNext())
            {   phoneNumber = phoneNumber + " <" + phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))+">";


               }
            phones.close();
        }


    }
    cursor.close();
    return phoneNumber;
}

What this does is when we click a "Contact" button it open a list with all the contacts, the user can select any contact and that selected contact will be added in the "To" field. I want to do the exactly same thing, but instead of displaying all the contacts i want to display only those who were recently used (call log) for selection.

Also it would be nice if you can tell how to do this with groups also.

like image 824
Ayush Pateria Avatar asked Jan 08 '12 06:01

Ayush Pateria


1 Answers

I got this going using my own version. i used a dialog and handed it the cursor to the call log. Here is the function:

public void getCallLog() {

    String[] callLogFields = { android.provider.CallLog.Calls._ID,
            android.provider.CallLog.Calls.NUMBER,
            android.provider.CallLog.Calls.CACHED_NAME /* im not using the name but you can*/};
    String viaOrder = android.provider.CallLog.Calls.DATE + " DESC";
    String WHERE = android.provider.CallLog.Calls.NUMBER + " >0"; /*filter out private/unknown numbers */

    final Cursor callLog_cursor = getActivity().getContentResolver().query(
            android.provider.CallLog.Calls.CONTENT_URI, callLogFields,
            WHERE, null, viaOrder);

    AlertDialog.Builder myversionOfCallLog = new AlertDialog.Builder(
            getActivity());

    android.content.DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialogInterface, int item) {
            callLog_cursor.moveToPosition(item);

            Log.v("number", callLog_cursor.getString(callLog_cursor
                    .getColumnIndex(android.provider.CallLog.Calls.NUMBER)));

            callLog_cursor.close();

        }
    };
    myversionOfCallLog.setCursor(callLog_cursor, listener,
            android.provider.CallLog.Calls.NUMBER);
    myversionOfCallLog.setTitle("Choose from Call Log");
    myversionOfCallLog.create().show();
}
like image 188
j2emanue Avatar answered Sep 22 '22 11:09

j2emanue