page.title=Android 4.2 APIs excludeFromSuggestions=true sdk.platform.version=4.2 sdk.platform.apiLevel=17 @jd:body

In this document

  1. Important Behavior Changes
  2. Daydream
  3. Secondary Displays
  4. Lockscreen Widgets
  5. Multiple Users
  6. RTL Layout Support
  7. Nested Fragments
  8. Renderscript

See also

  1. API Differences Report »

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.

Important Behavior Changes

If you have previously published an app for Android, be aware of the following changes that might affect your app’s behavior:

Daydream

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 } element in your manifest file. You must then include an intent filter with the action {@code "android.service.dreams.DreamService"}. For example:

<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.

Secondary Displays

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