Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to read call logs in the android [duplicate]

Possible Duplicate:
How can I retrieve recently used contacts in android?

I want to read recently used contacts.

Anyone know how to do this?

like image 440
kannappan Avatar asked Dec 22 '22 14:12

kannappan


1 Answers

1. Create observer:

class CustomContentObserver extends ContentObserver {

        public CustomContentObserver(Handler handler) {
            super(handler);

        }

        @Override public boolean deliverSelfNotifications() { 
            return false; 
        }

        public void logCallLog() {
            long dialed;
            String columns[]=new String[] {
                    CallLog.Calls._ID, 
                    CallLog.Calls.NUMBER, 
                    CallLog.Calls.DATE, 
                    CallLog.Calls.DURATION, 
                    CallLog.Calls.TYPE};
            Cursor c;
            c = getContentResolver().query(Uri.parse("content://call_log/calls"),
                    columns, null, null, "Calls._ID DESC"); //last record first
            while (c.moveToNext()) {
                dialed=c.getLong(c.getColumnIndex(CallLog.Calls.DATE));                 
                Log.i("CallLog","type: " + c.getString(4) + "Call to number: "+number+", registered at: "+new Date(dialed).toString());
            }
        }

        public void onChange(boolean selfChange) {
            super.onChange(selfChange);
            Log.d("PhoneService", "StringsContentObserver.onChange( " + selfChange + ")");
            logCallLog();
        }

}

2. Register observer:

 Uri mediaUri = android.provider.CallLog.Calls.CONTENT_URI;
        Log.d("PhoneService", "The Encoded path of the media Uri is "
                + mediaUri.getEncodedPath());
        CustomContentObserver custObser = new CustomContentObserver(handler);
        imageContentRsr.registerContentObserver(mediaUri, false, custObser);

EDIT: As of Jellybean (4.1), you now need the permission:

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

For this to work and not throw a Permission Denial exception

like image 73
alezhka Avatar answered Dec 28 '22 09:12

alezhka