我想在android中读取通话记录

时间:2011-05-06 11:42:29

标签: android phone-call calllog

  

可能重复:
  How can I retrieve recently used contacts in android?

我想阅读最近使用的联系人。

任何人都知道怎么做?

1 个答案:

答案 0 :(得分:10)

<强> 1。创建观察者:

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。注册观察员:

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

修改 从Jellybean(4.1)开始,您现在需要获得许可:

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

为此工作而不抛出权限拒绝异常