Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Add contacts programatically in android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 146
    Comment on it

    In android we can easily share data across the applications and this is done by using ContactProvider. A contact provider class is a flexible java class that provides you multiple row contacts to access person data.

    we can also use contact provider to insert some records in contact diary.

    Here ContentProviderOperation.Builder is created by using newInsert(Uri), newUpdate(Uri), newDelete(Uri).

    you can use these codes to insert some records in contact table like this :

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

    create arraylist of ContentProviderOperation type.

                arrayList.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactID)
                        .withValue(ContactsContract.Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
                        .withValue(Phone.NUMBER, mobileEdittext.getText().toString())
                        .withValue(Phone.TYPE, CommonDataKinds.Phone.TYPE_MOBILE)
                        .build());
    

    now apply batch method to execute queries :

      try{
                    getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
                    Toast.makeText(getBaseContext(), "Contact added", Toast.LENGTH_SHORT).show();
                }catch (RemoteException e) {
                    e.printStackTrace();
                }catch (OperationApplicationException e) {
                    e.printStackTrace();
                }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: