如何向联系经理添加活动(生日和周年纪念日)

时间:2014-03-05 07:34:55

标签: java android contacts

我可以将姓名,电话号码和他们的电子邮件ID添加到联系人管理员,现在我想将事件类别中的生日日期和周年日期添加到联系人管理器,我已经编写了代码但是在添加了该代码之后我的设备那些日子没有显示

EditText etName = (EditText) findViewById(R.id.et_name);

                // Getting reference to Mobile EditText 
                EditText etMobile = (EditText) findViewById(R.id.et_mobile_phone);

                etbirthday = (EditText) findViewById(R.id.editText1);

                etanniversary  = (EditText) findViewById(R.id.editText2);

                // Getting reference to HomePhone EditText 
                //EditText etHomePhone = (EditText) findViewById(R.id.et_home_phone);



                // Getting reference to WorkEmail EditText 
                EditText etWorkEmail = (EditText) findViewById(R.id.et_work_email); 




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

                int rawContactID = ops.size();

                // Adding insert operation to operations list 
                // to insert a new raw contact in the table ContactsContract.RawContacts
                ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
                        .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
                        .withValue(RawContacts.ACCOUNT_NAME, null)
                        .build());

                // Adding insert operation to operations list
                // to insert display name in the table ContactsContract.Data
                ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactID)
                        .withValue(ContactsContract.Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
                        .withValue(StructuredName.DISPLAY_NAME, etName.getText().toString())
                        .build());

                // Adding insert operation to operations list
                // to insert Mobile Number in the table ContactsContract.Data
                ops.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, etMobile.getText().toString())
                        .withValue(Phone.TYPE, CommonDataKinds.Phone.TYPE_MOBILE)
                        .build());


                // Adding insert operation to operations list
                // to insert Work Email in the table ContactsContract.Data

                ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactID)
                        .withValue(ContactsContract.Data.MIMETYPE, Email.CONTENT_ITEM_TYPE)
                        .withValue(Email.ADDRESS, etWorkEmail.getText().toString())
                        .withValue(Email.TYPE, Email.TYPE_WORK)
                        .build());

                // Event
                ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactID)
                        .withValue(ContactsContract.Data.MIMETYPE, Event.CONTENT_ITEM_TYPE)
                        .withValue(Event.START_DATE, etanniversary.getText().toString())
                        .withValue(Event.TYPE, Event.TYPE_OTHER)
                        .build());


                System.out.println("anniversarydate"+etanniversary.getText().toString());
                ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactID)
                    .withValue(ContactsContract.Data.MIMETYPE,Event.CONTENT_ITEM_TYPE)
                    .withValue(Event.START_DATE, etanniversary.getText().toString())
                    .withValue(Event.TYPE, Event.TYPE_ANNIVERSARY)
                    .build());

                System.out.println("birthdaydate"+etbirthday.getText().toString());
                ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactID)
                    .withValue(ContactsContract.Data.MIMETYPE,Event.CONTENT_ITEM_TYPE)
                    .withValue(Event.START_DATE, etbirthday.getText().toString())
                    .withValue(Event.TYPE, Event.TYPE_BIRTHDAY)
                    .build());

                try{
                    // Executing all the insert operations as a single database transaction
                    System.out.println("ops"+ops);
                    getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

                    Toast.makeText(getBaseContext(), "Contact is successfully added", Toast.LENGTH_SHORT).show();
                    SharedPreferences prefs1=Addcontact.this.getSharedPreferences("IMS1",MODE_WORLD_WRITEABLE);
                    SharedPreferences.Editor editor1=prefs1.edit();
                    editor1.putBoolean("isContatcadded",true);
                    editor1.commit();
                    finish();
                }catch (RemoteException e) {                    
                    e.printStackTrace();
                }catch (OperationApplicationException e) {
                    e.printStackTrace();
                }
            }
        };

0 个答案:

没有答案