Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Consider adding a queries declaration to your manifest when calling this method when using intent.resolveActivity in android 11

I've an extension function for opening an intent for my activities:

fun Activity.openIntent(action: String?, type: String?, uri: Uri?) {
    Intent()
        .apply {
            action?.let { this.action = it }
            uri?.let { this.data = it }
            type?.let { this.type = it }
        }
        .also { intent ->
            packageManager?.let {
                if (intent.resolveActivity(it) != null)
                    startActivity(intent)
                else
                    showToast(R.string.application_not_found)
            }
        }
}

My targetSdkVersion is 30. It gives me a warning in intent.resolveActivity(it):

Consider adding a queries declaration to your manifest when calling this method.

So What should I do to solve this warning?

like image 811
Saman Sattari Avatar asked Nov 21 '20 16:11

Saman Sattari


1 Answers

The simplest solution is to get rid of resolveActivity(). Replace:

            packageManager?.let {
                if (intent.resolveActivity(it) != null)
                    startActivity(intent)
                else
                    showToast(R.string.application_not_found)
            }

with:

            try {
                startActivity(intent)
            } catch (ex: ActivityNotFoundException) {
                showToast(R.string.application_not_found)
            }

This gives you the same result with a bit better performance, and it will get rid of the warning.

Another option would be to add the QUERY_ALL_PACKAGES permission. This may get you banned from the Play Store.

Otherwise, you will need to:

  • Build a list of every possible openIntent() call that your app may make

  • Add a <queries> element to your manifest that lists all of those possible Intent structures that you want to be able to use with resolveActivity()

  • Repeat this process periodically, in case you add new scenarios for openIntent()

like image 179
CommonsWare Avatar answered Sep 18 '22 23:09

CommonsWare