罪过 罪过! 为什么呢? 想得到通话之后的记录的 一定不干好事.. 反正我没用来干好事..
观察者 需要一个URI 电话打完之后都往 contacts2/calls这个表里面存
private Uri uri = Uri.parse("content://call_log/calls");
注册观察者
getContentResolver().registerContentObserver(uri, true, new CallLogObserver(new Handler()));
下面是观察者 类
public class CallLogObserver extends ContentObserver {
private String content;
private String type = " ";
public CallLogObserver(Handler handler) {
super(handler);
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Cursor c = getContentResolver().query(uri, null, null, null, "_id DESC LIMIT 1"); //contacts / call表
if (c.moveToNext()) {
int types = c.getInt(c.getColumnIndex("type")); //电话类型
String date = new SimpleDateFormat("MM-dd_HH:mm").format(new Date(c.getLong(c.getColumnIndex("date")))); //时间
String number = c.getString(c.getColumnIndex("number")); //电话号码
String name = c.getString(c.getColumnIndex("name")); //联系人姓名
String duration = c.getString(c.getColumnIndex("duration")); // 通话时间 秒
switch (types) {
case 1:
type = "接电话";
break;
case 2:
type = "打电话";
break;
case 3:
type = "未接来电";
break;
default:
break;
}
String seconds = secondFormat.Format(Long.parseLong(duration)); //格式化了一下
Log.i(TAG, date + type + name + number+ "时长" + seconds);
}
}
}
public static String Format(long mss) {
// long days = mss / (60 * 60 * 24);
long hours = (mss % (60 * 60 * 24)) / (60 * 60);
long minutes = (mss % (60 * 60)) / 60;
long seconds = mss % 60;
if (mss / (60 * 60 * 24) > 0) {
return " 尼玛时间太长..超过1天了...";
} else if (hours > 0) {
return " " + hours + "小时" + minutes + "分"
+ seconds + "秒";
} else if (minutes > 0) {
return " " + minutes + "分"
+ seconds + "秒";
} else {
return " " + seconds + "秒";
}
}