Reputation: 573
I have below a listener to detect incoming calls
I make a query to the CallLog content provider every after an incoming call
My cursor always return null even if there was already a call logged just seconds ago
btw, I cleared my call log before running the project in eclipse
I want to be able to point my Cursor at the first row every after an incoming call but it just doesn't work
// 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 };
Upvotes: 3
Views: 1250
Reputation: 2779
I similarly read calls from the log post-call and experienced some issues but have a working solution. There are two things to consider:
1) I had trouble on some devices when using the constant
CallLog.Calls.CONTENT_URI
Try using the URI string directly instead:
Uri.parse("content://call_log/calls")
2) You are reading the call log too fast after the end of a call, sleep your Listener for 1000ms to let the call log update before querying it.
Upvotes: 3
Reputation: 11
try use local Cursor variable, ~.~ then be sure query not return null~.~
Upvotes: 0