4
votes

I am new to android and working on an application where I need the all the outgoing call logs, number, call duration and Name of the contact. So my question is can I get the Name and number of the outgoing call for the CallLog.Calls.CONTENT_URI table of android system or I need to read it from separate table and map it. Below is my code. Thanks in advance.

private String getCallDetails() {

        StringBuffer sb = new StringBuffer();
        // Cursor managedCursor =
        // getContentResolver().query(CallLog.Calls.CONTENT_URI, null,
        // null, null, null);

        Cursor managedCursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, CallLog.Calls.DATE + ">?",
                new String[] { String.valueOf("1451586601000") }, CallLog.Calls.NUMBER + " asc");
        int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
        int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
        int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
        int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
        int name = managedCursor.getColumnIndex(CallLog.Calls.CACHED_NAME);
        // int geoCodeColumn =
        // managedCursor.getColumnIndex(CallLog.Calls.GEOCODED_LOCATION);

        // sb.append("Call Details :");
        while (managedCursor.moveToNext()) {
            String phNumber = managedCursor.getString(number);
            String callType = managedCursor.getString(type);
            String callDate = managedCursor.getString(date);
            String callerName = managedCursor.getString(name);
            // long calldate_timeStamp= Long.parseLong(callDate);
            // long temp_time = 1451586601000L;
            // if(calldate_timeStamp>temp_time){
            // String geoCode = managedCursor.getString(geoCodeColumn);
            Date callDayTime = new Date(Long.valueOf(callDate));
            String callDuration = managedCursor.getString(duration);
            String dir = null;
            int dircode = Integer.parseInt(callType);
            switch (dircode) {
            case CallLog.Calls.OUTGOING_TYPE:
                dir = "OUTGOING";
                int total_call_duration = Integer.parseInt(callDuration);
                total_time = total_time + total_call_duration;
                MyContact dialedContact = new MyContact();
                dialedContact.setPhoneNumber(Long.parseLong(phNumber));
                dialedContact.setCallDuration(Integer.parseInt(callDuration));
                // dialedContact.se
                 sb.append("\nPhone Number:--- " + phNumber + " \nCallType:--- "
                 + dir + " \nCall Date:--- " + callDayTime
                 + " \nCall duration in sec :--- " + callDuration+ " \nGeocode: " );
                 sb.append("\n----------------------------------");

                break;

            case CallLog.Calls.INCOMING_TYPE:
                dir = "INCOMING";
                break;

            case CallLog.Calls.MISSED_TYPE:
                dir = "MISSED";
                break;
            }
        }

        // }
        managedCursor.close();
    //  sb.append("" + total_time / 60);// call duration in minute
        return sb.toString();

    }
1

1 Answers

0
votes

You need:

1) Permissions

Add a permission to read contacts data to your application manifest.

2) Calling the Contact Picker

Within your Activity, create an Intent that asks the system to find an Activity that can perform a PICK action from the items in the Contacts URI.

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); Call startActivityForResult, passing in this Intent (and a request code integer, PICK_CONTACT in this example). This will cause Android to launch an Activity that's registered to support ACTION_PICK on the People.CONTENT_URI, then return to this Activity when the selection is made (or canceled).

startActivityForResult(intent, PICK_CONTACT);

3) Listening for the Result

Also in your Activity, override the onActivityResult method to listen for the return from the 'select a contact' Activity you launched in step 2. You should check that the returned request code matches the value you're expecting, and that the result code is RESULT_OK.

You can get the URI of the selected contact by calling getData() on the data Intent parameter. To get the name of the selected contact you need to use that URI to create a new query and extract the name from the returned cursor.

@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
  super.onActivityResult(reqCode, resultCode, data); 
switch (reqCode) {
case (PICK_CONTACT) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c =  getContentResolver().query(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// TODO Whatever you want to do with the selected contact name. 
    }
}
break;
    }
}