Pages

Search This Blog

Monday, 13 August 2012

RETRIEVING DATE FROM CALLLOG IN ANDROID

When android app developers are in a need to get the date of a particular call the following stuff works very fine.

               try
{
'fields to retrieve from callLog
                               String[] strFields = {
       android.provider.CallLog.Calls.NUMBER, 
       android.provider.CallLog.Calls.TYPE,
       android.provider.CallLog.Calls.DATE        
       };

'form a query
Cursor mCallCursor = getContentResolver().query(
       android.provider.CallLog.Calls.CONTENT_URI,
       strFields,
       null,
       null,
      null
       );

while (mCallCursor.moveToNext()) 
{
                   int dateColumn = mCallCursor.getColumnIndex(android.provider.CallLog.Calls.DATE);
           long callDate = mCallCursor.getLong(dateColumn);
           SimpleDateFormat datePattern = new SimpleDateFormat ("yyyy-MM-dd");
           datePattern.setTimeZone(TimeZone.getTimeZone("GMT"));
           String datestr = datePattern.format(new Date(callDate));
           Toast.makeText(this, datestr, 50).show();
}
In some cases when getting date it will display something like 1970-01-01. In this situation you would have used int instead of long. So replace long callDate = mCallCursor.getInt(dateColumn); as long callDate = mCallCursor.getLong(dateColumn);
Recommended packages:

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

No comments:

Post a Comment