【问题标题】:Jetpack Compose Contact PickerJetpack Compose 联系人选取器
【发布时间】:2023-01-05 20:50:58
【问题描述】:

所以...昨天我的代码工作得很好,但今天,我不知道发生了什么,它停止工作了。 游标为空,同时返回“”。我不知道该怎么办。

光标还能用吗?

@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")
    }
}

游标是如何工作的? 我需要等待吗? 我应该征得访问联系人的许可吗?

【问题讨论】:

    标签: android kotlin android-jetpack-compose cursor jetpack


    【解决方案1】:

    显然是的,您需要在运行时请求许可才能访问联系人。对于参考检查此链接 [查看链接][1]https://www.geeksforgeeks.org/contact-picker-in-android-using-jetpack-compose/

    【讨论】:

      猜你喜欢
      • 2022-11-14
      • 1970-01-01
      • 1970-01-01
      • 2012-07-02
      • 2018-01-09
      • 1970-01-01
      • 2019-07-08
      • 1970-01-01
      • 2017-02-05
      相关资源
      最近更新 更多