티스토리 뷰

개발/개발 자료

(Android) Content Add/Insert

시원한물냉 2015. 2. 4. 19:35

String DisplayName = getString(R.string.app_name);

        String MobileNumber = getString(R.string.mobile);

        String HomeNumber = "";

        String WorkNumber = "";

        String emailID = "";

        String company = "";

        String jobTitle = "";



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

        ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)

                .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)

                .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)

                .build());


        //------------------------------------------------------ Names

        if(DisplayName != null)

        {

            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, DisplayName).build());

        }                       

        //------------------------------------------------------ Mobile Number

        if(MobileNumber != null)

        {

            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, MobileNumber)

                            .withValue(ContactsContract.CommonDataKinds.Phone.TYPE

                                    ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)

                                    .build());

        }


        //------------------------------------------------------ Home Numbers

        if(HomeNumber != null)

        {

            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, HomeNumber)

                            .withValue(ContactsContract.CommonDataKinds.Phone.TYPE

                                    ContactsContract.CommonDataKinds.Phone.TYPE_HOME)

                                    .build());

        }


        //------------------------------------------------------ Work Numbers

        if(WorkNumber != null)

        {

            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, WorkNumber)

                            .withValue(ContactsContract.CommonDataKinds.Phone.TYPE

                      ContactsContract.CommonDataKinds.Phone.TYPE_WORK)

                      .build());

        }


        //------------------------------------------------------ Email

        if(emailID != null)

        {

            ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)

                    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)

                    .withValue(ContactsContract.Data.MIMETYPE,

                            ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)

                            .withValue(ContactsContract.CommonDataKinds.Email.DATA, emailID)

                            .withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK)

                            .build());

        }


        //------------------------------------------------------ Organization

        if(!company.equals("") && !jobTitle.equals(""))

        {

            ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)

                    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)

                    .withValue(ContactsContract.Data.MIMETYPE,

                            ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)

                            .withValue(ContactsContract.CommonDataKinds.Organization.COMPANY, company)

                            .withValue(ContactsContract.CommonDataKinds.Organization.TYPE, ContactsContract.CommonDataKinds.Organization.TYPE_WORK)

                            .withValue(ContactsContract.CommonDataKinds.Organization.TITLE, jobTitle)

                            .withValue(ContactsContract.CommonDataKinds.Organization.TYPE, ContactsContract.CommonDataKinds.Organization.TYPE_WORK)

                            .build());

        }


        // Asking the Contact provider to create a new contact                  

        try 

        {

            getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

       

        catch (Exception e) 

        {               

            e.printStackTrace();

            Toast.makeText(this, "Exception: " + e.getMessage(), Toast.LENGTH_SHORT).show();

         }

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함