1.聯絡人權限
<uses-permission android:name="android.permission.READ_CONTACTS"/>
2.聯絡人工具類
import android.content.ContentResolver
import android.net.Uri
import android.provider.ContactsContract
object ContactUtil {
//獲取所有聯絡人
fun getContacts(contentResolver: ContentResolver): List<Contact>? {
val contacts = ArrayList<Contact>()
val uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI
val projection = arrayOf(
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.Contacts.DISPLAY_NAME
)
try {
contentResolver.query(uri, projection, null, null, null).use { cursor ->
var phoneNum: String
var name: String
cursor?.apply {
if (count > 0) {
while (moveToNext()) {
phoneNum =
getString(getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
name =
getString(getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))
phoneNum = phoneNum.replace("^(\\+886)".toRegex(), "0")
phoneNum = phoneNum.replace("^(886)".toRegex(), "0")
phoneNum = phoneNum.replace("-".toRegex(), "")
phoneNum = phoneNum.replace(" ".toRegex(), "")
phoneNum = phoneNum.trim { it <= ' ' }
val contact = Contact(name,phoneNum)
contacts.add(contact)
}
}
}
}
} catch (e: Exception) {
e.printStackTrace()
}
return contacts
}
//關鍵字過濾
fun getFilterData(filter: String?, contentResolver: ContentResolver): List<Contact>? {
val contacts = ArrayList<Contact>()
val uri =
Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, filter)
val projection = arrayOf(
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER
)
try {
contentResolver.query(uri, projection, null, null, null).use { cursor ->
var phoneNum: String
var name: String
cursor?.apply {
if (count > 0) {
while (moveToNext()) {
phoneNum =
getString(getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
name =
getString(getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))
phoneNum = phoneNum.replace("^(\\+886)".toRegex(), "0")
phoneNum = phoneNum.replace("^(886)".toRegex(), "0")
phoneNum = phoneNum.replace("-".toRegex(), "")
phoneNum = phoneNum.replace(" ".toRegex(), "")
phoneNum = phoneNum.trim { it <= ' ' }
val contact = Contact(name, phoneNum)
contacts.add(contact)
}
}
}
}
} catch (e: java.lang.Exception) {
e.printStackTrace()
}
return contacts
}
data class Contact(var name: String, var phone: String)
}
3.效果展示
private val READ_CONTACT_REQUEST = 40
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (ContextCompat.checkSelfPermission(this,Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(arrayOf(Manifest.permission.READ_CONTACTS), READ_CONTACT_REQUEST)
} else {
getContact()
}
}
private fun getContact() {
val contacts = ContactUtil.getContacts(contentResolver)
contacts?.also {
contact ->
for (index in contact.indices) {
val name = contact[index].name
val phone = contact[index].phone
}
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == READ_CONTACT_REQUEST && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
getContact()
}
}