Android 通话记录分析

http://stackoverflow.com/questions/6786666/how-do-i-access-call-log-for-android

http://android2011dev.blogspot.in/2011/08/get-android-phone-call-historylog.html

How do I access call log for android?

http://developer.android.com/reference/android/provider/CallLog.Calls.html

First you need to give the permission to read call logs from the device.

<uses-permission android:name="android.permission.READ_CONTACTS" />

Now use this method to get the recent call logs getCallDetails()

private void getCallDetails() {

    StringBuffer sb = new StringBuffer();
    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 = new Date(Long.valueOf(callDate));
        String callDuration = managedCursor.getString( duration );
        String dir = null;
        int dircode = Integer.parseInt( callType );

        switch( dircode ) {
            case CallLog.Calls.OUTGOING_TYPE:
                dir = "OUTGOING";
            break;

            case CallLog.Calls.INCOMING_TYPE:
                dir = "INCOMING";
            break;

            case CallLog.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 static String getCallDetails(Context context) {
    StringBuffer stringBuffer = new StringBuffer();
    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 = new Date(Long.valueOf(callDate));
        String callDuration = cursor.getString(duration);
        String dir = null;
        int dircode = Integer.parseInt(callType);
        switch (dircode) {
        case CallLog.Calls.OUTGOING_TYPE:
            dir = "OUTGOING";
            break;
        case CallLog.Calls.INCOMING_TYPE:
            dir = "INCOMING";
            break;

        case CallLog.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();
    return stringBuffer.toString();
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。