As a developer, keeping up with the latest changes in Android is essential to building robust and up-to-date mobile apps. Android 14, the latest iteration of the Android operating system, is packed with new features and improvements that can enhance the user experience and streamline the development process.
This article is the first in the “Android 14 — What’s New?” series. We will take a closer look at some of the features and updates in Android 14. This is a non-exhaustive list and only covers some of the more exciting new developments. These are based on what’s available in Android 14 Beta 1 and the UpsideDownCake Preview revision 3. These APIs could change between the time of writing and the final release.
Screenshot Detection API
Android 14 introduces a new privacy-preserving Screenshot Detection API that allows apps to detect when a user takes a screenshot. The API lets the app register a callback at an activity level. When a screenshot is taken, a visual notification is shown and the callback is invoked. It is important to note that the callback doesn’t provide the image of the screenshot and as of now only supports screenshots taken with a specific combination of hardware button presses. Screenshots taken using adb
command or instrumentation tests with DeviceCapture
are not supported.
To get started, add the following permission to the manifest
<uses-permission android:name="android.permission.DETECT_SCREEN_CAPTURE" />
In the activity, register and unregister the callback and implement the ScreenCaptureCallback
interface.
class ScreenCaptureActivity : AppCompatActivity(), Activity.ScreenCaptureCallback {
...
override fun onStart() {
super.onStart()
registerScreenCaptureCallback(mainExecutor, this)
}
override fun onStop() {
super.onStop()
unregisterScreenCaptureCallback(this)
}
override fun onScreenCaptured() {
// Do what you want to do with the detection
}
...
}
Alternatively, you can create an anonymous object of the interface and use it as follows
class ScreenCaptureActivity : AppCompatActivity() {
...
val screenCaptureCallback = Activity.ScreenCaptureCallback {
// Do what you want to do with the detection
}
override fun onStart() {
super.onStart()
registerScreenCaptureCallback(mainExecutor, screenCaptureCallback)
}
override fun onStop() {
super.onStop()
unregisterScreenCaptureCallback(screenCaptureCallback)
}
...
}
If you are testing this on an emulator, use this adb
command to trigger the hardware combination for taking a screenshot.
adb shell input keyevent 120
Read more about this API over at Android Developers
Selected Photo Access
Android 14 builds on top of the privacy features in previous versions of Android. This new feature allows a user to grant access to specific media files when an app requests visual media permissions READ_MEDIA_IMAGES
or READ_MEDIA_VIDEO
, first introduced in Android 13. To enable this feature, READ_MEDIA_VISUAL_USER_SELECTED
permission has to be declared and requested along with the appropriate media permission.
With the change in the API, the permission dialog will now show the following options
- Select photos, as the name suggests, grants permission for the user-selected photo option
- Allow all, behaves like Android 13, and grants read permission for all media
- Don’t allow, behaves like Android 13, and denies read permission
There’s a behavioral change with the new permission that needs to be accounted for. When the new permission is declared and requested, the permission controller is made aware of the fact the user can re-request the media permission to select more photos and videos. If the user selects Select Photos, the following happens
- The
READ_MEDIA_IMAGES
andREAD_MEDIA_VIDEO
permissions are both denied. - The
READ_MEDIA_VISUAL_USER_SELECTED
permission is granted, providing partial and temporary access to the user’s photos and videos.
In order to get access to other photos and videos, media permissions must be requested again
Read more about this on Android Developers
Back Gesture Preview
Technically available in Android 13 as an early preview, Android 14 stable is expected to come with full predictive back gesture support out of the box. When fully implemented, it lets users preview the destination or result of the back gesture, allowing them to decide if they want to stay or continue with the gesture.
With Android 14, supporting Predictive Back Gesture is as easy as adding this entry in <application>
tag of your manifest.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission ... />
...
<application
...
android:enableOnBackInvokedCallback="true"
... >
<activity ... />
...
</application>
</manifest>
To see it in action, enable the feature toggle in the Developer options.
Read more about the predictive back gesture at Android Developers.
For regular updates and more Android goodness, follow me on my socials. If you want to support me with the work I do, feel free to buy me a coffee!
Originally published on ProAndroidDev
Comments