[英]Jetpack Compose Contact Picker
So... Yesterday my code was working just fine, but today, I don't know what happend it stoped working.所以...昨天我的代码工作得很好,但今天,我不知道发生了什么,它停止工作了。 The cursor is null and returns both "".
cursor 是 null 并返回两个“”。 I don't know what to do.
我不知道该怎么办。
Is the cursor even working? cursor 还能用吗?
@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")
}
}
How does the cursor works? cursor 是如何工作的? Do I have to wait?
我必须等待吗? am I supposed to ask permission to access the contacts?
我是否应该请求访问联系人的权限?
Apparently Yes you need to ask for permission in runtime to get access the contact.显然是的,您需要在运行时请求许可才能访问联系人。 For References check this link [check link][1]https://www.geeksforgeeks.org/contact-picker-in-android-using-jetpack-compose/
对于参考,请检查此链接 [检查链接][1]https://www.geeksforgeeks.org/contact-picker-in-android-using-jetpack-compose/
Replace: var name = "" -> var name = remember{ mutableStateOf("") }替换:var name = "" -> var name = remember{ mutableStateOf("") }
var number = "" -> var number = remember{ mutableStateOf("") } var number = "" -> var number = remember{ mutableStateOf("") }
and while accessing, use name.value and number.value并在访问时使用 name.value 和 number.value
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.