[关闭]
@cxm-2016 2016-08-15T16:49:28.000000Z 字数 2797 阅读 1387

Android操作联系人

android no


  1. fun Cursor.getString(columnName: String): String = this.getString(this.getColumnIndex(columnName))
  2. fun Cursor.getLong(columnName: String): Long = this.getLong(this.getColumnIndex(columnName))
  3. /**
  4. * 陈小默 16/8/13.
  5. */
  6. class ContactsUtils {
  7. companion object {
  8. private val RAW_CONTACTS_URI = ContactsContract.RawContacts.CONTENT_URI
  9. private val DATA_URI = ContactsContract.Data.CONTENT_URI
  10. private val RAW_CONTACT_ID = ContactsContract.RawContactsEntity.RAW_CONTACT_ID
  11. private val MIME_TYPE_ID = "mimetype_id"
  12. private val TYPE_PHONE = 5
  13. private val TYPE_NAME = 7
  14. private val TYPE_EMALL = 1
  15. private val DISPLAY_NAME = "display_name"
  16. private val ITEM = "vnd.android.cursor.item/"
  17. private val CONDITION = "$RAW_CONTACT_ID=? and $MIME_TYPE_ID=?"
  18. private val MIME_TYPE = ContactsContract.Data.MIMETYPE
  19. private val MIME_TYPES: Array<String> = arrayOf(
  20. "",
  21. "${ITEM}email_v2",
  22. "${ITEM}im",
  23. "${ITEM}nickname",
  24. "${ITEM}organization",
  25. "${ITEM}phone_v2",
  26. "${ITEM}sip_address",
  27. "${ITEM}name",
  28. "${ITEM}postal-address_v2",
  29. "${ITEM}identity",
  30. "${ITEM}photo",
  31. "${ITEM}group_membership"
  32. )
  33. fun insertContacts(context: Context, contactsData: ContactsData): Long {
  34. val resolver = context.contentResolver
  35. val raw = resolver.insert(RAW_CONTACTS_URI, ContentValues())
  36. val id = ContentUris.parseId(raw)
  37. val (i, name, phone, email) = contactsData
  38. val nameData = getDataValues(id, MIME_TYPES[TYPE_NAME], name, name)
  39. resolver.insert(DATA_URI, nameData)
  40. val phoneData = getDataValues(id, MIME_TYPES[TYPE_PHONE], phone, "2")
  41. resolver.insert(DATA_URI, phoneData)
  42. val emailData = getDataValues(id, MIME_TYPES[TYPE_EMALL], email, "2")
  43. resolver.insert(DATA_URI, emailData)
  44. return id
  45. }
  46. fun deleteContacts(context: Context, id: Long): Boolean {
  47. val resolver = context.contentResolver
  48. val count = resolver.delete(RAW_CONTACTS_URI, "_id=$id", null)
  49. return count > 0
  50. }
  51. fun query(context: Context): List<ContactsData> {
  52. val list = ArrayList<ContactsData>()
  53. val resolver = context.contentResolver
  54. val cursor = resolver.query(RAW_CONTACTS_URI, null, null, null, null)
  55. while (cursor.moveToNext()) {
  56. val id = cursor.getLong("_id")
  57. val name = cursor.getString(DISPLAY_NAME)
  58. val phone = getCursor(id, TYPE_PHONE, resolver)?.getString("data1")
  59. val email = getCursor(id, TYPE_EMALL, resolver)?.getString("data1")
  60. list.add(ContactsData(id, name, "$phone", "$email"))//TODO
  61. }
  62. return list
  63. }
  64. //创建时构造
  65. private fun getDataValues(id: Long, mimeType: String, data1: String, data2: String): ContentValues {
  66. val values = ContentValues()
  67. values.put(RAW_CONTACT_ID, id)
  68. values.put(MIME_TYPE, mimeType)
  69. values.put("data1", data1)
  70. values.put("data2", data2)
  71. return values
  72. }
  73. //更新时构造
  74. private fun getDataValues(data1: String, data2: String): ContentValues {
  75. val values = ContentValues()
  76. values.put("data1", data1)
  77. values.put("data2", data2)
  78. return values
  79. }
  80. private fun getCursor(id: Long, mimeType: Int, resolver: ContentResolver): Cursor? {
  81. val c = resolver.query(DATA_URI, null, CONDITION, arrayOf("$id", "$mimeType"), null)
  82. return if (c.moveToNext()) c else null
  83. }
  84. }
  85. }
  86. data class ContactsData(var id: Long = 0, val name: String, val phone: String, val email: String)
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注