标签:adc cursor idt content sel self grant height comm
<uses-permission android:name="android.permission.READ_CONTACTS"/>
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_CONTACTS), 1)
} else {
readContacts() //读取联系人信息
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
when (requestCode) {
1 ->
if (grantResults.size > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
readContacts()
} else {
Toast.makeText(this, "You denied the permission", Toast.LENGTH_SHORT).show()
}
}
}
var adapter: ArrayAdapter<String>? = null
var contactsList = mutableListOf<String>()
//实例化 适配器
adapter = ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, contactsList)
//为listView 设置适配器
lv_contents_view.adapter = adapter
/**
* 加载联系人信息
*/
private fun readContacts() {
var cursor: Cursor
//查询联系人数据
cursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null, null)
if (cursor != null) {
while (cursor.moveToNext()) {
//获取联系人姓名
var displayName = cursor.getString(cursor.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME))
//获取联系人电话
var number = cursor.getString(cursor.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER))
// 添加联系人信息到集合当中
contactsList.add(displayName + "\n" + number)
}
//刷新 ListView
adapter?.notifyDataSetChanged()
}
cursor.close() //关闭 Cursor 对象
}
标签:adc cursor idt content sel self grant height comm
原文地址:http://www.cnblogs.com/-Tiger/p/7137067.html