Friday, 1 January 2016

Get Call Logs from android device


   Here I am going to explain how to call history from android device


Step 1)

  
         Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI,                                                                                       null, null, null, null);

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

        while (managedCursor.moveToNext()) {

            String phNumber = managedCursor.getString(number);
            String callType = managedCursor.getString(type);
            String callDate = managedCursor.getString(date);


            String callDuration = managedCursor.getString(duration);
            String dir = null;
            int dircode = Integer.parseInt(callType);
            switch (dircode) {
                case CallLog.Calls.OUTGOING_TYPE:
                    dir = "OUTGOING";
                    break;
                case CallLog.Calls.INCOMING_TYPE:
                    dir = "INCOMING";
                    break;
                case CallLog.Calls.MISSED_TYPE:
                    dir = "MISSED";
                    break;
            }

         
            Date callDayTime = null;
            if (callDate != null) {
                callDayTime = new Date(Long.valueOf(callDate));
            } else 
            {
             Log.e("date","");
            }


           Log.e("date",callDayTime.toString());
           Log.e("number",phNumber);
           Log.e("type",dir);
           Log.e("duration",callDuration);

        }


Step 2).Add below permission to your manifest file.

  <uses-permission android:name="android.permission.READ_CALL_LOG" />

 <uses-permission android:name="android.permission.READ_CONTACTS" />


Happy coding :)