本文主要實作類似iPhone通話記錄效果,即相鄰通話如果通話号碼相同、通話類型(呼入、呼出、未接)相同、間隔時間(某一門檻值範圍内),則将其歸為一條記錄,否則,将其分别顯示。首先擷取通話記錄必須添加一下權限:
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.WRITE_CALL_LOG"/>
然後通過一下代碼擷取通話記錄:
Cursor cursor = context.getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, null);
然後須通過周遊通話記錄,按照顯示規則進行顯示,在此需了解Cursor的相關知識:cursor.moveToFirst表示移動至目前記錄的第一條,cursor.moveToNext表示目前記錄的下一條記錄,cursor.moveToPrevious表示目前記錄的上一條記錄,cursor.moveToLast表示目前記錄的最後一條記錄,一般moveToFirst和moveToNext、moveToPrevious和moveToLast配套使用。随後我們進行通話記錄顯示:
private void getCallRecord() {
SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd");
Cursor cursor = context.getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, null);
int i=0;
if(cursor.moveToLast()){
String prenumber=cursor.getString(cursor.getColumnIndex(Calls.NUMBER));
int type = TypeDecision(Integer.parseInt(cursor.getString(cursor.getColumnIndex(Calls.TYPE))));
Date date = new Date(Long.parseLong(cursor.getString(cursor.getColumnIndexOrThrow(Calls.DATE))));
String time = sfd.format(date);
String name = cursor.getString(cursor.getColumnIndexOrThrow(Calls.CACHED_NAME));
String duration = cursor.getString(cursor.getColumnIndexOrThrow(Calls.DURATION));
callrecords.add(new CallEntity(type, time, "0", prenumber, duration));
while(cursor.moveToPrevious()){
String numbers = cursor.getString(cursor.getColumnIndex(Calls.NUMBER));
int temptype = TypeDecision(Integer.parseInt(cursor.getString(cursor.getColumnIndex(Calls.TYPE))));
Date tempdate = new Date(Long.parseLong(cursor.getString(cursor.getColumnIndexOrThrow(Calls.DATE))));
String temptime = sfd.format(tempdate);
String tempname = cursor.getString(cursor.getColumnIndexOrThrow(Calls.CACHED_NAME));
String tempduration = cursor.getString(cursor.getColumnIndexOrThrow(Calls.DURATION));
if(!prenumber.equals(numbers) || !temptime.equals(time) || (temptype != type)){
callrecords.add(new CallEntity(temptype, temptime, "0", numbers, tempduration));
prenumber=numbers;
time=temptime;
type=temptype;
}else{
time=temptime;
prenumber=numbers;
type=temptype;
}
}
}
}
随後需要建立一個擴充卡來顯示相應的listview item,相信大家都會,網上也有很多資料可供大家參考,希望此篇文章對大家有所幫助,不對之處歡迎大家指正。