在前面有提到批量添加联系人:Android批量添加联系人到通讯录,通话记录和通讯录一样都是直接操作ContentProvider。为了使批量更新、插入、删除数据更加方便,android系统引入了 ContentProviderOperation类,使用ContentProviderOperation的理由
1.所有的操作都在一个事务中执行,这样可以保证数据完整性
2.由于批量操作在一个事务中执行,只需要打开和关闭一个事务,比多次打开关闭多个事务性能要好些
3.使用批量操作和多次单个操作相比,减少了应用和content provider之间的上下文切换,这样也会提升应用的性能,并且减少占用CPU的时间,当然也会减少电量的消耗。
首先定义一个通话记录的实体类
package com.csr.BTApp.apical.domain; public class Tb_calllogs { private String mNumber; // 号码 private String mName; // 在通讯录中的联系人 private int mCallLogType; // 通话记录的状态 1:来电 2:去电 3:未接 private Long mCallLogDate; // 通话记录日期 private int mCallLogDuration; // 通话记录时长 public Tb_calllogs() { } public Tb_calllogs(String mNumber, String mName, int mCallLogType, Long mCallLogDate, int mCallLogDuration) { super(); this.mNumber = mNumber; this.mName = mName; this.mCallLogType = mCallLogType; this.mCallLogDate = mCallLogDate; this.mCallLogDuration = mCallLogDuration; } public String getmNumber() { return mNumber; } public void setmNumber(String mNumber) { this.mNumber = mNumber; } public String getmName() { return mName; } public void setmName(String mName) { this.mName = mName; } public int getmCallLogType() { return mCallLogType; } public void setmCallLogType(int mCallLogType) { this.mCallLogType = mCallLogType; } public Long getmCallLogDate() { return mCallLogDate; } public void setmCallLogDate(Long mCallLogDate) { this.mCallLogDate = mCallLogDate; } public int getmCallLogDuration() { return mCallLogDuration; } public void setmCallLogDuration(int mCallLogDuration) { this.mCallLogDuration = mCallLogDuration; } @Override public String toString() { return "Tb_calllogs [mNumber=" + mNumber + ", mName=" + mName + ", mCallLogType=" + mCallLogType + ", mCallLogDate=" + mCallLogDate + ", mCallLogDuration=" + mCallLogDuration + "]"; } }
添加单条通话记录
/** * 往数据库中新增通话记录 * * @param name * @param number */ public static void AddCallLogs(Tb_calllogs calllog) { ContentValues values = new ContentValues(); values.clear(); values.put(CallLog.Calls.CACHED_NAME, calllog.getmName()); values.put(CallLog.Calls.NUMBER, calllog.getmNumber()); values.put(CallLog.Calls.TYPE, calllog.getmCallLogType()); values.put(CallLog.Calls.DATE, calllog.getmCallLogDate()); values.put(CallLog.Calls.DURATION, calllog.getmCallLogDuration()); values.put(CallLog.Calls.NEW, "0");// 0已看1未看 ,由于没有获取默认全为已读 mContext.getContentResolver().insert(CallLog.Calls.CONTENT_URI, values); }批量操作
public static void BatchAddCallLogs(List<Tb_calllogs> list) throws RemoteException, OperationApplicationException { GlobalConstants.PrintLog_D("[GlobalVariables->]BatchAddCallLogs begin"); ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); ContentValues values = new ContentValues(); for (Tb_calllogs calllog : list) { values.clear(); values.put(CallLog.Calls.CACHED_NAME, calllog.getmName()); values.put(CallLog.Calls.NUMBER, calllog.getmNumber()); values.put(CallLog.Calls.TYPE, calllog.getmCallLogType()); values.put(CallLog.Calls.DATE, calllog.getmCallLogDate()); values.put(CallLog.Calls.DURATION, calllog.getmCallLogDuration()); values.put(CallLog.Calls.NEW, "0");// 0已看1未看 ,由于没有获取默认全为已读 ops.add(ContentProviderOperation .newInsert(CallLog.Calls.CONTENT_URI).withValues(values) .withYieldAllowed(true).build()); } if (ops != null) { // 真正添加 ContentProviderResult[] results = mContext.getContentResolver() .applyBatch(CallLog.AUTHORITY, ops); // for (ContentProviderResult result : results) { // GlobalConstants // .PrintLog_D("[GlobalVariables->]BatchAddCallLogs " // + result.uri.toString()); // } } }传入list遍历所有的list后,添加到ContentProviderOperation最后才调用applyBatch效率会高很多。
Android批量添加通话记录,布布扣,bubuko.com
原文:http://blog.csdn.net/deng0zhaotai/article/details/26362469