Search This Blog

Monday 22 July 2013

Example with source code of Reading call History from android phone


This tutorial teach you to fetch all call from android phone (Outgoing call, Incoming call, Missed Call) with information like call duration, user, call type etc

Because this whole process is very simple so i just make one method to read all call log and save them inside a vector of HasMap.

Android provide CallLog.Calls.CONTENT_URI content provider to read and write about calls. Every attribute has a call name inside data base of call content provider. Read those column and iterate through the loop

This need some permission in android manifest

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

Function to read everything about calls

/** Result HasMap **/
    Vector<HashMap<String, Object>> mCallHostory = new Vector<HashMap<String, Object>>();
    private String name = "name", date = "date", duration = "duration",
            isRead = "is_read", type = "type";

    /** Function to read all calls **/

    public void getCallHistory() {
        Cursor cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI,
                null, null, null, null);
        cursor.moveToFirst();
        do {
            HashMap<String, Object> mTemp = new HashMap<String, Object>();
            /* Reading Name */
            String nameTemp = cursor.getString(cursor
                    .getColumnIndex(CallLog.Calls.CACHED_NAME));
            if (name.equalsIgnoreCase(""))
                mTemp.put(name, "Unknown");
            else
                mTemp.put(name, nameTemp);
            /* Reading Date */
            long dateTemp = cursor.getLong(cursor
                    .getColumnIndex(CallLog.Calls.DATE));
            mTemp.put(date, dateTemp);

            /* Reading duration */
            long durationTemp = cursor.getLong(cursor
                    .getColumnIndex(CallLog.Calls.DURATION));
            mTemp.put(duration, durationTemp);

            /* Reading already user see it or not */
            long IS_READ = cursor.getLong(cursor
                    .getColumnIndex(CallLog.Calls.IS_READ));
            mTemp.put(isRead, IS_READ);

            /* Reading Date */
            int typeTemp = cursor.getInt(cursor
                    .getColumnIndex(CallLog.Calls.TYPE));
            mTemp.put(type, typeTemp);

            /* Add one call Detail to Vector */
            mCallHostory.add(mTemp);
        } while (cursor.moveToNext());

        Log.e("Check", "Data");
    }

Reference Link

Android Developer

No comments:

Post a Comment

Feedback always help in improvement. If you have any query suggestion feel free to comment and Keep visiting my blog to encourage me to blogging

Android News and source code