2

I am new to android. I am getting a problem while restoring the call log, which I stored in a database.

I am storing the call log with the following code:

Cursor managedCursor = cr.query(CallLog.Calls.CONTENT_URI, null,
                                CallLog.Calls.NUMBER + "=?",
                                new String[] {(ActiveUserContacts.get(i).getnumber()) },
                                null);
int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
int name = managedCursor.getColumnIndex(CallLog.Calls.CACHED_NAME);
int NEW = managedCursor.getColumnIndex(CallLog.Calls.NEW);

while (managedCursor.moveToNext()) {
    CallLogsModel Log = new CallLogsModel(Integer.toString(i),
                                          managedCursor.getString(type),
                                          managedCursor.getString(date),
                                          managedCursor.getString(duration),
                                          managedCursor.getString(number),
                                          managedCursor.getString(name),
                                          managedCursor.getString(NEW));
    StoreData.addCallLog(UserNAME, Log);
}
managedCursor.close();

And I restore it with the code:

ContentValues values = new ContentValues();
values.put(CallLog.Calls.TYPE, PrevContents.get(i).getType());
values.put(CallLog.Calls.DATE, PrevContents.get(i).getDate());
values.put(CallLog.Calls.DURATION, PrevContents.get(i).getDuration());
values.put(CallLog.Calls.NUMBER, PrevContents.get(i).getNumber());
values.put(CallLog.Calls.CACHED_NAME, PrevContents.get(i).getName());
values.put(CallLog.Calls.NEW, PrevContents.get(i).getNew());

getActivity().getContentResolver().insert(CallLog.Calls.CONTENT_URI, values);

However, everything but the time of call got restored. Did I make a mistake?

dwitvliet
  • 7,242
  • 7
  • 36
  • 62
Rebecca
  • 45
  • 5

1 Answers1

0

Your PrevContents.get(i).getDate() may not be the right type or in the right format for the call log. Some of the examples I see of records inserted into the call log (e.g., android adding number to Call logs) use System.currentTimeMillis() as the date, which is actually of type long. You probably want to use managedCursor.getLong(date) in the code you use to retrieve the date, and store it as a long.

Another note: it looks as if you're getting 1 record when you query the call log, put that in your cursor, save that 1 record, then do the same thing for the next record, and get all the records by looping through all of the call log records manually (I assume that's what that index 'i' is for). You don't need to do that--the query can get all records for you, then you can use managedCursor.moveToNext() to do the looping. Take a look at http://android2011dev.blogspot.com/2011/08/get-android-phone-call-historylog.html for an example of how to do this. When you do the restoration you may need a loop (though there may be an easier way to do that, too).

Community
  • 1
  • 1
hBrent
  • 1,696
  • 1
  • 17
  • 38