Query & Interact with Apps in Android 11 with Package

News

Avatar

James

October 15th, 2020

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’s device.

App icons for email apps and code to query intents on the device

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 Intent. Before calling StartActivity it is best practice to QueryIntentActivities or ResolveActivity to ensure there is an application that can handle the request. If you are using Xamarin.Essentials, then you may not have seen these APIs because the library handles all of the logic for you automatically for Browser(External), Email, and SMS.

Before Android 11 every app could easily query all installed applications and see if a specific Intent would open when StartActivity 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.

Android project settings targeting Android 11

Once you retarget to Android 11 and run your application on a device running Android 11 you will receive zero results if you use QueryIntentActivities. If you are using Xamarin.Essentials you will receive a FeatureNotSupportedException when you try to call one of the APIs that needs to query activities. Let’s say you are using the Email feature of Xamarin.Essentials. Your code may look like this:

public async Task SendEmail(string subject, string body, List recipients) { try { var message = new EmailMessage { Subject = subject, Body = body, To = recipients }; await Email.ComposeAsync(message); } catch (FeatureNotSupportedException fbsEx) { // Email is not supported on this device } catch (Exception ex) { // Some other exception occurred } }

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 FeatureNotSupportedException 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 queries into your AndroidManifest.xml.

If you need query multiple intents or use multiple APIs you will need to add them all into the list.

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.

Be sure to browse through the official Android 11 documentation on package visibility, and of course the newly updated Xamarin.Essentials documentation. Finally, be sure to read through the Xamarin.Android 11 release notes.

This article originally appeared on https://devblogs.microsoft.com/xamarin/android-11-package-visibility/

Leave a Reply