//处理时间
public String formatDuring(long mss) {
long hours = mss / (60 * 60);
long minutes = (mss % (1000 * 60 * 60)) / 60;
long seconds = (mss % (1000 * 60));
return hours + ":" + minutes+ ":" + seconds;
}
public String getdays(long callTime) {
String value = "";
long newTime = new Date().getTime();
long duration = (newTime - callTime) / (1000 * 60);
if (duration < 60) {
value = duration + "分钟前";
} else if (duration >= 60 && duration < DAY) {
value = (duration / 60) + "小时前";
} else if (duration >= DAY && duration < DAY * 2) {
value = "昨天";
} else if (duration >= DAY * 2 && duration < DAY * 3) {
value = "前天";
} else if (duration >= DAY * 7) {
SimpleDateFormat sdf = new SimpleDateFormat("M月dd日");
value = sdf.format(new Date(callTime));
} else {
value = (duration / DAY) + "天前";
}
return value;
}
/*
*为了方便后面页面的封装,这里定义了一个实体类
*/
public class Call {
private Bitmap img;
private String telName;
private String telNo;
private String telDate;
private String telTime;
private String simpleTime;
private int type;
private String longTime;
get、set......
}
/*
*获取所有的通话记录
*/
public List<Call> getCallList() {
List<Call> list = new ArrayList<Call>();
int type;
Date date; //日期
String time = "";//通话时间
String telName = "";//姓名
String telNo = "";//电话号码
ContentResolver cr = getContentResolver();
final Cursor cursor = cr.query(CallLog.Calls.CONTENT_URI, new String[] {
CallLog.Calls.NUMBER, CallLog.Calls.CACHED_NAME,
CallLog.Calls.TYPE, CallLog.Calls.DATE,CallLog.Calls.DURATION }, null, null,
CallLog.Calls.DEFAULT_SORT_ORDER);
for (int i = 0; i < cursor.getCount(); i++) {
Call call = new Call();
cursor.moveToPosition(i);
telName = cursor.getString(1);
telNo = cursor.getString(0);
type = cursor.getInt(2);
SimpleDateFormat sfd = new SimpleDateFormat("HH:mm:ss");
SimpleDateFormat sfd2 = new SimpleDateFormat("yyyy-MM-dd");
date = new Date(Long.parseLong(cursor.getString(3)));
time = sfd.format(date);
call.setLongTime(formatDuring(Long.valueOf(cursor.getString(4)));
String callDate = getdays(Long.parseLong(cursor.getString(3)));
String x = getContactIDFromPhoneNum(telNo);
byte[] kk = getPhoto(x);//联系人头像
if (kk != null) {
img = BitmapFactory.decodeByteArray(kk, 0, kk.length);
call.setImg(img);
}
Manager.addList(img, telName, telNo, callDate, time,
sfd2.format(date), type);
call.setTelName(telName);
call.setTelNo(telNo);
call.setType(type);
call.setTelDate(callDate);
call.setTelTime(time);
if (CallLog.Calls.INCOMING_TYPE == type) {//已接来电
call.setType(0);
} else if (CallLog.Calls.OUTGOING_TYPE == type) {//已拨
call.setType(1);
} else if (CallLog.Calls.MISSED_TYPE == type) {//未接来电
call.setType(-1);
}
list.add(call);
}
return list;
}
/*
*删除记录
*/
public void deleteLog(String phoneNumber){
String strUriInbox = "content://call_log/calls";
Uri uriCall = Uri.parse(strUriInbox);
Cursor c = getContentResolver().query(uriCall,
new String[] { "_id", "number", "date" }, null, null, null);
if (c != null && c.moveToFirst()) {
String num = c.getString(1);
String id = c.getString(0);
if (num != null &&phoneNumber!=null&& num.equals(phoneNumber)) {
getContentResolver().delete(uriCall, "_id=" + id, null);
}
c.close(); }
}
以下代码源自互联网,由于众多网络转载的原因,未找到原作者,请原作者见谅
/*
*获取联系人头像
*/
public byte[] getPhoto(String people_id) {
Cursor cur1 = null;
String photo_id = null;
String[] projection1 = new String[] {
ContactsContract.Contacts.PHOTO_ID
};
if (people_id != null && !"".equals(people_id)) {
String selection1 = ContactsContract.Contacts._ID + " = "
+ people_id;
try {
cur1 = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, projection1,
selection1, null, null);
} catch (Exception e) {
}
if (cur1.getCount() > 0) {
cur1.moveToFirst();
photo_id = cur1.getString(0);
}
String[] projection = new String[] {
ContactsContract.Data.DATA15
};
String selection = "ContactsContract.Data._ID = " + photo_id;
Cursor cur = getContentResolver().query(
ContactsContract.Data.CONTENT_URI, projection, selection,
null, null);
cur.moveToFirst();
if (cur.getCount() < 0 || cur.getCount() == 0) {
return null;
}
byte[] contactIcon = cur.getBlob(0);
if (contactIcon == null) {
return null;
} else {
return contactIcon;
}
} else {
return null;
}
}
"Copy" did not complete normally. Please see the log for more information.
这个错误是由于你开着有道词典的问题,把词典的划词翻译关了就好了