1.手机-输入号码-添加到联系人-新建联系人-选择存储位置(sim卡/设备等)输入姓名等-储存
2.联系人-点击右上角加号-先在存储位置(sim卡/设备等)输入姓名号码等信息-储存
mport android.content.ContentValuesimport android.net.Uri
import android.os.Bundle
import android.provider.ContactsContract.CommonDataKinds.Email
import android.provider.ContactsContract.CommonDataKinds.Phone
import android.provider.ContactsContract.CommonDataKinds.StructuredName
import android.provider.ContactsContract.RawContacts
import android.provider.ContactsContract.Data
import android.widget.Toast
public class MainActivity extends Activity {
参考下面: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()))
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)