码迷,mamicode.com
首页 > 移动开发 > 详细

Android 通话记录分析

时间:2015-04-29 13:11:50      阅读:369      评论:0      收藏:0      [点我收藏+]

标签:

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();
}

 

Android 通话记录分析

标签:

原文地址:http://www.cnblogs.com/shangdawei/p/4465594.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!