Home > Enterprise >  Android map opening from intent and call dialing from intent not working after updating target versi
Android map opening from intent and call dialing from intent not working after updating target versi

Time:10-30

This seems like a package visibility issue as mentioned by 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()
    }

}

CodePudding user response:

Your problem lies in the line of code intent.resolveActivity(getPackageManager()). When you call resolveActivity, you will get a warning like this:

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

Check the document under PackageManager, you will see this note:

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.

So what does that mean?

In android 11, Google added package visibility policy. Apps now have tighter control over viewing other apps. Your application will not be able to view or access applications outside of your application.

What do you need to do?

All you need to do is add below line of code to 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>

More information:

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