【问题标题】:why does my cursor return null?为什么我的光标返回null?
【发布时间】:2013-08-02 18:24:22
【问题描述】:

我有一个监听器来检测来电
每次来电后,我都会向 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 };

【问题讨论】:

  • 用户收到的任何来电。无论您是接听电话还是未接电话,它都会在每次来电后向 CallLog 内容提供者查询

标签: android android-contentprovider android-cursor


【解决方案1】:

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

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

CallLog.Calls.CONTENT_URI

尝试直接使用 URI 字符串:

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

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

【讨论】:

  • 是内容 URI 吗?
【解决方案2】:

尝试使用本地游标变量, ~.~ 然后确保查询不返回 null~.~

【讨论】:

  • 光标在日志中仍然返回 0
猜你喜欢
  • 1970-01-01
  • 2021-11-29
  • 2013-10-04
  • 1970-01-01
  • 2022-01-10
  • 2020-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多