Android 在 Android 11 台设备上将目标版本更新为 30 后,从意图打开地图和从意图拨打电话不起作用

Android map opening from intent and call dialing from intent not working after updating target version to 30 on Android 11 devices

这似乎是 Android 提到的包可见性问题 https://developer.android.com/about/versions/11/privacy/package-visibility

fun Activity.callPlayStoreIntent() {
    val appPackageName = this.packageName
    try {
        val uri = Uri.parse("market://details?id=$appPackageName")
        val intent = Intent(ACTION_VIEW, uri)
        startActivity(intent)
    } catch (exception: ActivityNotFoundException) {
        try {
            val uri = Uri.parse("https://play.google.com/store/apps/details?id=$appPackageName")
            val intent = Intent(ACTION_VIEW, uri)
            startActivity(intent)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}


fun Context.callFromDialer(number: String) {
    try {
        val callIntent = Intent(Intent.ACTION_DIAL)
        callIntent.data = Uri.parse("tel:$number")
        if (callIntent.resolveActivity(packageManager) != null) {
            startActivity(callIntent)
        }
    } catch (e: Exception) {
        e.printStackTrace()
        Toast.makeText(this, "No SIM Found", Toast.LENGTH_LONG).show()
    }
}


fun Context.intentOpenMap(
    latitude: Double,
    longitude: Double,
    label: String,
) {
    try {
        val uriBegin = "geo:$latitude,$longitude"
        val query = "$latitude,$longitude($label)"
        val encodedQuery = Uri.encode(query)
        val uriString = "$uriBegin?q=$encodedQuery&z=20"
        val uri = Uri.parse(uriString)
        val intent = Intent(Intent.ACTION_VIEW, uri)
        if (intent.resolveActivity(packageManager) != null) {
            startActivity(intent)
        }
    } catch (ex: Exception) {
        ex.printStackTrace()
        Toast.makeText(this, "Unable to open Map", Toast.LENGTH_LONG).show()
    }

}

你的问题出在代码行intent.resolveActivity(getPackageManager())。当你调用 resolveActivity 时,你会得到这样的警告:

Consider adding a declaration to your manifest when calling this method; see https://g.co/dev/packagevisibility for details

查看PackageManager下的文档,你会看到这个注释:

Note: If your app targets Android 11 (API level 30) or higher, the methods in this class each return a filtered list of apps. Learn more about how to manage package visibility.

那是什么意思呢?

在 android 11 中,Google 添加了包可见性策略。应用程序现在可以更严格地控​​制查看其他应用程序。您的应用程序将无法查看或访问应用程序之外的应用程序。

你需要做什么?

您需要做的就是将以下代码行添加到 AndroidManifest.xml:

<manifest>
    <queries>
        <intent>
            <action android:name="android.intent.action.VIEW" />
            <data android:scheme="geo" />
        </intent>

        <intent>
            <action android:name="android.intent.action.VIEW" />
            <data android:scheme="market" />
        </intent>

        <intent>
            <action android:name="android.intent.action.VIEW" />
            <data android:scheme="https" />
        </intent>

        <intent>
            <action android:name="android.intent.action.DIAL" />
        </intent>
    </queries>
</manifest>

更多信息:

  1. Package visibility in Android 11
  2. Package visibility filtering on Android