Android11上获取联系人APP的隐式intent检查返回null但是可以正常运行,要怎么修复?

新手上路,请多包涵

按照《Android编程权威指南第四版》中的教程,有如下代码:

suspectButton.apply {
    val pickContactIntent = Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI)
    setOnClickListener {
        startActivityForResult(pickContactIntent, REQUEST_CONTACT)
    }

    val packageManager: PackageManager = requireActivity().packageManager
    val resolvedActivity: ResolveInfo? = packageManager.resolveActivity(pickContactIntent, PackageManager.MATCH_DEFAULT_ONLY)
    if(resolvedActivity == null) {
        isEnabled = false
    }
}

目前,如果去掉对resolvedActivity的检查,会正常弹出联系人APP,且可以正常获取选择的联系人名字。
但是检查代码返回的一直是null。

编译环境:

  1. compileSdk 32
  2. minSdk 21
  3. targetSdk 32
  4. 依赖:
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.6.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
implementation 'androidx.recyclerview:recyclerview:1.2.1'
implementation 'androidx.room:room-runtime:2.4.2'
kapt 'androidx.room:room-compiler:2.4.2'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

运行环境:

  1. 模拟器API28,正常运行且检查代码没有返回null。
  2. Android11真机(realmex7),检查代码返回null导致按钮被禁用。
阅读 1.8k
1 个回答
新手上路,请多包涵

在bnr上找到答案了:

<queries>
    <package android:name="com.android.contacts"/>
</queries>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题