2011-06-08 162 views

回答

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

ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI) 
    .withValue(RawContacts.ACCOUNT_TYPE, null) 
    .withValue(RawContacts.ACCOUNT_NAME, null) 
    .build()); 

ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) 
    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) 
    .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) 
    .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name) 
    .build()); 

    Log.d("test","2222222222"); 

ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) 
    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) 
    .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) 
    .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, parser.getText()) 
    .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, Phone.TYPE_WORK) 
    .build()); 

ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) 
    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) 
    .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE) 
    .withValue(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID, 3) 
    .build()); 

Log.d("test","333333"); 

try {                  
    getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); 
    Log.d("test","4444"); 
} catch (Exception e) { 
    Log.d("test","5555"); 
    // Display warning 
    Context ctx = getApplicationContext(); 
    CharSequence txt = "contactCreationFailure"; 
    int duration = Toast.LENGTH_SHORT; 
    Toast toast = Toast.makeText(ctx, txt, duration); 
    toast.show(); 

    // Log exception 
    Log.e("test", "Exceptoin encoutered while inserting contact: " + e); 
} 
相關問題