利用ContentValues.put()方法,通过程序添加通讯录里的联系人资料

读取通讯录的权限:

<uses-permission android:name="android.permission.READ_CONTACTS"/>

try{ ContentResolver cr = this.getContentResolver(); ContentValues cv = new ContentValues(); Uri phoneUri = null; cv.put(Contacts.People.NAME, strName); cv.put(Contacts.People.STARRED, 1);// like the conatctor best Uri uri = Contacts.People.createPersonInMyContactsGroup(arg0, cv); if(phoneNum !=null && phoneNum.trim().equals("")){ phoneUri = Uri.withAppendedPath(uri, Contacts.People.Phones.CONTENT_DIRECTORY); cv.clear(); cv.put(Contacts.Phones.TYPE, Contacts.Phones.NUMBER); cv.put(Contacts.Phones.NUMBER, phoneNum); cr.insert(phoneUri, cv); } if(strEmail !=null && strEmail.trim().equals("")){ Uri emailUri = Uri.withAppendedPath(uri, Contacts.People.ContactMethods.CONTENT_DIRECTORY); cv.clear(); // judge the contact type via Contacts.ContactMethods.KIND cv.put(Contacts.ContactMethods.KIND, Contacts.KIND_EMAIL); cv.put(Contacts.ContactMethods.DATA, strEmail); cv.put(Contacts.ContactMethods.TYPE, Contacts.ContactMethods.TYPE_HOME); cr.insert(emailUri, cv); } }catch(Exception e){ return false; } 

你可能感兴趣的:(exception,android,null,email,通讯)