ANDROID 管理連絡人

來源:互聯網
上載者:User

通過Android系統提供的介面,可以很方便的管理連絡人資訊。 

添加 

新增連絡人... 
1.6上的代碼

String peopleName = "name";<br />ContentValues personValues = new ContentValues();<br />// name<br />personValues.put(Contacts.People.NAME, peopleName);<br />/* STARRED 0 = Contacts, 1 = Favorites */<br />personValues.put(Contacts.People.STARRED, 0);<br />Uri newPersonUri = Contacts.People.createPersonInMyContactsGroup(<br /> getContentResolver(), personValues);  

2.1時需要用下面的代碼才可以添加 

String peopleName = "name";<br />ContentValues personValues = new ContentValues();<br />// name<br />personValues.put(Contacts.People.NAME, peopleName);<br />newPersonUri = getContentResolver().insert(People.CONTENT_URI, personValues);  

添加電話號碼 
根據TYPE的不同,可以添加不同類型的電話號碼。 
1.6上支援的TYPE有:TYPE_CUSTOM,TYPE_FAX_HOME,TYPE_FAX_WORK,TYPE_HOME,TYPE_MOBILE,TYPE_OTHER,TYPE_PAGER,TYPE_WORK。 

Uri uri = null;<br />ContentValues values = new ContentValues(); </p><p>// add phone number<br />String tel = "86-21-65432100"; </p><p>if (!AppUtils.isEmpty(tel)) {<br /> values.clear();<br /> uri = Uri.withAppendedPath(newPersonUri, Contacts.People.Phones.CONTENT_DIRECTORY);<br /> values.put(Contacts.Phones.TYPE, Contacts.Phones.TYPE_HOME);<br /> values.put(Contacts.Phones.NUMBER, tel);<br /> getContentResolver().insert(uri, values);<br />}  

添加Email地址 
通過改變KIND的值,可以添加不同的連絡方式。 
1.6支援KIND_EMAIL,KIND_IM,KIND_ORGANIZATION,KIND_PHONE,KIND_POSTAL 

// add email address<br />String email = "abc@com.cn"; </p><p>if (!AppUtils.isEmpty(email)) {<br /> values.clear();<br /> uri = Uri.withAppendedPath(newPersonUri, Contacts.People.ContactMethods.CONTENT_DIRECTORY);<br /> values.put(Contacts.ContactMethods.KIND, Contacts.KIND_EMAIL);<br /> values.put(Contacts.ContactMethods.DATA, email);<br /> values.put(Contacts.ContactMethods.TYPE, Contacts.ContactMethods.TYPE_WORK);<br /> getContentResolver().insert(uri, values);<br />}<br /> 

添加公司和職務 

// add company name & title<br />String company = "Google?";<br />String position= "CEO!!!"; </p><p>if (!AppUtils.isEmpty(company) || !AppUtils.isEmpty(position)) {<br /> values.clear();<br /> uri = Uri.withAppendedPath(newPersonUri, Contacts.Organizations.CONTENT_DIRECTORY); </p><p> // company name<br /> if (!AppUtils.isEmpty(company)) {<br /> values.put(Contacts.Organizations.COMPANY, companyNameText);<br /> } </p><p> // position<br /> if (!AppUtils.isEmpty(position)) {<br /> values.put(Contacts.Organizations.TITLE, positionNameText);<br /> } </p><p> values.put(Contacts.Organizations.TYPE, Contacts.Organizations.TYPE_WORK);<br /> getContentResolver().insert(uri, values);<br />}<br /> 

查詢 

要實現查詢功能,可能需要有點SQL的基礎。 

查詢人名 

// the contents want to get<br />String projection[] = new String[] { People._ID, People.NAME };<br />// the name to be found<br />String name = "find me";<br />// start search<br />Cursor cur = getContentResolver().query(People.CONTENT_URI,<br />projection, // select sentence<br />People.NAME + " = ?", // where sentence<br />new String[] { name }, // where values<br />People.NAME); // order by </p><p>if (cur.moveToFirst()) {<br /> // get the results<br /> do {<br /> String id = cur.getString(cur.getColumnIndex(People._ID));<br /> String name = cur.getString(cur.getColumnIndex(People.NAME));<br /> } while (cur.moveToNext());<br />} </p><p>// close while finish<br />if (cur != null) {<br /> cur.close();<br />}  

通過修改projection的內容,可以取得不同的內容。 
如果要獲得電話號碼,就可以改成(id由上面的代碼獲得) 

String phoneProjection[] = new String[] { Contacts.Phones.PERSON_ID, Contacts.Phones.NUMBER }; </p><p>Cursor phoneCursor = getContentResolver().query(Contacts.Phones.CONTENT_URI,<br />phoneProjection, // select<br />Contacts.Phones.PERSON_ID + " = " + id, // where (another style)<br />null,<br />Contacts.Phones.DEFAULT_SORT_ORDER); // order </p><p>if (phoneCursor.moveToFirst()) {<br /> // get the results<br /> do {<br /> String phone = phoneCursor.getString(phoneCursor.getColumnIndex(Contacts.Phones.NUMBER));<br /> } while (phoneCursor.moveToNext());<br />} </p><p>// close while finish<br />if (phoneCursor != null) {<br /> phoneCursor.close();<br />}  

如果要獲得email地址,稍微麻煩點 

String emailProjection[] = new String[] { Contacts.Phones.PERSON_ID, Contacts.ContactMethods.KIND, Contacts.ContactMethods.DATA }; </p><p>Cursor emailCursor = getContentResolver().query(Contacts.ContactMethods.CONTENT_URI,<br />emailProjection, // select<br />Contacts.ContactMethods.PERSON_ID + " = " + id, // where<br />null,<br />Contacts.ContactMethods.DEFAULT_SORT_ORDER); // order </p><p>if (emailCursor.moveToFirst()) {<br /> do {<br /> int kind = emailCursor.getInt(emailCursor.getColumnIndex(Contacts.ContactMethods.KIND));<br /> if (Contacts.KIND_EMAIL == kind) {<br /> email = emailCursor.getString(emailCursor.getColumnIndex(Contacts.ContactMethods.DATA));<br /> }<br /> } while (emailCursor.moveToNext());<br />} </p><p>// close while finish<br />if (emailCursor != null) {<br /> emailCursor.close();<br />}  

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.