1

I was trying to get call logs from android device by using the code from here.

 Cursor c = managedQuery(allCalls, null, null, null, null);

        String num= c.getString(c.getColumnIndex(CallLog.Calls.NUMBER));// for  number
        String name= c.getString(c.getColumnIndex(CallLog.Calls.CACHED_NAME));// for name
        String duration = c.getString(c.getColumnIndex(CallLog.Calls.DURATION));// for duration
        int type = Integer.parseInt(c.getString(c.getColumnIndex(CallLog.Calls.TYPE)));//

and getting an exception as: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 500

Can anyone help me on this?

Thanks

Community
  • 1
  • 1
Hari Krishnan
  • 5,992
  • 9
  • 37
  • 55

2 Answers2

0

Can you try this?

Pass new String[]{"*"} as a projection instead of null.

Cursor c = managedQuery(allCalls, new String[]{"*"}, null, null, null);
dsharew
  • 10,377
  • 6
  • 49
  • 75
0

I got the answer after trying this code

 Cursor c = managedQuery(CallLog.Calls.CONTENT_URI, null,null, null, null);



        int durationIndex = c.getColumnIndex(CallLog.Calls.DURATION);

        String duration="";
        while (c.moveToNext()) {
           duration = c.getString(durationIndex);
        }

number and name is not required for me, but we can get that too using the same way.

Hari Krishnan
  • 5,992
  • 9
  • 37
  • 55