Query & Interact with Apps in Android 11 with Package Visibility
source link: https://devblogs.microsoft.com/xamarin/android-11-package-visibility/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Query & Interact with Apps in Android 11 with Package Visibility
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.
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.
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<string> 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
.
<manifest package="com.mycompany.myapp">
<queries>
<intent>
<action android:name="android.intent.action.SENDTO" />
<data android:scheme="mailto" />
</intent>
</queries>
</manifest>
If you need query multiple intents or use multiple APIs you will need to add them all into the list.
<queries>
<intent>
<action android:name="android.intent.action.SENDTO" />
<data android:scheme="mailto" />
</intent>
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="http"/>
</intent>
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="https"/>
</intent>
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="smsto"/>
</intent>
</queries>
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.
Learn More
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.
James Montemagno
Principal Lead Program Manager, .NET Community
Follow
Recommend
-
116
PerfOps cli - Global network testing and benchmarking A simple command line tool to interact with hundreds of servers around the world. Run benchmarks and debug your infrastructure without leaving your console.
-
71
Learn new Google tools with your community. Find a DevFest near you!
-
84
cri - chrome remote interface Package cri provides type-safe bindings for devtools protocol. It can be used with Chrome or any other target that implements the interface. Protocol is generated by cmd/generate.sh. Scrip...
-
100
vimux - vim plugin to interact with tmux
-
41
More visibility into the Android Open Source Project 2018-12-15...
-
22
README.md go-simplejson a Go package to interact with arbitrary JSON
-
32
Posted by David Winer , Product Manager One of the central themes fo...
-
6
-
21
React Native Health A React Native package to interact with Apple HealthKit for iOS. Getting Started Automatic Installation Install the react-native-health package from
-
12
Web Front-end18 minute readCreating React Apps With Redux Toolkit and RTK QueryHave you ever wanted to use...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK