我想在android中读取通话记录

发布于 2024-11-05 18:55:03 字数 246 浏览 1 评论 0原文

可能的重复:
如何在 Android 中检索最近使用过的联系人?

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

有人知道该怎么做吗?

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

I want to read recently used contacts.

Anyone know how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

度的依靠╰つ 2024-11-12 18:55:03

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" />

为了使其正常工作并且不会引发权限拒绝异常

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文