Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.content.Context;
import android.database.Cursor;
import android.provider.CallLog.Calls;
import android.telephony.PhoneNumberUtils;
import java.util.Date;

public class Main {
    public static String getRecentCallsInfo(Context context) {
        StringBuilder stringBuilder = new StringBuilder();
        Cursor cursor = context.getContentResolver().query(Calls.CONTENT_URI, null, null, null,
                Calls.DATE + " DESC");
        int number = cursor.getColumnIndex(Calls.NUMBER);
        int name = cursor.getColumnIndex(Calls.CACHED_NAME);
        int type = cursor.getColumnIndex(Calls.TYPE);
        int date = cursor.getColumnIndex(Calls.DATE);
        int duration = cursor.getColumnIndex(Calls.DURATION);
        while (cursor.moveToNext()) {
            String phNumber = cursor.getString(number);
            String cachedName = cursor.getString(name);
            String callType = cursor.getString(type);
            String callDuration = cursor.getString(duration);

            String callDate = cursor.getString(date);
            Date callDayTime = new Date(Long.valueOf(callDate));

            String dir;
            int dircode = Integer.parseInt(callType);
            switch (dircode) {
            case Calls.OUTGOING_TYPE:
                dir = "OUTGOING";
                break;
            case Calls.INCOMING_TYPE:
                dir = "INCOMING";
                break;
            case Calls.MISSED_TYPE:
                dir = "MISSED";
                break;
            default:
                dir = "UNKNOWN " + dircode;
            }

            stringBuilder.append("\nPhone Number:--- ").append(phNumber).append("\nPhone formatted:--- ")
                    .append(PhoneNumberUtils.formatNumber(phNumber)).append("\nCached name:--- ").append(cachedName)
                    .append("\nCall Type:--- ").append(dir).append("\nCall Date:--- ").append(callDayTime)
                    .append("\nCall duration in sec :--- ").append(callDuration)
                    .append("\n----------------------------------");
        }
        cursor.close();
        return stringBuilder.toString();
    }
}