Android获取通话记录【名称,号码,日期,通话时间,类型】

Android获取通话记录[名称,号码,日期,通话时间,类型]

    /**
     * 得到所属'列'后,记住这些值,访问数据库的速度可以提高两倍
     */
    public static final int CACHED_NAME=16;
    public static final int NUMBER=0;
    public static final int DATE=6;
    public static final int DURATION=7;
    public static final int TYPE=22;

    public List getCallList(Context context) {
        Cursor cursor = context.getContentResolver()
                .query(CallLog.Calls.CONTENT_URI, null, null, null, CallLog.Calls.DEFAULT_SORT_ORDER);

        if (cursor.getCount() <= 0) {
            return null;
        }
        List list = new ArrayList<>();
        while (cursor.moveToNext()) {
            CallPO call = new CallPO();

            /* Reading Name */
            /**int a=cursor.getColumnIndex(CallLog.Calls.CACHED_NAME);*/
            String nameTemp = cursor.getString(CACHED_NAME);

            if (nameTemp==null||nameTemp.length()==0) {
                call.setDisplay_name("未知");
            } else {
                call.setDisplay_name(nameTemp);
            }

            /**int b=cursor.getColumnIndex(CallLog.Calls.NUMBER);*/
            call.setNumber(cursor.getString(NUMBER));

            /* Reading Date */
            /**int c=cursor.getColumnIndex(CallLog.Calls.DATE);*/
            String date= DateUtil.timedate(cursor.getString(DATE));
            call.setDate(date);

            /* Reading duration */
            /** int d=cursor.getColumnIndex(CallLog.Calls.DURATION);*/
            call.setDuration(cursor.getString(DURATION)+"秒");

            /* Reading Date */
            /**
             * 1.已接
             * 2.已拨
             * 3.未接
             */
            /**int e=cursor.getColumnIndex(CallLog.Calls.TYPE);*/
            int type=cursor.getInt(TYPE);
            String result="";
            switch (type) {
                case CallLog.Calls.INCOMING_TYPE:
                    result = "已接";
                    break;
                case CallLog.Calls.OUTGOING_TYPE:
                    result = "已拨";
                    break;
                case CallLog.Calls.MISSED_TYPE:
                    result = "未接";
                    break;
                default:
                    break;
            }
            call.setType(result);
            list.add(call);
        }
        cursor.close();
        return list;
    }

public class CallPO {
    private String display_name;
    private String number;
    private String date;
    private String duration;
    private String type;

    public String getDisplay_name() {
        return display_name;
    }

    public void setDisplay_name(String display_name) {
        this.display_name = display_name;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getDuration() {
        return duration;
    }

    public void setDuration(String duration) {
        this.duration = duration;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}


你可能感兴趣的:(Android)