Menu
Slideshow

In Pictures: 12 great Lollipop APIs every Android 5.0 developer will love

From 3D views to managed provisioning processes, there’s a lot for Android app developers to love about Lollipop

  • 12 great Lollipop APIs every Android 5.0 developer will love Android 5.0 Lollipop is best known for the Material Design look and feel, and it includes an expanded UI toolkit that developers can use to add the illusion of 3D with real-time shadows, smooth animated activity transitions, and scalable vector “drawables.” Android 5 introduces more APIs than developers will likely ever get their heads around; we’ve chosen a dozen of them to highlight for you.

  • 3D views and real-time shadows Android 5 introduces an elevation property to give your app added dimension. As shown above, you can set the new elevation property for graphical objects in an Android 5 application using the android:elevation property. This will automatically enable real-time shadows beneath the object to create the illusion that it’s floating above the background. Another way to change elevation is with the new setTranslationZ method of views, for example view.setTranslationZ(120); you can animate elevation changes with the new ViewPropertyAnimator.z() and ViewPropertyAnimator.translationZ() methods. These added touches will help your app stand out, almost literally.

  • Activity transitions Transitions have been punched up with Android 5, thanks to activity transitions. With this new API, you can animate transitions between activities by finding their view IDs, creating an Intent and a Bundle of views to define a scene transition animation, and passing them to startActivity(), as shown above. The Lollipop system takes care of rendering the actual transition animation. Snazzy.

  • Heads-up notifications Android 5 takes notifications to the next level, including the ability to create “heads-up notifications,” floating alerts that can be acted on or swiped away without leaving the current, full-screen app. The figure above shows a Lollipop heads-up notification for an incoming phone call, displayed over a text view (of a passage from "Alice in Wonderland"). The following code snippet shows how a heads-up notification is set up: /** * Creates a new notification depending on the argument. * * @param makeHeadsUpNotification A boolean value to indicating whether a notification will be * created as a heads-up notification or not. *

      *
    • true : Creates a heads-up notification.
    • *
    • false : Creates a non-heads-up notification.
    • *
    * * @return A Notification instance. */ //@VisibleForTesting Notification createNotification(boolean makeHeadsUpNotification) { Notification.Builder notificationBuilder = new Notification.Builder(getActivity()) .setSmallIcon(R.drawable.ic_launcher_notification) .setPriority(Notification.PRIORITY_DEFAULT) .setCategory(Notification.CATEGORY_MESSAGE) .setContentTitle("Sample Notification") .setContentText("This is a normal notification."); if (makeHeadsUpNotification) { Intent push = new Intent(); push.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); push.setClass(getActivity(), LNotificationActivity.class); PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(getActivity(), 0, push, PendingIntent.FLAG_CANCEL_CURRENT); notificationBuilder .setContentText("Heads-Up Notification on Android L or above.") .setFullScreenIntent(fullScreenPendingIntent, true); } return notificationBuilder.build(); } } Lollipop also allows more control over lock-screen notifications, for example to mark certain information as private. That’s set with values of NotificationVisibility, such as NotificationVisibility.SECRET.
  • Leanback UI and Android TV Input Frameworks Android 5 also introduces Leanback, the new Lollipop UI for TV screens. Developers looking to capitalize on growing interest in Android-based TVs will welcome this addition. The code snippet above shows how to include a LEANBACK_LAUNCHER intent filter in your Android 5 application manifest to ensure your app operates under Leanback when opened on an Android TV. You don’t have to use the Leanback library or any of the other support libraries and frameworks (such as the Android TV Input Framework, aka TIF, which allows TV apps to handle video streams from sources such as HDMI inputs, TV tuners, and IPTV receivers) that have been added for big screens in Lollipop, but it streamlines your code and helps to make your UIs consistent with the guidelines.

  • Concurrent documents The overview screen, formerly called the “recents” screen, is a system-level UI that lists recently accessed activities and tasks. In Lollipop, multiple instances of the same activity containing different documents may appear as tasks in the overview screen, thereby allowing users easy access to more of what your app offers. While you should normally allow the system to define how your tasks and activities are represented in the overview screen, your app can use the flags of the Intent class to specify when an activity is added or removed from the overview screen.

  • Bluetooth Low Energy scanning and advertising Lollipop adds new APIs that allow apps to perform concurrent operations with BLE (Bluetooth Low Energy), allowing both scanning (central mode) and advertising (peripheral mode). BLE is gaining widespread interest for health and fitness applications, as it is aimed at greatly reducing the power usage of Bluetooth, while delivering similar performance and functionality. As shown above, your app can start and stop its Bluetooth LE advertising activity. To find BLE devices, you use the BluetoothAdapter.startLeScan() method.

  • OpenGL ES 3.1 and Android Extension Pack OpenGL ES 3.1 and the Android Extension Pack offer Lollipop programmers high-performance 2D and 3D graphics APIs. Capabilities include compute shaders, stencil textures, accelerated visual effects, advanced texture rendering, tessellation shaders, geometry shaders, ASTC texture compression, and per-sample interpolation and shading. OpenGL APIs work across different brands of GPU; the ES designation refers to the OpenGL standard for embedded systems.

  • MediaBrowser and MediaSession APIs Lollipop allows apps to browse the media content libraries of other apps through the android.media.browse API. The content must first be exposed by the originating app via the MediaBrowserService class. The new API is all about apps integrating with each other, using the same UIs, and avoiding the need to duplicate existing services. In addition, the MediaSession class shown above replaces the deprecated RemoteControlClient class and provides a single set of callback methods for handling transport controls and media buttons.

  • Low-latency audio input The Android multimedia framework includes support for capturing and encoding a variety of common audio formats, so you can easily integrate audio into your applications. You can record audio using the MediaRecorder APIs if supported by the device hardware. The code snippet above illustrates the use of the Media-Recorder class. The new audio capture design includes a fast capture thread that never blocks except during a read; fast track capture clients at the native sample rate, channel count, and bit depth; and normal capture clients that offer resampling, up/down channel mix, and up/down bit depth.

  • New camera APIs with RAW support Serious photographers love RAW image formats because they offer higher dynamic range, fewer artifacts, and more postprocessing flexibility than compressed JPEG formats. Up until now, RAW formats were not available from Android device cameras. The code above demonstrates how to capture an image from the Android camera device in JPEG format, but the new camera2 class, parent of the CameraDevice class seen in this snippet, also supports the RAW_SENSOR image format, and can write the received RAW pixel data in the standard Adobe DNG (Digital Negative) format.

  • Managed provisioning process for workplace security Android hasn’t had a very good out-of-the-box security story for work use compared to BlackBerry and iOS -- until now. A new managed provisioning process, using code like that shown above, creates a secure work profile on the device; in the launcher, apps are shown with a Work badge. An IT administrator takes care of work apps and their data inside of the work profile. You can go further and deploy and activate a device owner prior to provisioning, using an NFC data transfer from a programming app. A device owner app can use the methods in the DevicePolicyManager class to take fine-grain control of the configuration, security, and apps on a managed device.

  • Screen capturing and sharing Early versions of Android had no easy way to take screenshots; you had to install the SDK and use the debugger, and that was often problematic because the necessary device drivers were hard to locate. Later on, you could install a third-party screenshot app, but you had to root your device. More recently, most Android devices have supported at least one way to generate a screenshot using some UI; for example, the Moto X uses a simultaneous press of the down toggle and the power button. In Lollipop, apps can initiate screen captures themselves. As explained in the image above, first call Context.getSystemService() with the argument MEDIA_-PROJECTION_SERVICE to get a MediaProjectionManager class instance; second, call createScreenCaptureIntent () to get an Intent; third, pass the Intent to startActivityForResult() to start the screen capture; and finally, pass the result to getMediaProjection() to get the capture data.

Show Comments

Market Place

Computerworld
ARN
Techworld
CMO