昨天我的代码运行的很好,但是今天,我不知道发生了什么,它停止工作了。光标是空的并且返回两个“"。我不知道该怎么办。
光标还能用吗?
@SuppressLint("Range", "Recycle")
@Composable
fun ContactPickerTwinTurbo(
done: (String, String) -> Unit
) {
val context = LocalContext.current
val launcher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.PickContact(),
onResult = {
val contentResolver: ContentResolver = context.contentResolver
var name = ""
var number = ""
val cursor: Cursor? = contentResolver.query(it!!, null, null, null, null)
if (cursor != null) {
if (cursor.moveToFirst()) {
name =
cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))
Log.d("Name", name)
val id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID))
val phones: Cursor? = contentResolver.query(
Phone.CONTENT_URI, null,
Phone.CONTACT_ID + " = " + id, null, null
)
if (phones != null) {
while (phones.moveToNext()) {
number = phones.getString(phones.getColumnIndex(Phone.NUMBER))
Log.d("Number", number)
}
phones.close()
}
}
}
done(name, number)
}
)
Button(
onClick = {
launcher.launch()
},
modifier = Modifier
.fillMaxWidth()
.padding(10.dp)
) {
Text(text = "Pick Contact")
}
}
光标是如何工作的?我必须等待吗?我应该请求权限才能访问联系人吗?
1条答案
按热度按时间jq6vz3qz1#
显然是的,您需要在运行时请求权限才能访问联系人。有关参考,请查看此链接[check link][1] www.geeksforgeeks.org/contact-picker-in-android-using-jetpack-compose/