http://stackoverflow.com/questions/6786666/how-do-i-access-call-log-for-android
First you need to give the permission to read call logs from the device.
Now use this method to get the recent call logs getCallDetails()
private voidgetCallDetails() {
StringBuffer sb= newStringBuffer();
Cursor managedCursor= managedQuery( CallLog.Calls.CONTENT_URI,null, null,null, 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);
sb.append("Call Details :");while( managedCursor.moveToNext() ) {
String phNumber=managedCursor.getString( number );
String callType=managedCursor.getString( type );
String callDate=managedCursor.getString( date );
Date callDayTime= newDate(Long.valueOf(callDate));
String callDuration=managedCursor.getString( duration );
String dir= null;int dircode =Integer.parseInt( callType );switch( dircode ) {caseCallLog.Calls.OUTGOING_TYPE:
dir= "OUTGOING";break;caseCallLog.Calls.INCOMING_TYPE:
dir= "INCOMING";break;caseCallLog.Calls.MISSED_TYPE:
dir= "MISSED";break;
}
sb.append("\nPhone Number:--- "+phNumber +" \nCall Type:--- "+dir+" \nCall Date:--- "+callDayTime+" \n
Call duration in sec :--- "+callDuration );sb.append("\n----------------------------------");
}
managedCursor.close();
call.setText(sb);
}
private staticString getCallDetails(Context context) {
StringBuffer stringBuffer= newStringBuffer();
Cursor cursor=context.getContentResolver().query(CallLog.Calls.CONTENT_URI,null, null, null, CallLog.Calls.DATE + " DESC");int number =cursor.getColumnIndex(CallLog.Calls.NUMBER);int type =cursor.getColumnIndex(CallLog.Calls.TYPE);int date =cursor.getColumnIndex(CallLog.Calls.DATE);int duration =cursor.getColumnIndex(CallLog.Calls.DURATION);while(cursor.moveToNext()) {
String phNumber=cursor.getString(number);
String callType=cursor.getString(type);
String callDate=cursor.getString(date);
Date callDayTime= newDate(Long.valueOf(callDate));
String callDuration=cursor.getString(duration);
String dir= null;int dircode =Integer.parseInt(callType);switch(dircode) {caseCallLog.Calls.OUTGOING_TYPE:
dir= "OUTGOING";break;caseCallLog.Calls.INCOMING_TYPE:
dir= "INCOMING";break;caseCallLog.Calls.MISSED_TYPE:
dir= "MISSED";break;
}
stringBuffer.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- "
+ dir + " \nCall Date:--- " +callDayTime+ " \nCall duration in sec :--- " +callDuration);
stringBuffer.append("\n----------------------------------");
}
cursor.close();returnstringBuffer.toString();
}
原文:http://www.cnblogs.com/shangdawei/p/4465594.html