上一篇讲的是Android开发之获取手机通讯录,这一篇博客也将针对手机联系人这一块进行开发。下面是获取手机通话记录的详细步骤:
1. 首先,我们需要新建一个类CallLogInfo,用于通话记录的数据封装,定义字符串。
public class CallLogInfo {
public String number;
public long date;
public int type;
public CallLogInfo(String number, long date, int type) {
super();
this.number = number;
this.date = date;
this.type = type;
}
public CallLogInfo() {
super();
}
}
2.然后,主要是通过 context.getContentResolver()方法来获得通话记录 ,这个方法返回一个游标的数据类型,通过moveToNext()方法来获取所有的手机号码及其类型、时间,将获得的信息放入列表CallLogInfo中。
public class ContactsMsgUtils {
public List<CallLogInfo> getCallLog(Context context) {
List<CallLogInfo> infos = new ArrayList<CallLogInfo>();
ContentResolver cr = context.getContentResolver();
Uri uri = Calls.CONTENT_URI;
String[] projection = new String[] { Calls.NUMBER, Calls.DATE,
Calls.TYPE };
Cursor cursor = cr.query(uri, projection, null, null, null);
while (cursor.moveToNext()) {
String number = cursor.getString(0);
long date = cursor.getLong(1);
int type = cursor.getInt(2);
infos.add(new CallLogInfo(number, date, type));
}
cursor.close();
return infos;

本文介绍如何在Android应用中获取手机的通话记录,并提供了一个完整的示例代码,包括数据封装类CallLogInfo的设计、通过ContentResolver获取通话记录的具体实现、必要的权限配置及主Activity的编写。
最低0.47元/天 解锁文章
1465





