page.title=Android 4.2 APIs excludeFromSuggestions=true sdk.platform.version=4.2 sdk.platform.apiLevel=17 @jd:body
API Level: 17
Android 4.2 ({@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1}) is an update to the Jelly Bean release that offers new features for users and app developers. This document provides an introduction to the most notable and useful new APIs for developers.
As an app developer, you should download the Android 4.2 system image and SDK platform from the SDK Manager as soon as possible. If you don’t have a device running Android 4.2 on which to test your app, use the Android 4.2 system image to test your app on the Android emulator. Then build your apps against the Android 4.2 platform to begin using the latest APIs.
To better optimize your app for devices running Android {@sdkPlatformVersion},
you should set your {@code targetSdkVersion} to
"{@sdkPlatformApiLevel}"
, install it on an Android {@sdkPlatformVersion} system image,
test it, then publish an update with this change.
You can use APIs in Android {@sdkPlatformVersion} while also supporting older versions by adding conditions to your code that check for the system API level before executing APIs not supported by your {@code minSdkVersion}. To learn more about maintaining backward-compatibility, read Creating Backward-Compatible UIs.
More information about how API levels work is available in What is API Level?
If you have previously published an app for Android, be aware of the following changes that might affect your app’s behavior:
This change takes effect only if you set either {@code android:targetSdkVersion} or {@code android:minSdkVersion} to 17 or higher. Otherwise, the default value is still {@code “true"} even when running on Android 4.2 and higher.
To meet the privacy expectations of users when your app requests permission for coarse location (and not fine location), the system will not provide a user location estimate that’s more accurate than a city block.
Even if your value for {@code android:targetSdkVersion} and {@code android:minSdkVersion} is lower than 17, your app is not able to modify the settings that have moved to {@link android.provider.Settings.Global} when running on Android 4.2 and higher.
Read more about binding JavaScript code to Android code.
Daydream is a new interactive screensaver mode for Android devices. It activates automatically when the device is inserted into a dock or when the device is left idle while plugged in to a charger (instead of turning the screen off). Daydream displays one dream at a time, which may be a purely visual, passive display that dismisses upon touch, or may be interactive and responsive to the full suite of input events. Your dreams run in your app’s process and have full access to the Android UI toolkit, including views, layouts, and animations, so they are more flexible and powerful than either live wallpapers or app widgets.
You can create a dream for Daydream by implementing a subclass of {@link android.service.dreams.DreamService}. The {@link android.service.dreams.DreamService} APIs are designed to be similar to those of {@link android.app.Activity}. To specify the UI for your dream, pass a layout resource ID or {@link android.view.View} to {@link android.service.dreams.DreamService#setContentView setContentView()} at any point after you have a window, such as from the {@link android.service.dreams.DreamService#onAttachedToWindow()} callback.
The {@link android.service.dreams.DreamService} class provides other important lifecycle callback methods on top of the base {@link android.app.Service} APIs, such as {@link android.service.dreams.DreamService#onDreamingStarted()}, {@link android.service.dreams.DreamService#onDreamingStopped()}, and {@link android.service.dreams.DreamService#onDetachedFromWindow()}. You cannot initiate a {@link android.service.dreams.DreamService} from your app—it is launched automatically by the system.
If your dream is interactive, you can start an activity from the dream to send the user into your app’s full UI for more detail or control. You can use {@link android.service.dreams.DreamService#finish()} to end the dream so the user can see the new Activity.
To make your daydream available to the system, declare your {@link
android.service.dreams.DreamService} with a {@code
<service android:name=".MyDream" android:exported="true" android:icon="@drawable/dream_icon" android:label="@string/dream_label" > <intent-filter> <action android:name="android.service.dreams.DreamService" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </service>
There are some other useful methods in {@link android.service.dreams.DreamService} to be aware of:
For more information, see the {@link android.service.dreams.DreamService} documentation.
Android now allows your app to display unique content on additional screens that are connected to the user’s device over either a wired connection or Wi-Fi. To create unique content for a secondary display, extend the {@link android.app.Presentation} class and implement the {@link android.app.Presentation#onCreate onCreate()} callback. Within {@link android.app.Presentation#onCreate onCreate()}, specify your UI for the secondary display by calling {@link android.app.Presentation#setContentView setContentView()}. As an extension of the {@link android.app.Dialog} class, the {@link android.app.Presentation} class provides the region in which your app can display a unique UI on the secondary display.
To detect secondary displays where you can display your {@link android.app.Presentation}, use either the {@link android.hardware.display.DisplayManager} or {@link android.media.MediaRouter} APIs. While the {@link android.hardware.display.DisplayManager} APIs allow you to enumerate multiple displays that may be connected at once, you should usually use {@link android.media.MediaRouter} instead to quickly access the system’s default display for presentations.
To get the default display for your presentation, call {@link android.media.MediaRouter#getSelectedRoute MediaRouter.getSelectedRoute()} and pass it {@link android.media.MediaRouter#ROUTE_TYPE_LIVE_VIDEO}. This returns a {@link android.media.MediaRouter.RouteInfo} object that describes the system’s currently selected route for video presentations. If the {@link android.media.MediaRouter.RouteInfo} is not null, call {@link android.media.MediaRouter.RouteInfo#getPresentationDisplay()} to get the {@link android.view.Display} representing the connected display.
You can then display your presentation by passing the {@link android.view.Display} object to a constructor for your {@link android.app.Presentation} class. Your presentation will now appear on the secondary display.
To detect at runtime when a new display has been connected, create an instance of {@link android.media.MediaRouter.SimpleCallback} in which you implement the {@link android.media.MediaRouter.SimpleCallback#onRoutePresentationDisplayChanged onRoutePresentationDisplayChanged()} callback method, which the system will call when a new presentation display is connected. Then register the {@link android.media.MediaRouter.SimpleCallback} by passing it to {@link android.media.MediaRouter#addCallback MediaRouter.addCallback()} along with the {@link android.media.MediaRouter#ROUTE_TYPE_LIVE_VIDEO} route type. When you receive a call to {@link android.media.MediaRouter.SimpleCallback#onRoutePresentationDisplayChanged onRoutePresentationDisplayChanged()}, simply call {@link android.media.MediaRouter#getSelectedRoute MediaRouter.getSelectedRoute()} as mentioned above.
To further optimize the UI in your {@link android.app.Presentation} for secondary screens, you can apply a different theme by specifying the {@link android.R.attr#presentationTheme android:presentationTheme} attribute in the {@code