从Kotlin的联系人列表中获取联系人信息



我正在尝试使用Kotlin获取有关用户可以在联系人列表中选择的联系人的一些信息。我尝试了几个教程,只有部分工作。这是应该调用startActivityForResult()的代码,来自MainActivity.kt:

add_contact.setOnClickListener {
val intent = Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI)
startActivityForResult(intent, CONTACT_PICK_CODE)
}

下面是重写的onActivityResult方法:

@SuppressLint("Range")
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
// handle intent results || calls when user from Intent (Contact Pick) picks or cancels pick contact
if (resultCode == RESULT_OK) {
if (requestCode == CONTACT_PICK_CODE) {
val cursor1: Cursor
val cursor2: Cursor?
// get data from intent
val uri = data!!.data
cursor1 = contentResolver.query(uri!!, null, null, null, null)!!
if (cursor1.moveToFirst()) {
// get contact details
val contactId = cursor1.getString(cursor1.getColumnIndex(ContactsContract.Contacts._ID))
val contactName = cursor1.getString(cursor1.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))
val contactThumbnail = cursor1.getString(cursor1.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI))
val idResults = cursor1.getString(cursor1.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))
val idResultHold = idResults.toInt()
// check if contact has a phone number or not
if (idResultHold == 1) {
cursor2 = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
null,
null
)
// a contact may have multiple phone numbers
var contactNumber : String = ""
while (cursor2!!.moveToNext()) {
// get phone number
contactNumber = cursor2.getString(cursor2.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
}
Toast.makeText(this, "Contact number: $contactNumber", Toast.LENGTH_SHORT).show()
cursor2.close()
}
cursor1.close()
}
}
} else {
Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show()
}
}

打印&;联系电话:…&;永远不会打印联系人号码,这意味着while循环永远不会运行。

所有其他变量(contactId,contactName…)似乎每次都被分配了正确的值,但数字没有。有人知道这是什么原因吗?

cursor2 = > contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, ...

要返回电话号码,您需要将ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URIUri替换为ContactsContract.CommonDataKinds.Phone.CONTENT_URI

不使用String,你可以使用Set来没有重复的数字,因为;这个URI返回所有电话号码除了手机的链接存储;比如谷歌、WhatsApp……等。

@SuppressLint("Range")
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
// handle intent results || calls when user from Intent (Contact Pick) picks or cancels pick contact
if (resultCode == RESULT_OK) {
if (requestCode == CONTACT_PICK_CODE) {
val cursor1: Cursor
val cursor2: Cursor?
// get data from intent
val uri = data!!.data
cursor1 = contentResolver.query(uri!!, null, null, null, null)!!
if (cursor1.moveToFirst()) {
// get contact details
val contactId =
cursor1.getString(cursor1.getColumnIndex(ContactsContract.Contacts._ID))
val contactName =
cursor1.getString(cursor1.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))
val contactThumbnail =
cursor1.getString(cursor1.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI))
val idResults =
cursor1.getString(cursor1.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))
val idResultHold = idResults.toInt()
// check if contact has a phone number or not
if (idResultHold == 1) {
cursor2 = contentResolver.query(
//                            ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, // WRONG
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
null,
null
)
// a contact may have multiple phone numbers
val numbers = mutableSetOf<String>()
cursor2?.let {
while (cursor2.moveToNext()) {
val phoneNumber =
cursor2.getString(
cursor2.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER
)
).replace("-", "").replace(
" ",
""
)  // Remove the dash sign & spaces from the numbers
numbers.add(phoneNumber)
}
Toast.makeText(
this@MainActivity,
"$contactName $numbers",
Toast.LENGTH_LONG
).show()
cursor2.close()
}
cursor1.close()
}
}

}
} else {
Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show()
}
}

注意:你正在使用一个废弃的APIonActivityResultregisterForActivityResult取代

下面是使用它的演示:

class MainActivity : AppCompatActivity() {
private val requestSinglePermission =
registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
// Do something if permission granted
if (isGranted) {
Log.d("LOG_TAG", "permission granted by the user")
// Do something as the permission is not granted
} else {
Log.d("LOG_TAG", "permission denied by the user")
}
}

private var allNumLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
// There are no request codes
val data = result.data?.data
data?.let {
val cursor = contentResolver.query(
data,
null,
null,
null,
null
)
cursor?.let {
if (it.moveToFirst()) {
val name =
it.getString(it.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME))
if (Integer.parseInt(
it.getString(
it.getColumnIndex(
ContactsContract.Contacts.HAS_PHONE_NUMBER
)
)
) > 0 // Check if the contact has phone numbers
) {
val id =
it.getString(it.getColumnIndex(ContactsContract.Contacts._ID))
val phonesCursor = contentResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id,
null,
null
)
val numbers = mutableSetOf<String>()
phonesCursor?.let {
while (phonesCursor.moveToNext()) {
val phoneNumber =
phonesCursor.getString(
phonesCursor.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER
)
).replace("-", "").replace(" ", "")
numbers.add(phoneNumber)
}
Toast.makeText(
this@MainActivity,
"$name $numbers",
Toast.LENGTH_LONG
).show()
Log.d(TAG, "$name $numbers")
}
phonesCursor?.close()
} else {
Toast.makeText(
this@MainActivity,
"$name - No numbers",
Toast.LENGTH_LONG
).show()
Log.d(TAG, "$name - No numbers")
}
}
cursor.close()
}
}
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (ContextCompat.checkSelfPermission(
this,
Manifest.permission.READ_CONTACTS
) != PackageManager.PERMISSION_GRANTED
) {
requestSinglePermission.launch(
Manifest.permission.READ_CONTACTS
)
}

// get a contact:
val intent = Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI) 
allNumLauncher.launch(intent)
}
}

尝试从联系人中获取多个电话号码

const val REQUEST_SELECT_PHONE_NUMBER = 1
fun selectContact() {
// Start an activity for the user to pick a phone number from contacts
val intent = Intent(Intent.ACTION_PICK).apply {
type = CommonDataKinds.Phone.CONTENT_TYPE
}
if (intent.resolveActivity(packageManager) != null) {
startActivityForResult(intent, REQUEST_SELECT_PHONE_NUMBER)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
if (requestCode == REQUEST_SELECT_PHONE_NUMBER && resultCode == Activity.RESULT_OK) {
// Get the URI and query the content provider for the phone number
val contactUri: Uri = data.data
val projection: Array<String> = arrayOf(CommonDataKinds.Phone.NUMBER)
contentResolver.query(contactUri, projection, null, null, null).use { cursor ->
// If the cursor returned is valid, get the phone number
if (cursor.moveToFirst()) {
val numberIndex = cursor.getColumnIndex(CommonDataKinds.Phone.NUMBER)
val number = cursor.getString(numberIndex)
// Do something with the phone number
...
}
val c: Cursor =  managedQuery(contactUri, null, null, null, null)
val name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))
}
}
}

相关内容

  • 没有找到相关文章

最新更新