{"id":48151,"date":"2020-10-15T11:38:30","date_gmt":"2020-10-15T18:38:30","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/xamarin\/?p=48151"},"modified":"2020-10-15T08:40:33","modified_gmt":"2020-10-15T15:40:33","slug":"android-11-package-visibility","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/android-11-package-visibility\/","title":{"rendered":"Query &amp; Interact with Apps in Android 11 with Package Visibility"},"content":{"rendered":"<p>Android 11 introduced several exciting updates for developers to integrate into their app experience including new device and media controls, enhanced support for foldables, and a lot more. In addition to new features there are also several privacy enhancements that developers need to integrate into their application when upgraded and re-targeting to Android 11. One of those enhancements is the introduction of package visibility that alters the ability to query installed applications and packages on a user&#8217;s device.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/10\/queryintentactivities.png\" alt=\"App icons for email apps and code to query intents on the device\" \/><\/p>\n<p>When you want to open a browser or send an email then your application will have to launch and interact with another application on the device through an <code>Intent<\/code>. Before calling <code>StartActivity<\/code> it is best practice to <code>QueryIntentActivities<\/code> or <code>ResolveActivity<\/code> to ensure there is an application that can handle the request. If you are using <a href=\"https:\/\/docs.microsoft.com\/xamarin\/essentials?WT.mc_id=packagevisibility-blog-jamont\">Xamarin.Essentials<\/a>, then you may not have seen these APIs because the library handles all of the logic for you automatically for <a href=\"https:\/\/docs.microsoft.com\/xamarin\/essentials\/open-browser?WT.mc_id=packagevisibility-blog-jamont\">Browser(External)<\/a>, <a href=\"https:\/\/docs.microsoft.com\/xamarin\/essentials\/email?WT.mc_id=packagevisibility-blog-jamont\">Email<\/a>, and <a href=\"https:\/\/docs.microsoft.com\/xamarin\/essentials\/sms?WT.mc_id=packagevisibility-blog-jamont\">SMS<\/a>.<\/p>\n<p>Before Android 11 every app could easily query all installed applications and see if a specific <code>Intent<\/code> would open when <code>StartActivity<\/code> is called. That has all changed with Android 11 with the introduction of package visibility. You will now need to declare what intents and data schemes you want your app to be able to query when your app is targeting Android 11.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/10\/TargetAndroid11.png\" alt=\"Android project settings targeting Android 11\" \/><\/p>\n<p>Once you retarget to Android 11 and run your application on a device running Android 11 you will receive zero results if you use <code>QueryIntentActivities<\/code>. If you are using Xamarin.Essentials you will receive a <code>FeatureNotSupportedException<\/code> when you try to call one of the APIs that needs to query activities. Let&#8217;s say you are using the Email feature of Xamarin.Essentials. Your code may look like this:<\/p>\n<pre><code class=\"csharp\">public async Task SendEmail(string subject, string body, List&lt;string&gt; recipients)\n{\n    try\n    {\n        var message = new EmailMessage\n        {\n            Subject = subject,\n            Body = body,\n            To = recipients\n        };\n        await Email.ComposeAsync(message);\n    }\n    catch (FeatureNotSupportedException fbsEx)\n    {\n        \/\/ Email is not supported on this device\n    }\n    catch (Exception ex)\n    {\n        \/\/ Some other exception occurred\n    }\n}\n<\/code><\/pre>\n<p>If your app targeted Android 10 and earlier, it would just work. With package visibility in Android 11 when you try to send an Email, Xamarin.Essentials will try to query for pacakges that support email and zero results will be return. This will result in a <code>FeatureNotSupportedException<\/code> to be thrown, which is not ideal. To enable your application to get visbility into the packages you will need to add a list of <code>queries<\/code> into your <code>AndroidManifest.xml<\/code>.<\/p>\n<pre><code class=\"xml\">&lt;manifest package=\"com.mycompany.myapp\"&gt;\n  &lt;queries&gt;\n    &lt;intent&gt;\n      &lt;action android:name=\"android.intent.action.SENDTO\" \/&gt;\n      &lt;data android:scheme=\"mailto\" \/&gt;\n    &lt;\/intent&gt;\n  &lt;\/queries&gt;\n&lt;\/manifest&gt;\n<\/code><\/pre>\n<p>If you need query multiple intents or use multiple APIs you will need to add them all into the list.<\/p>\n<pre><code class=\"xml\">&lt;queries&gt;\n  &lt;intent&gt;\n    &lt;action android:name=\"android.intent.action.SENDTO\" \/&gt;\n    &lt;data android:scheme=\"mailto\" \/&gt;\n  &lt;\/intent&gt;\n  &lt;intent&gt;\n    &lt;action android:name=\"android.intent.action.VIEW\" \/&gt;\n    &lt;data android:scheme=\"http\"\/&gt;\n  &lt;\/intent&gt;\n  &lt;intent&gt;\n    &lt;action android:name=\"android.intent.action.VIEW\" \/&gt;\n    &lt;data android:scheme=\"https\"\/&gt;\n  &lt;\/intent&gt;\n  &lt;intent&gt;\n    &lt;action android:name=\"android.intent.action.VIEW\" \/&gt;\n    &lt;data android:scheme=\"smsto\"\/&gt;\n  &lt;\/intent&gt;\n&lt;\/queries&gt;\n<\/code><\/pre>\n<p>And there you have it, with just a small amount of configuration you are app will continue to work flawless when you target Android 11.<\/p>\n<h2>Learn More<\/h2>\n<p>Be sure to browse through the official <a href=\"https:\/\/developer.android.com\/about\/versions\/11\/privacy\/package-visibility\">Android 11 documentation<\/a> on package visibility, and of course the newly updated <a href=\"https:\/\/docs.microsoft.com\/xamarin\/essentials?WT.mc_id=packagevisibility-blog-jamont\">Xamarin.Essentials documentation<\/a>. Finally, be sure to read through the Xamarin.Android 11 <a href=\"https:\/\/docs.microsoft.com\/en-us\/xamarin\/android\/release-notes\/11\/11.0?WT.mc_id=packagevisibility-blog-jamont\">release notes<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are several exciting new APIs and enhancements in Android 11. One of the more interesting changes for developers is changes to package visibility. In this post we will see what package visibility is and what changes need to be made to apps to query and interact with apps installed on the user&#8217;s device.<\/p>\n","protected":false},"author":544,"featured_media":48149,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[313,556,1],"tags":[9174,9175,24],"class_list":["post-48151","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android","category-integrations","category-xamarin","tag-android-11","tag-package-visibility","tag-xamarin-essentials"],"acf":[],"blog_post_summary":"<p>There are several exciting new APIs and enhancements in Android 11. One of the more interesting changes for developers is changes to package visibility. In this post we will see what package visibility is and what changes need to be made to apps to query and interact with apps installed on the user&#8217;s device.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/48151","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/544"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=48151"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/48151\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/48149"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=48151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=48151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=48151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}