android - 为什么我的游标返回 null?

标签 android android-contentprovider android-cursor

我下面有一个监听器来检测来电
每次来电后,我都会向 CallLog 内容提供程序查询
我的光标总是返回 null,即使几秒钟前已经记录了一个调用
顺便说一句,在 eclipse 中运行项目之前我清除了我的通话记录
我希望能够在每次来电后将我的光标指向第一行,但它不起作用

// Listener to detect incoming calls.

private class CallStateListener extends PhoneStateListener {

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {

        if (previousState == TelephonyManager.CALL_STATE_RINGING
                && state == TelephonyManager.CALL_STATE_IDLE) {

            cursor = context.getContentResolver().query(
                    CallLog.Calls.CONTENT_URI, projection, null, null,
                    Calls.DEFAULT_SORT_ORDER);

            Log.i("SIZE OF CURSOR", String.valueOf(cursor.getCount()));

            if (cursor.moveToFirst()) {

            }// end if
        }// end if

        Log.i("onCallStateChanged",
                String.format("State changed to %d", state));

        previousState = state;

    }// end onCallStateChanged()
}// end CallStateListener class

Cursor cursor;
private String[] projection = new String[] { Calls.TYPE, Calls.NUMBER };

最佳答案

我同样从通话后日志中读取了通话,遇到了一些问题,但有一个可行的解决方案。有两件事需要考虑:

1) 我在某些设备上使用常量时遇到了问题

CallLog.Calls.CONTENT_URI

尝试直接使用 URI 字符串:

Uri.parse("content://call_log/calls")

2) 您在通话结束后读取通话记录的速度太快,让您的 Listener 休眠 1000 毫秒,让通话记录在查询之前更新。

关于android - 为什么我的游标返回 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18013948/

相关文章:

android - 我应该使用 Cursor 还是 CursorLoader?

android - 将事件添加到 native 日历不起作用

android - RecyclerView 添加和删除项目

android - 游标在调用此方法之前停用

android - 使用 SimpleCursorAdapter 从 Cursor 更改值

java - 在字典中的单个键下保存多个值

java - Android 如何解释布局中的@null 关键字?

android - 如何在android中的两个或多个应用程序之间安全地共享数据?

android - 即使应用程序关闭,全局变量仍然存在

Android-检查 lastKnownLocation 年龄的最佳方法