接着上次通讯录获取,继续实现Android6.0的通话记录获取
1、在MainActivity的layout中添加一个button用于获取通话记录功能
2、新建一个包CallLog用于存放获取通话记录所需的类。并在包中新建一个Activity:GetCallLog,实现通话记录获取的基本功能。由于上一篇博客已经将权限申请封装好了,所以直接在onCreate函数中调用就可以了。
private static List callLogList = new ArrayList();
private static final String CALLLOG_PERMISSION = android.Manifest.permission.READ_CALL_LOG;
private static final int readCallLogRequest = 2;
public static String getCallLog() {
String[] projection = {CallLog.Calls.CACHED_NAME, CallLog.Calls.NUMBER,
CallLog.Calls.TYPE,CallLog.Calls.DURATION,CallLog.Calls.DATE};
try {
Uri callLogUri = CallLog.Calls.CONTENT_URI;
Cursor cursor = MyApplication.getContext().getContentResolver().query(callLogUri,projection,null,null,CallLog.Calls.DEFAULT_SORT_ORDER);
String callLogName;
String callLogNumber;
String callLogDate;
int callLogType;
int callLogTime;
String type;
String time;
while (cursor.moveToNext()){
callLogName = cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME));
if (callLogName==null){
callLogName = "未知号码";
}
callLogNumber = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
callLogDate = cursor.getString(cursor.getColumnIndex(CallLog.Calls.DATE));
SimpleDateFormat DateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date d = new Date(Long.parseLong(callLogDate));
callLogDate = DateFormat.format(d);
callLogType =cursor.getInt(cursor.getColumnIndex(CallLog.Calls.TYPE));
if (callLogType == 1){
type ="来电";
}else if (callLogType ==2){
type = "拨出";
}else
type = "未接";
callLogTime = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.DURATION));
if (type =="未接"){
time ="未接";
}else {
time = timeChange(callLogTime);
}
CallLogInfo callLogInfo = new CallLogInfo(callLogName,callLogNumber
,callLogDate,callLogType,time);
callLogList.add(callLogInfo);
// Log.d("输出",callLogDate + callLogName + callLogNumber + callLogTime + callLogType);
}
cursor.close();
}catch (Exception e){
e.printStackTrace();
}
return null;
}
3、在这里还是要数据封装
public class CallLogInfo {
private String name;
private String number;
private String date;
private int type; // 来电:1,拨出:2,未接:3
private String time; //通话时长
public CallLogInfo(String name,String number,String date,int type,String time){
setName(name);
setNumber(number);
setType(type);
setTime(time);
setDate(date);
}
4、为了让通话持续时间显得更人性化,实现了一个将秒转换为时分秒的方法:
private static String timeChange(int time){
int h = 0;
int m = 0;
int s = 0;
int temp = time % 3600;
if (time >3600){
h = time/3600;
if(temp!=0){
if (temp>60){
m = temp/60;
if (temp%60!= 0 ){
s = temp/60;
}
}else {
s = temp;
}
}
}else {
m = time/60;
if(time%60!=0){
s = time%60;
}
}
return "通话时长:"+ h +"时" + m +"分" + s + "秒";
}
5、添加自定义的适配器来显示ListView
public class CallLogAdapter extends BaseAdapter {
private List list;
private Context context;
public CallLogAdapter(List list,Context context){
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
CallLogInfo callLog = list.get(position);
if (convertView ==null){
convertView = LayoutInflater.from(context).inflate(R.layout.calllog,null);
holder = new ViewHolder();
holder.call_type = (ImageView) convertView.findViewById(R.id.call_type);
holder.name = (TextView) convertView.findViewById(R.id.call_name);
holder.number = (TextView) convertView.findViewById(R.id.call_number);
holder.date = (TextView) convertView.findViewById(R.id.call_date);
holder.time = (TextView) convertView.findViewById(R.id.call_time);
setType(callLog.getType(),holder);
holder.name.setText(callLog.getName());
holder.number.setText(callLog.getNumber());
holder.date.setText(callLog.getDate());
holder.time.setText(callLog.getTime());
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
setType(callLog.getType(),holder);
holder.name.setText(callLog.getName());
holder.number.setText(callLog.getNumber());
holder.date.setText(callLog.getDate());
holder.time.setText(callLog.getTime());
}
return convertView;
}
private static class ViewHolder{
ImageView call_type;
TextView name;
TextView number;
TextView date;
TextView time;
}
private void setType(int callLog,ViewHolder holder){
switch (callLog) {
case 1:
holder.call_type.setBackgroundResource(R.drawable.laidian);
break;
case 2:
holder.call_type.setBackgroundResource(R.drawable.bochu);
break;
case 3:
holder.call_type.setBackgroundResource(R.drawable.weijie);
break;
}
}
}
6、清理缓存别忘记了
@Override
public void onBackPressed() { //清除缓存
callLogList.clear();
super.onBackPressed();
}
7、绑定listview和adapter
CallLogLv = (ListView) findViewById(R.id.CallLogLv);
adapter = new CallLogAdapter(callLogList, this);
CallLogLv.setAdapter(adapter);
8、大功告成了,界面还可以设计的好看一点,我这里只是为了方便展示数据,所以界面弄的很简单