API Level: 16

Android 4.1 ([JELLY_BEAN](https://developer.android.com/reference/android/os/Build.VERSION_CODES#JELLY_BEAN)) is a progression of the platform that offers improved performance and enhanced user experience. It adds new features for users and app developers. This document provides an introduction to the most notable and useful new APIs for app developers.

As an app developer, Android 4.1 is available to you from the[SDK Manager](https://developer.android.com/tools/help/sdk-manager)as a system image you can run in the Android emulator and an SDK platform against which you can build your app. You should download the system image and platform as soon as possible to build and test your app on Android 4.1.

To better optimize your app for devices running Android 4.1, you should set your[`targetSdkVersion`](https://developer.android.com/guide/topics/manifest/uses-sdk-element#target)to`"16"`, install it on an Android 4.1 system image, test it, then publish an update with this change.

You can use APIs in Android 4.1 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[`minSdkVersion`](https://developer.android.com/guide/topics/manifest/uses-sdk-element#min). To learn more about maintaining backward-compatibility, read[Creating Backward-Compatible UIs](https://developer.android.com/training/backward-compatible-ui).

More information about how API levels work is available in[What is API Level?](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

## App Components

### Isolated services

By specifying[`android:isolatedProcess="true"`](https://developer.android.com/guide/topics/manifest/service-element#isolated)in the[`<service>`](https://developer.android.com/guide/topics/manifest/service-element)tag, your[Service](https://developer.android.com/reference/android/app/Service)will run under its own isolated user ID process that has no permissions of its own.

### Memory management

New[ComponentCallbacks2](https://developer.android.com/reference/android/content/ComponentCallbacks2)constants such as[TRIM_MEMORY_RUNNING_LOW](https://developer.android.com/reference/android/content/ComponentCallbacks2#TRIM_MEMORY_RUNNING_LOW)and[TRIM_MEMORY_RUNNING_CRITICAL](https://developer.android.com/reference/android/content/ComponentCallbacks2#TRIM_MEMORY_RUNNING_CRITICAL)provide foreground processes more information about memory state before the system calls[onLowMemory()](https://developer.android.com/reference/android/app/Activity#onLowMemory()).

New[getMyMemoryState(ActivityManager.RunningAppProcessInfo)](https://developer.android.com/reference/android/app/ActivityManager#getMyMemoryState(android.app.ActivityManager.RunningAppProcessInfo))method allows you to retrieve the general memory state.

### Content providers

A new method,[acquireUnstableContentProviderClient()](https://developer.android.com/reference/android/content/ContentResolver#acquireUnstableContentProviderClient(android.net.Uri)), allows you to access a[ContentProviderClient](https://developer.android.com/reference/android/content/ContentProviderClient)that may be "unstable" such that your app will not crash if the content provider does. It's useful when you are interacting with content providers in a separate app.

### Live Wallpapers

New intent protocol to directly launch the live wallpaper preview activity so you can help users easily select your live wallpaper without forcing them to leave your app and navigate through the Home wallpaper picker.

To launch the live wallpaper picker, call[startActivity()](https://developer.android.com/reference/android/content/Context#startActivity(android.content.Intent))with an[Intent](https://developer.android.com/reference/android/content/Intent)using[ACTION_CHANGE_LIVE_WALLPAPER](https://developer.android.com/reference/android/app/WallpaperManager#ACTION_CHANGE_LIVE_WALLPAPER)and an extra that specifies your live wallpaper[ComponentName](https://developer.android.com/reference/android/content/ComponentName)as a string in[EXTRA_LIVE_WALLPAPER_COMPONENT](https://developer.android.com/reference/android/app/WallpaperManager#EXTRA_LIVE_WALLPAPER_COMPONENT).

### App stack navigation

Android 4.1 makes it much easier to implement the proper design patterns for Up navigation. All you need to do is add the[`android:parentActivityName`](https://developer.android.com/guide/topics/manifest/activity-element#parent)to each[`<activity>`](https://developer.android.com/guide/topics/manifest/activity-element)element in your manifest file. The system uses this information to open the appropriate activity when the user presses the Up button in the action bar (while also finishing the current activity). So if you declare the[`android:parentActivityName`](https://developer.android.com/guide/topics/manifest/activity-element#parent)for each activity, you don't need the[onOptionsItemSelected()](https://developer.android.com/reference/android/app/Activity#onOptionsItemSelected(android.view.MenuItem))method to handle click events on the action bar's app icon---the system now handles that event and resumes or creates the appropriate activity.

This is particularly powerful for scenarios in which the user enters one of your app's activities through a "deep dive" intent such as from a notification or an intent from different app (as described in the design guide for[Navigating Between Apps](https://developer.android.com/design/patterns/navigation#between-apps)). When the user enters your activity this way, your app may not naturally have a back stack of activities that can be resumed as the user navigates up. However, when you supply the[`android:parentActivityName`](https://developer.android.com/guide/topics/manifest/activity-element#parent)attribute for your activities, the system recognizes whether or not your app already contains a back stack of parent activities and, if not, constructs a synthetic back stack that contains all parent activities.

**Note:**When the user enters a deep activity in your app and it creates a new task for your app, the system actually inserts the stack of parent activities into the task. As such, pressing the Back button also navigates back through the stack of parent activities.

When the system creates a synthetic back stack for your app, it builds a basic[Intent](https://developer.android.com/reference/android/content/Intent)to create a new instance of each parent activity. So there's no saved state for the parent activities the way you'd expect had the user naturally navigated through each activity. If any of the parent activities normally show a UI that's dependent on the user's context, that context information will be missing and you should deliver it when the user navigates back through the stack. For example, if the user is viewing an album in a music app, navigating up might bring them to an activity that lists all albums in a chosen music genre. In this case, if the stack must be created, it's necessary that you inform the parent activity what genre the current album belongs to so that the parent can display the proper list as if the user actually came from that activity. To deliver such information to a synthetic parent activity, you must override the[onPrepareNavigateUpTaskStack()](https://developer.android.com/reference/android/app/Activity#onPrepareNavigateUpTaskStack(android.app.TaskStackBuilder))method. This provides you with a[TaskStackBuilder](https://developer.android.com/reference/android/app/TaskStackBuilder)object that the system created in order to synthesize the parent activities. The[TaskStackBuilder](https://developer.android.com/reference/android/app/TaskStackBuilder)contains[Intent](https://developer.android.com/reference/android/content/Intent)objects that the system uses to create each parent activity. In your implementation of[onPrepareNavigateUpTaskStack()](https://developer.android.com/reference/android/app/Activity#onPrepareNavigateUpTaskStack(android.app.TaskStackBuilder)), you can modify the appropriate[Intent](https://developer.android.com/reference/android/content/Intent)to add extra data that the parent activity can use to determine the appropriate context and display the appropriate UI.

When the system creates the[TaskStackBuilder](https://developer.android.com/reference/android/app/TaskStackBuilder), it adds the[Intent](https://developer.android.com/reference/android/content/Intent)objects that are used to create the parent activities in their logical order beginning from the top of the activity tree. So, the last[Intent](https://developer.android.com/reference/android/content/Intent)added to the internal array is the direct parent of the current activity. If you want to modify the[Intent](https://developer.android.com/reference/android/content/Intent)for the activity's parent, first determine the length of the array with[getIntentCount()](https://developer.android.com/reference/android/app/TaskStackBuilder#getIntentCount())and pass that value to[editIntentAt()](https://developer.android.com/reference/android/app/TaskStackBuilder#editIntentAt(int)).

If your app structure is more complex, there are several other APIs available that allow you to handle the behavior of Up navigation and fully customize the synthetic back stack. Some of the APIs that give you additional control include:

[onNavigateUp()](https://developer.android.com/reference/android/app/Activity#onNavigateUp())
:   Override this to perform a custom action when the user presses the Up button.

[navigateUpTo(Intent)](https://developer.android.com/reference/android/app/Activity#navigateUpTo(android.content.Intent))
:   Call this to finish the current activity and go to the activity indicated by the supplied[Intent](https://developer.android.com/reference/android/content/Intent). If the activity exists in the back stack, but is not the closest parent, then all other activities between the current activity and the activity specified with the intent are finished as well.

[getParentActivityIntent()](https://developer.android.com/reference/android/app/Activity#getParentActivityIntent())
:   Call this to get the[Intent](https://developer.android.com/reference/android/content/Intent)that will start the logical parent for the current activity.

[shouldUpRecreateTask(Intent)](https://developer.android.com/reference/android/app/Activity#shouldUpRecreateTask(android.content.Intent))
:   Call this to query whether a synthetic back stack must be created in order to navigate up. Returns true if a synthetic stack must be created, false if the appropropriate stack already exists.

[finishAffinity()](https://developer.android.com/reference/android/app/Activity#finishAffinity())
:   Call this to finish the current activity and all parent activities with the same task affinity that are chained to the current activity. If you override the default behaviors such as[onNavigateUp()](https://developer.android.com/reference/android/app/Activity#onNavigateUp()), you should call this method when you create a synthetic back stack upon Up navigation.

[onCreateNavigateUpTaskStack](https://developer.android.com/reference/android/app/Activity#onCreateNavigateUpTaskStack(android.app.TaskStackBuilder))
:   Override this if you need to fully control how the synthetic task stack is created. If you want to simply add some extra data to the intents for your back stack, you should instead override[onPrepareNavigateUpTaskStack()](https://developer.android.com/reference/android/app/Activity#onPrepareNavigateUpTaskStack(android.app.TaskStackBuilder))

However, most apps don't need to use these APIs or implement[onPrepareNavigateUpTaskStack()](https://developer.android.com/reference/android/app/Activity#onPrepareNavigateUpTaskStack(android.app.TaskStackBuilder)), but can can achieve the correct behavior simply by adding[`android:parentActivityName`](https://developer.android.com/guide/topics/manifest/activity-element#parent)to each[`<activity>`](https://developer.android.com/guide/topics/manifest/activity-element)element.

## Multimedia

### Media codecs

The[MediaCodec](https://developer.android.com/reference/android/media/MediaCodec)class provides access to low-level media codecs for encoding and decoding your media. You can instantiate a[MediaCodec](https://developer.android.com/reference/android/media/MediaCodec)by calling[createEncoderByType()](https://developer.android.com/reference/android/media/MediaCodec#createEncoderByType(java.lang.String))to encode media or call[createDecoderByType()](https://developer.android.com/reference/android/media/MediaCodec#createDecoderByType(java.lang.String))to decode media. Each of these methods take a MIME type for the type of media you want to encode or decode, such as`"video/3gpp"`or`"audio/vorbis"`.

With an instance of[MediaCodec](https://developer.android.com/reference/android/media/MediaCodec)created, you can then call[configure()](https://developer.android.com/reference/android/media/MediaCodec#configure(android.media.MediaFormat, android.view.Surface, android.media.MediaCrypto, int))to specify properties such as the media format or whether or not the content is encrypted.

Whether you're encoding or decoding your media, the rest of the process is the same after you create the[MediaCodec](https://developer.android.com/reference/android/media/MediaCodec). First call[getInputBuffers()](https://developer.android.com/reference/android/media/MediaCodec#getInputBuffers())to get an array of input[ByteBuffer](https://developer.android.com/reference/java/nio/ByteBuffer)objects and[getOutputBuffers()](https://developer.android.com/reference/android/media/MediaCodec#getOutputBuffers())to get an array of output[ByteBuffer](https://developer.android.com/reference/java/nio/ByteBuffer)objects.

When you're ready to encode or decode, call[dequeueInputBuffer()](https://developer.android.com/reference/android/media/MediaCodec#dequeueInputBuffer(long))to get the index position of the[ByteBuffer](https://developer.android.com/reference/java/nio/ByteBuffer)(from the array of input buffers) that you should use to to feed in your source media. After you fill the[ByteBuffer](https://developer.android.com/reference/java/nio/ByteBuffer)with your source media, release ownership of the buffer by calling[queueInputBuffer()](https://developer.android.com/reference/android/media/MediaCodec#queueInputBuffer(int, int, int, long, int)).

Likewise for the output buffer, call[dequeueOutputBuffer()](https://developer.android.com/reference/android/media/MediaCodec#dequeueOutputBuffer(android.media.MediaCodec.BufferInfo, long))to get the index position of the[ByteBuffer](https://developer.android.com/reference/java/nio/ByteBuffer)where you'll receive the results. After you read the output from the[ByteBuffer](https://developer.android.com/reference/java/nio/ByteBuffer), release ownership by calling[releaseOutputBuffer()](https://developer.android.com/reference/android/media/MediaCodec#releaseOutputBuffer(int, boolean)).

You can handle encrypted media data in the codecs by calling[queueSecureInputBuffer()](https://developer.android.com/reference/android/media/MediaCodec#queueSecureInputBuffer(int, int, android.media.MediaCodec.CryptoInfo, long, int))in conjunction with the[MediaCrypto](https://developer.android.com/reference/android/media/MediaCrypto)APIs, instead of the normal[queueInputBuffer()](https://developer.android.com/reference/android/media/MediaCodec#queueInputBuffer(int, int, int, long, int)).

For more information about how to use codecs, see the[MediaCodec](https://developer.android.com/reference/android/media/MediaCodec)documentation.

### Record audio on cue

New method[startRecording()](https://developer.android.com/reference/android/media/AudioRecord#startRecording())allows you to begin audio recording based on a cue defined by a[MediaSyncEvent](https://developer.android.com/reference/android/media/MediaSyncEvent). The[MediaSyncEvent](https://developer.android.com/reference/android/media/MediaSyncEvent)specifies an audio session (such as one defined by[MediaPlayer](https://developer.android.com/reference/android/media/MediaPlayer)), which when complete, triggers the audio recorder to begin recording. For example, you can use this functionality to play an audio tone that indicates the beginning of a recording session and recording automatically begins so you don't have to manually synchronize the tone and the beginning of recording.

### Timed text tracks

The[MediaPlayer](https://developer.android.com/reference/android/media/MediaPlayer)now handles both in-band and out-of-band text tracks. In-band text tracks come as a text track within an MP4 or 3GPP media source. Out-of-band text tracks can be added as an external text source via[addTimedTextSource()](https://developer.android.com/reference/android/media/MediaPlayer#addTimedTextSource(android.content.Context, android.net.Uri, java.lang.String))method. After all external text track sources are added,[getTrackInfo()](https://developer.android.com/reference/android/media/MediaPlayer#getTrackInfo())should be called to get the refreshed list of all available tracks in a data source.

To set the track to use with the[MediaPlayer](https://developer.android.com/reference/android/media/MediaPlayer), you must call[selectTrack()](https://developer.android.com/reference/android/media/MediaPlayer#selectTrack(int)), using the index position for the track you want to use.

To be notified when the text track is ready to play, implement the[MediaPlayer.OnTimedTextListener](https://developer.android.com/reference/android/media/MediaPlayer.OnTimedTextListener)interface and pass it to[setOnTimedTextListener()](https://developer.android.com/reference/android/media/MediaPlayer#setOnTimedTextListener(android.media.MediaPlayer.OnTimedTextListener)).

### Audio effects

The[AudioEffect](https://developer.android.com/reference/android/media/audiofx/AudioEffect)class now supports additional audio pre-processing types when capturing audio:

- Acoustic Echo Canceler (AEC) with[AcousticEchoCanceler](https://developer.android.com/reference/android/media/audiofx/AcousticEchoCanceler)removes the contribution of the signal received from the remote party from the captured audio signal.
- Automatic Gain Control (AGC) with[AutomaticGainControl](https://developer.android.com/reference/android/media/audiofx/AutomaticGainControl)automatically normalizes the output of the captured signal.
- Noise Suppressor (NS) with[NoiseSuppressor](https://developer.android.com/reference/android/media/audiofx/NoiseSuppressor)removes background noise from the captured signal.

You can apply these pre-processor effects on audio captured with an[AudioRecord](https://developer.android.com/reference/android/media/AudioRecord)using one of the[AudioEffect](https://developer.android.com/reference/android/media/audiofx/AudioEffect)subclasses.

**Note:** It's not guaranteed that all devices support these effects, so you should always first check availability by calling[isAvailable()](https://developer.android.com/reference/android/media/audiofx/AcousticEchoCanceler#isAvailable())on the corresponding audio effect class.

### Gapless playback

You can now perform gapless playback between two separate[MediaPlayer](https://developer.android.com/reference/android/media/MediaPlayer)objects. At any time before your first[MediaPlayer](https://developer.android.com/reference/android/media/MediaPlayer)finishes, call[setNextMediaPlayer()](https://developer.android.com/reference/android/media/MediaPlayer#setNextMediaPlayer(android.media.MediaPlayer))and Android attempts to start the second player the moment that the first one stops.
Media router. The new APIs MediaRouter, MediaRouteActionProvider, and MediaRouteButton provide standard mechanisms and UI for choosing where to play media.

## Camera

### Auto focus movement

The new interface[Camera.AutoFocusMoveCallback](https://developer.android.com/reference/android/hardware/Camera.AutoFocusMoveCallback)allows you to listen for changes to the auto focus movement. You can register your interface with[setAutoFocusMoveCallback()](https://developer.android.com/reference/android/hardware/Camera#setAutoFocusMoveCallback(android.hardware.Camera.AutoFocusMoveCallback)). Then when the camera is in a continuous autofocus mode ([FOCUS_MODE_CONTINUOUS_VIDEO](https://developer.android.com/reference/android/hardware/Camera.Parameters#FOCUS_MODE_CONTINUOUS_VIDEO)or[FOCUS_MODE_CONTINUOUS_PICTURE](https://developer.android.com/reference/android/hardware/Camera.Parameters#FOCUS_MODE_CONTINUOUS_PICTURE)), you'll receive a call to[onAutoFocusMoving()](https://developer.android.com/reference/android/hardware/Camera.AutoFocusMoveCallback#onAutoFocusMoving(boolean, android.hardware.Camera)), which tells you whether auto focus has started moving or has stopped moving.

### Camera sounds

The[MediaActionSound](https://developer.android.com/reference/android/media/MediaActionSound)class provides a simple set of APIs to produce standard sounds made by the camera or other media actions. You should use these APIs to play the appropriate sound when building a custom still or video camera.

To play a sound, simply instantiate a[MediaActionSound](https://developer.android.com/reference/android/media/MediaActionSound)object, call[load()](https://developer.android.com/reference/android/media/MediaActionSound#load(int))to pre-load the desired sound, then at the appropriate time, call[play()](https://developer.android.com/reference/android/media/MediaActionSound#play(int)).

## Connectivity

### Android Beam

Android Beam™ now supports large payload transfers over Bluetooth. When you define the data to transfer with either the new[setBeamPushUris()](https://developer.android.com/reference/android/nfc/NfcAdapter#setBeamPushUris(android.net.Uri[], android.app.Activity))method or the new callback interface[NfcAdapter.CreateBeamUrisCallback](https://developer.android.com/reference/android/nfc/NfcAdapter.CreateBeamUrisCallback), Android hands off the data transfer to Bluetooth or another alternate transport to achieve faster transfer speeds. This is especially useful for large payloads such as image and audio files and requires no visible pairing between the devices. No additional work is required by your app to take advantage of transfers over Bluetooth.

The[setBeamPushUris()](https://developer.android.com/reference/android/nfc/NfcAdapter#setBeamPushUris(android.net.Uri[], android.app.Activity))method takes an array of[Uri](https://developer.android.com/reference/android/net/Uri)objects that specify the data you want to transfer from your app. Alternatively, you can implement the[NfcAdapter.CreateBeamUrisCallback](https://developer.android.com/reference/android/nfc/NfcAdapter.CreateBeamUrisCallback)interface, which you can specify for your activity by calling[setBeamPushUrisCallback()](https://developer.android.com/reference/android/nfc/NfcAdapter#setBeamPushUrisCallback(android.nfc.NfcAdapter.CreateBeamUrisCallback, android.app.Activity)).

When using the callback interface, the system calls the interface's[createBeamUris()](https://developer.android.com/reference/android/nfc/NfcAdapter.CreateBeamUrisCallback#createBeamUris(android.nfc.NfcEvent))method when the user executes a share with Android Beam so that you can define the URIs to share at share-time. This is useful if the URIs to share might vary depending on the user context within the activity, whereas calling[setBeamPushUris()](https://developer.android.com/reference/android/nfc/NfcAdapter#setBeamPushUris(android.net.Uri[], android.app.Activity))is useful when the URIs to share are unchanging and you can safely define them ahead of time.

### Network service discovery

Android 4.1 adds support for multicast DNS-based service discovery, which allows you to find and connect to services offered by peer devices over Wi-Fi, such as mobile devices, printers, cameras, media players, and others that are registered on the local network.

The new package[android.net.nsd](https://developer.android.com/reference/android/net/nsd/package-summary)contains the new APIs that allow you to broadcast your services on the local network, discover local devices on the network, and connect to devices.

To register your service, you must first create an[NsdServiceInfo](https://developer.android.com/reference/android/net/nsd/NsdServiceInfo)object and define the various properties of your service with methods such as[setServiceName()](https://developer.android.com/reference/android/net/nsd/NsdServiceInfo#setServiceName(java.lang.String)),[setServiceType()](https://developer.android.com/reference/android/net/nsd/NsdServiceInfo#setServiceType(java.lang.String)), and[setPort()](https://developer.android.com/reference/android/net/nsd/NsdServiceInfo#setPort(int)).

Then you need to implement[NsdManager.RegistrationListener](https://developer.android.com/reference/android/net/nsd/NsdManager.RegistrationListener)and pass it to[registerService()](https://developer.android.com/reference/android/net/nsd/NsdManager#registerService(android.net.nsd.NsdServiceInfo, int, android.net.nsd.NsdManager.RegistrationListener))with your[NsdServiceInfo](https://developer.android.com/reference/android/net/nsd/NsdServiceInfo).

To discover services on the network, implement[NsdManager.DiscoveryListener](https://developer.android.com/reference/android/net/nsd/NsdManager.DiscoveryListener)and pass it to[discoverServices()](https://developer.android.com/reference/android/net/nsd/NsdManager#discoverServices(java.lang.String, int, android.net.nsd.NsdManager.DiscoveryListener)).

When your[NsdManager.DiscoveryListener](https://developer.android.com/reference/android/net/nsd/NsdManager.DiscoveryListener)receives callbacks about services found, you need to resolve the service by calling[resolveService()](https://developer.android.com/reference/android/net/nsd/NsdManager#resolveService(android.net.nsd.NsdServiceInfo, android.net.nsd.NsdManager.ResolveListener)), passing it an implementation of[NsdManager.ResolveListener](https://developer.android.com/reference/android/net/nsd/NsdManager.ResolveListener)that receives an[NsdServiceInfo](https://developer.android.com/reference/android/net/nsd/NsdServiceInfo)object that contains information about the discovered service, allowing you to initiate the connection.

### Wi-Fi P2P service discovery

The Wi-Fi P2P APIs are enhanced in Android 4.1 to support pre-association service discovery in the[WifiP2pManager](https://developer.android.com/reference/android/net/wifi/p2p/WifiP2pManager). This allows you to discover and filter nearby devices by services using Wi-Fi P2P before connecting to one, while Network Service Discovery allows you to discover a service on an existing connected network (such as a local Wi-Fi network).

To broadcast your app as a service over Wi-Fi so that other devices can discover your app and connect to it, call[addLocalService()](https://developer.android.com/reference/android/net/wifi/p2p/WifiP2pManager#addLocalService(android.net.wifi.p2p.WifiP2pManager.Channel, android.net.wifi.p2p.nsd.WifiP2pServiceInfo, android.net.wifi.p2p.WifiP2pManager.ActionListener))with a[WifiP2pServiceInfo](https://developer.android.com/reference/android/net/wifi/p2p/nsd/WifiP2pServiceInfo)object that describes your app services.

To initiate discover of nearby devices over Wi-Fi, you need to first decide whether you'll communicate using Bonjour or Upnp. To use Bonjour, first set up some callback listeners with[setDnsSdResponseListeners()](https://developer.android.com/reference/android/net/wifi/p2p/WifiP2pManager#setDnsSdResponseListeners(android.net.wifi.p2p.WifiP2pManager.Channel, android.net.wifi.p2p.WifiP2pManager.DnsSdServiceResponseListener, android.net.wifi.p2p.WifiP2pManager.DnsSdTxtRecordListener)), which takes both a[WifiP2pManager.DnsSdServiceResponseListener](https://developer.android.com/reference/android/net/wifi/p2p/WifiP2pManager.DnsSdServiceResponseListener)and[WifiP2pManager.DnsSdTxtRecordListener](https://developer.android.com/reference/android/net/wifi/p2p/WifiP2pManager.DnsSdTxtRecordListener). To use Upnp, call[setUpnpServiceResponseListener()](https://developer.android.com/reference/android/net/wifi/p2p/WifiP2pManager#setUpnpServiceResponseListener(android.net.wifi.p2p.WifiP2pManager.Channel, android.net.wifi.p2p.WifiP2pManager.UpnpServiceResponseListener)), which takes a[WifiP2pManager.UpnpServiceResponseListener](https://developer.android.com/reference/android/net/wifi/p2p/WifiP2pManager.UpnpServiceResponseListener).

Before you can start discovering services on local devices, you also need to call[addServiceRequest()](https://developer.android.com/reference/android/net/wifi/p2p/WifiP2pManager#addServiceRequest(android.net.wifi.p2p.WifiP2pManager.Channel, android.net.wifi.p2p.nsd.WifiP2pServiceRequest, android.net.wifi.p2p.WifiP2pManager.ActionListener)). When the[WifiP2pManager.ActionListener](https://developer.android.com/reference/android/net/wifi/p2p/WifiP2pManager.ActionListener)you pass to this method receives a successful callback, you can then begin discovering services on local devices by calling[discoverServices()](https://developer.android.com/reference/android/net/wifi/p2p/WifiP2pManager#discoverServices(android.net.wifi.p2p.WifiP2pManager.Channel, android.net.wifi.p2p.WifiP2pManager.ActionListener)).

When local services are discovered, you'll receive a callback to either the[WifiP2pManager.DnsSdServiceResponseListener](https://developer.android.com/reference/android/net/wifi/p2p/WifiP2pManager.DnsSdServiceResponseListener)or[WifiP2pManager.UpnpServiceResponseListener](https://developer.android.com/reference/android/net/wifi/p2p/WifiP2pManager.UpnpServiceResponseListener), depending on whether you registered to use Bonjour or Upnp. The callback received in either case contains a[WifiP2pDevice](https://developer.android.com/reference/android/net/wifi/p2p/WifiP2pDevice)object representing the peer device.

### Network usage

The new method[isActiveNetworkMetered()](https://developer.android.com/reference/android/net/ConnectivityManager#isActiveNetworkMetered())allows you to check whether the device is currently connected to a metered network. By checking this state before performing intensive network transactions, you can help manage the data usage that may cost your users money and make informed decisions about whether to perform the transactions now or later (such as when the device becomes connected to Wi-Fi).

## Accessibility

### Accessibility service APIs

The reach of accessibility service APIs has been significantly increased in Android 4.1. It now allows you to build services that monitor and respond to more input events, such as complex gestures using[onGesture()](https://developer.android.com/reference/android/accessibilityservice/AccessibilityService#onGesture(int))and other input events through additions to the[AccessibilityEvent](https://developer.android.com/reference/android/view/accessibility/AccessibilityEvent),[AccessibilityNodeInfo](https://developer.android.com/reference/android/view/accessibility/AccessibilityNodeInfo)and[AccessibilityRecord](https://developer.android.com/reference/android/view/accessibility/AccessibilityRecord)classes.

Accessibility services can also perform actions on behalf of the user, including clicking, scrolling and stepping through text using[performAction](https://developer.android.com/reference/android/view/accessibility/AccessibilityNodeInfo#performAction(int))and[setMovementGranularities](https://developer.android.com/reference/android/view/accessibility/AccessibilityNodeInfo#setMovementGranularities(int)). The[performGlobalAction()](https://developer.android.com/reference/android/accessibilityservice/AccessibilityService#performGlobalAction(int))method also allows services to perform actions such as Back, Home, and open Recent Apps and Notifications.

### Customizable app navigation

When building an Android app, you can now customize navigation schemes by finding focusable elements and input widgets using[findFocus()](https://developer.android.com/reference/android/view/accessibility/AccessibilityNodeInfo#findFocus(int))and[focusSearch()](https://developer.android.com/reference/android/view/accessibility/AccessibilityNodeInfo#focusSearch(int)), and set focus using[setAccessibilityFocused()](https://developer.android.com/reference/android/view/accessibility/AccessibilityNodeInfo#setAccessibilityFocused(boolean)).

### More accessible widgets

The new`android.view.accessibility.AccessibilityNodeProvider`class allows you to surface complex custom views to accessibility services so they can present the information in a more accessible way. The`android.view.accessibility.AccessibilityNodeProvider`allows a user widget with advanced content, such as a calendar grid, to present a logical semantic structure for accessibility services that is completely separate from the widget's layout structure. This semantic structure allows accessibility services to present a more useful interaction model for users who are visually impaired.

## Copy and Paste

### Copy and paste with intents

You can now associate a[ClipData](https://developer.android.com/reference/android/content/ClipData)object with an[Intent](https://developer.android.com/reference/android/content/Intent)using the[setClipData()](https://developer.android.com/reference/android/content/Intent#setClipData(android.content.ClipData))method. This is especially useful when using an intent to transfer multiple`content:`URIs to another application, such as when sharing multiple documents. The`content:`URIs supplied this way will also respect the intent's flags to offer read or write access, allowing you to grant access to multiple URIs in an the intent. When starting an[ACTION_SEND](https://developer.android.com/reference/android/content/Intent#ACTION_SEND)or[ACTION_SEND_MULTIPLE](https://developer.android.com/reference/android/content/Intent#ACTION_SEND_MULTIPLE)intent, the URIs supplied in the intent are now automatically propagated to the[ClipData](https://developer.android.com/reference/android/content/ClipData)so that the receiver can have access granted to them.

##### Support for HTML and string styles

The[ClipData](https://developer.android.com/reference/android/content/ClipData)class now supports styled text (either as HTML or Android[styled strings](https://developer.android.com/guide/topics/resources/string-resource#FormattingAndStyling)). You can add HTML styled text to the[ClipData](https://developer.android.com/reference/android/content/ClipData)with[newHtmlText()](https://developer.android.com/reference/android/content/ClipData#newHtmlText(java.lang.CharSequence, java.lang.CharSequence, java.lang.String)).

## Renderscript

Renderscript computation functionality has been enhanced with the following features:

- Support for multiple kernels within one script.
- Support for reading from allocation with filtered samplers from compute in a new script API`rsSample`.
- Support for different levels of FP precision in`#pragma`.
- Support for querying additional information from RS objects from a compute script.
- Numerous performance improvements.

New pragmas are also available to define the floating point precision required by your compute Renderscripts. This lets you enable NEON like operations such as fast vector math operations on the CPU path that wouldn't otherwise be possible with full IEEE 754-2008 standard.

**Note:**The experimental Renderscript graphics engine is now deprecated.

## Animation

### Activity launch animations

You can now launch an[Activity](https://developer.android.com/reference/android/app/Activity)using zoom animations or your own custom animations. To specify the animation you want, use the[ActivityOptions](https://developer.android.com/reference/android/app/ActivityOptions)APIs to build a[Bundle](https://developer.android.com/reference/android/os/Bundle)that you can then pass to any of the methods that start an activity, such as[startActivity()](https://developer.android.com/reference/android/app/Activity#startActivity(android.content.Intent, android.os.Bundle)).

The[ActivityOptions](https://developer.android.com/reference/android/app/ActivityOptions)class includes a different method for each type of animation you may want to show as your activity opens:

[makeScaleUpAnimation()](https://developer.android.com/reference/android/app/ActivityOptions#makeScaleUpAnimation(android.view.View, int, int, int, int))
:   Creates an animation that scales up the activity window from a specified starting position on the screen and a specified starting size. For example, the home screen in Android 4.1 uses this when opening an app.

[makeThumbnailScaleUpAnimation()](https://developer.android.com/reference/android/app/ActivityOptions#makeThumbnailScaleUpAnimation(android.view.View, android.graphics.Bitmap, int, int))
:   Creates an animation that scales up the activity window starting from a specified position and a provided thumbnail image. For example, the Recent Apps window in Android 4.1 uses this when returning to an app.

[makeCustomAnimation()](https://developer.android.com/reference/android/app/ActivityOptions#makeCustomAnimation(android.content.Context, int, int))
:   Creates an animation defined by your own resources: one that defines the animation for the activity opening and another for the activity being stopped.

### Time animator

The new[TimeAnimator](https://developer.android.com/reference/android/animation/TimeAnimator)provides a simple callback mechanism with the[TimeAnimator.TimeListener](https://developer.android.com/reference/android/animation/TimeAnimator.TimeListener)that notifies you upon every frame of the animation. There is no duration, interpolation, or object value-setting with this Animator. The listener's callback receives information for each frame including total elapsed time and the elapsed time since the previous animation frame.

## User Interface

### Notifications

In Android 4.1, you can create notifications with larger content regions, big image previews, multiple action buttons, and configurable priority.

##### Notification styles

The new method[setStyle()](https://developer.android.com/reference/android/app/Notification.Builder#setStyle(android.app.Notification.Style))allows you to specify one of three new styles for your notification that each offer a larger content region. To specify the style for your large content region, pass[setStyle()](https://developer.android.com/reference/android/app/Notification.Builder#setStyle(android.app.Notification.Style))one of the following objects:

[Notification.BigPictureStyle](https://developer.android.com/reference/android/app/Notification.BigPictureStyle)
:   For notifications that includes a large image attachment.

[Notification.BigTextStyle](https://developer.android.com/reference/android/app/Notification.BigTextStyle)
:   For notifications that includes a lot of text, such as a single email.

[Notification.InboxStyle](https://developer.android.com/reference/android/app/Notification.InboxStyle)
:   For notifications that include a list of strings, such as snippets from multiple emails.

##### Notification actions

There's now support for up to two action buttons that appear at the bottom of the notification message, whether your notification uses the normal or larger style.

To add an action button, call[addAction()](https://developer.android.com/reference/android/app/Notification.Builder#addAction(android.app.Notification.Action)). This method takes three arguments: a drawable resource for an icon, text for the button, and a[PendingIntent](https://developer.android.com/reference/android/app/PendingIntent)that defines the action to perfrom.

##### Priorities

You can now hint to the system how important your notification is to affect the order of your notification in the list by setting the priority with[setPriority()](https://developer.android.com/reference/android/app/Notification.Builder#setPriority(int)). You can pass this one of five different priority levels defined by`PRIORITY_*`constants in the[Notification](https://developer.android.com/reference/android/app/Notification)class. The default is[PRIORITY_DEFAULT](https://developer.android.com/reference/android/app/Notification#PRIORITY_DEFAULT), and there's two levels higher and two levels lower.

High priority notifications are things that users generally want to respond to quickly, such as a new instant message, text message, or impending event reminder. Low priority notifications are things like expired calendar events or app promotions.

### Controls for system UI

Android 4.0 (Ice Cream Sandwich) added new flags to control the visibility of the system UI elements, such as to dim the appearance of the system bar or make it disappear completely on handsets. Android 4.1 adds a few more flags that allow you to further control the appearance of system UI elements and your activity layout in relation to them by calling[setSystemUiVisibility()](https://developer.android.com/reference/android/view/View#setSystemUiVisibility(int))and passing the following flags:

[SYSTEM_UI_FLAG_FULLSCREEN](https://developer.android.com/reference/android/view/View#SYSTEM_UI_FLAG_FULLSCREEN)
:   Hides non-critical system UI (such as the status bar). If your activity uses the action bar in overlay mode (by enabling[`android:windowActionBarOverlay`](https://developer.android.com/reference/android/R.attr#windowActionBarOverlay)), then this flag also hides the action bar and does so with a coordinated animation when both hiding and showing the two.

[SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN](https://developer.android.com/reference/android/view/View#SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
:   Sets your activity layout to use the same screen area that's available when you've enabled[SYSTEM_UI_FLAG_FULLSCREEN](https://developer.android.com/reference/android/view/View#SYSTEM_UI_FLAG_FULLSCREEN)even if the system UI elements are still visible. Although parts of your layout will be overlaid by the system UI, this is useful if your app often hides and shows the system UI with[SYSTEM_UI_FLAG_FULLSCREEN](https://developer.android.com/reference/android/view/View#SYSTEM_UI_FLAG_FULLSCREEN), because it avoids your layout from adjusting to the new layout bounds each time the system UI hides or appears.

[SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION](https://developer.android.com/reference/android/view/View#SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION)
:   Sets your activity layout to use the same screen area that's available when you've enabled[SYSTEM_UI_FLAG_HIDE_NAVIGATION](https://developer.android.com/reference/android/view/View#SYSTEM_UI_FLAG_HIDE_NAVIGATION)(added in Android 4.0) even if the system UI elements are still visible. Although parts of your layout will be overlaid by the navigation bar, this is useful if your app often hides and shows the navigation bar with[SYSTEM_UI_FLAG_HIDE_NAVIGATION](https://developer.android.com/reference/android/view/View#SYSTEM_UI_FLAG_HIDE_NAVIGATION), because it avoids your layout from adjusting to the new layout bounds each time the navigation bar hides or appears.

[SYSTEM_UI_FLAG_LAYOUT_STABLE](https://developer.android.com/reference/android/view/View#SYSTEM_UI_FLAG_LAYOUT_STABLE)
:   You might want to add this flag if you're using[SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN](https://developer.android.com/reference/android/view/View#SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)and/or[SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION](https://developer.android.com/reference/android/view/View#SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION)to ensure that when you call[fitSystemWindows()](https://developer.android.com/reference/android/view/View#fitSystemWindows(android.graphics.Rect))on a view that the bounds defined remain consistent with regard to the available screen space. That is, with this flag set,[fitSystemWindows()](https://developer.android.com/reference/android/view/View#fitSystemWindows(android.graphics.Rect))will behave as if the visibility of system UI elements is unchanged even after you hide all system UI.
:

For more discussion about the other related system UI flags, read about those added in[Android 4.0](https://developer.android.com/about/versions/android-4.0#SystemUI).

### Remote views

[GridLayout](https://developer.android.com/reference/android/widget/GridLayout)and[ViewStub](https://developer.android.com/reference/android/view/ViewStub)are now remotable views so you can use them in layouts for your app widgets and notification custom layouts.

### Font families

Android 4.1 adds several more variants of the Roboto font style for a total of 10 variants, and they're all usable by apps. Your apps now have access to the full set of both light and condensed variants.

The complete set of Roboto font variants available is:

- Regular
- Italic
- Bold
- Bold-italic
- Light
- Light-italic
- Condensed regular
- Condensed italic
- Condensed bold
- Condensed bold-italic

You can apply any one of these with the new[fontFamily](https://developer.android.com/reference/android/R.attr#fontFamily)attribute in combination with the[textStyle](https://developer.android.com/reference/android/R.attr#textStyle)attribute.

Supported values for[fontFamily](https://developer.android.com/reference/android/R.attr#fontFamily)are:

- `"sans-serif"`for regular Roboto
- `"sans-serif-light"`for Roboto Light
- `"sans-serif-condensed"`for Roboto Condensed

You can then apply bold and/or italic with[textStyle](https://developer.android.com/reference/android/R.attr#textStyle)values`"bold"`and`"italic"`. You can apply both like so:`android:textStyle="bold|italic"`.

You can also use[Typeface.create()](https://developer.android.com/reference/android/graphics/Typeface#create(android.graphics.Typeface, int)). For example,`Typeface.create("sans-serif-light", Typeface.NORMAL)`.

## Input Framework

### Multiple input devices

The new[InputManager](https://developer.android.com/reference/android/hardware/input/InputManager)class allows you to query the set of input devices current connected and register to be notified when a new device is added, changed, or removed. This is particularly useful if you're building a game that supports multiple players and you want to detect how many controllers are connected and when there are changes to the number of controllers.

You can query all input devices connected by calling[getInputDeviceIds()](https://developer.android.com/reference/android/hardware/input/InputManager#getInputDeviceIds()). This returns an array of integers, each of which is an ID for a different input device. You can then call[getInputDevice()](https://developer.android.com/reference/android/hardware/input/InputManager#getInputDevice(int))to acquire an[InputDevice](https://developer.android.com/reference/android/view/InputDevice)for a specified input device ID.

If you want to be informed when new input devices are connected, changed, or disconnected, implement the[InputManager.InputDeviceListener](https://developer.android.com/reference/android/hardware/input/InputManager.InputDeviceListener)interface and register it with[registerInputDeviceListener()](https://developer.android.com/reference/android/hardware/input/InputManager#registerInputDeviceListener(android.hardware.input.InputManager.InputDeviceListener, android.os.Handler)).

### Vibrate for input controllers

If connected input devices have their own vibrate capabilities, you can now control the vibration of those devices using the existing[Vibrator](https://developer.android.com/reference/android/os/Vibrator)APIs simply by calling[getVibrator()](https://developer.android.com/reference/android/view/InputDevice#getVibrator())on the[InputDevice](https://developer.android.com/reference/android/view/InputDevice).

## Permissions

The following are new permissions:

[READ_EXTERNAL_STORAGE](https://developer.android.com/reference/android/Manifest.permission#READ_EXTERNAL_STORAGE)
:   Provides protected read access to external storage. In Android 4.1 by default all applications still have read access. This will be changed in a future release to require that applications explicitly request read access using this permission. If your application already requests write access, it will automatically get read access as well. There is a new developer option to turn on read access restriction, for developers to test their applications against how Android will behave in the future.

android.Manifest.permission.READ_USER_DICTIONARY
:   Allows an application to read the user dictionary. This should only be required by an IME, or a dictionary editor like the Settings app.

[READ_CALL_LOG](https://developer.android.com/reference/android/Manifest.permission#READ_CALL_LOG)
:   Allows an application to read the system's call log that contains information about incoming and outgoing calls.

[WRITE_CALL_LOG](https://developer.android.com/reference/android/Manifest.permission#WRITE_CALL_LOG)
:   Allows an application to modify the system's call log stored on your phone

android.Manifest.permission.WRITE_USER_DICTIONARY
:   Allows an application to write to the user's word dictionary.

## Device Features

Android 4.1 includes a new feature declaration for devices that are dedicated to displaying the user interface on a television screen:[FEATURE_TELEVISION](https://developer.android.com/reference/android/content/pm/PackageManager#FEATURE_TELEVISION). To declare that your app requires a television interface, declare this feature in your manifest file with the[`<uses-feature>`](https://developer.android.com/guide/topics/manifest/uses-feature-element)element:  

```carbon
<manifest ... >
    <uses-feature android:name="android.hardware.type.television"
                  android:required="true" />
    ...
</manifest>
```

This feature defines "television" to be a typical living room television experience: displayed on a big screen, where the user is sitting far away and the dominant form of input is be something like a d-pad, and generally not through touch or a mouse/pointer-device.