您的位置:首页 > 移动开发 > Android开发

Android 批量插入联系人 分享

2015-11-10 10:39 441 查看
大体的问题是这样的:

class VCardEntry{

String name;

String[] number;

getDisplayName();

getPhoneList()

class phone {

getNumber();

}

}

传来的数据:

这些是从网络端截获的数据

name :zhangsan

number : 123,456,789 (好几个号码)

name:lisi

number :111,222,333

name:wangwu

number:159753

。。。。。。

等好几百个数据

ArrayList<VCardEntry> list;

需要批量存入数据库

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

ArrayList<VCardEntry> list = params[0];

Iterator<VCardEntry> it = null;

if (list != null) {

it = list.iterator();

}

Logger.v(TAG,"--->doInBackground it:"+it);

int rawContactInsertIndex = 0;

while(it!= null && it.hasNext()) {

VCardEntry mv = it.next();

rawContactInsertIndex = ops.size(); // 有了它才能给真正的实现批量添加

Logger.v(TAG,"--->>>>>>>name:"+mv.getDisplayName());

Logger.v(TAG,"--->>>>>>>getPhoneList:"+mv.getPhoneList());

if (mv.getPhoneList() != null) {

ops.add(ContentProviderOperation

.newInsert(RawContacts.CONTENT_URI)

.withValue(RawContacts.ACCOUNT_TYPE, ACCOUNT_NAME)

.withValue(RawContacts.ACCOUNT_NAME, ACCOUNT_TYPE)

.withYieldAllowed(true).build());

// add name

ops.add(ContentProviderOperation

.newInsert(Data.CONTENT_URI)

.withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)

.withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)

.withValue(StructuredName.DISPLAY_NAME, mv.getDisplayName())

.withYieldAllowed(true).build());

// add number

for(VCardEntry.PhoneData phone : mv.getPhoneList()) {

Logger.v(TAG,"--->>>>>>>number:"+phone.getNumber());

ops.add(ContentProviderOperation

.newInsert(Data.CONTENT_URI)

.withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)

.withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)

.withValue(Phone.NUMBER, phone.getNumber())

.withValue(Phone.TYPE, Phone.TYPE_MOBILE)

.withValue(Phone.LABEL, "")

.withYieldAllowed(true).build());

}

}

}

ContentProviderResult[] results = null;

if (ops != null) {

try {

results = mContext.getContentResolver()

.applyBatch(ContactsContract.AUTHORITY, ops);

} catch (RemoteException e) {

Logger.e(TAG,String.format("%s: %s", e.toString(), e.getMessage()));

} catch (OperationApplicationException e) {

Logger.e(TAG,String.format("%s: %s", e.toString(), e.getMessage()));

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: