Category Archives: Java

Android Lollipop 5.0 APIs – special feature


Some of the features of Lollipop 5.0 :

  1. Material design

  2. Concurrent documents and activities in the recents screen

  3. WebView updates

  4. Screen capturing and sharing

  5. Notifications

  6. Graphics

  7. Camera API for advanced camera capabilities

  8. Bluetooth Low Energy

  9. Scheduling jobs

  10. Developer tools for battery usage

  1. Material design :

Android 5.0 adds support for Android””s new material design style. You can create apps with material design that are visually dynamic and have UI element transitions that feel natural to users. This support includes:

  • The material theme
  • View shadows
  • The RecyclerView widget
  • Drawable animation and styling effects
  • Material design animation and activity transition effects
  • Animators for view properties based on the state of the view
  • Customizable UI widgets and app bars with color palettes that you control

Animated and non-animated drawables based on XML vector graphics

  1. Concurrent documents and activities in the recents screen :

In previous releases, the recents screen could only display only one task for each app that the user interacted with most recently. Now your app can open more tasks as needed for additional concurrent activities for documents. This feature facilitates multitasking by letting users quickly switch between individual activities and documents from the recents screen, with a consistent switching experience across all apps. Examples of such concurrent tasks might include open tabs in a web browser app, documents in a productivity app, concurrent matches in a game, or chats in a messaging app. Your app can manage its tasks through the ActivityManager.AppTask class.

3.WebView updates

Android 5.0 updates the WebView implementation to Chromium M37, bringing security and stability enhancements, as well as bug fixes. The default user-agent string for a WebView running on Android 5.0 has been updated to incorporate as the version number.

This release introduces the PermissionRequest class, which allows your app to grant the WebView permission to access protected resources like the camera and microphone, through web APIs such as getUserMedia(). Your app must have the appropriate Android permissions for these resources in order to grant the permissions to the WebView.

  1. Screen capturing and sharing

Android 5.0 lets you add screen capturing and screen sharing capabilities to your app with the new APIs. This functionality is useful, for example, if you want to enable screen sharing in a video conferencing app.
The new createVirtualDisplay() method allows your app to capture the contents of the main screen (the default display) into a Surface object, which your app can then send across the network. The API only allows capturing non-secure screen content, and not system audio. To begin screen capturing, your app must first request the user’s permission by launching a screen capture dialog using an Intent obtained through the createScreenCaptureIntent() method.

  1. Notifications

Lock screen notifications

Lock screens in Android 5.0 have the ability to present notifications. Users can choose via Settings whether to allow sensitive notification content to be shown over a secure lock screen.

Your app can control the level of detail visible when its notifications are displayed over the secure lock screen. To control the visibility level, call setVisibility() and specify one of these values:

  • VISIBILITY_PRIVATE: Shows basic information, such as the notification’s icon, but hides the notification’s full content.
  • VISIBILITY_PUBLIC: Shows the notification’s full content.
  • VISIBILITY_SECRET: Shows nothing, excluding even the notification’s icon.

When the visibility level is VISIBILITY_PRIVATE, you can also provide a redacted version of the notification content that hides personal details. For example, an SMS app might display a notification that shows “You have 3 new text messages” but hides the message content and senders. To provide this alternative notification, first create the replacement notification using Notification.Builder. When you create the private notification object, attach the replacement notification to it through the setPublicVersion() method.

Notifications metadata

Android 5.0 uses metadata associated with your app notifications to sort the notifications more intelligently. To set the metadata, call the following methods in Notification.Builder when you construct the notification:

  • setCategory(): Tells the system how to handle your app notifications when the device is in priority mode (for example, if a notification represents an incoming call, instant message, or alarm).
  • setPriority(): Marks the notification as more or less important than normal notifications. Notifications with the priority field set to PRIORITY_MAX or PRIORITY_HIGH appear in a small floating window if the notification also has sound or vibration.
  • addPerson(): Enables you to add one or more people who are relevant to a notification. Your app can use this to signal to the system that it should group together notifications from the specified people, or rank notifications from these people as being more important.
  1. Graphics

Support for OpenGL ES 3.1

Android 5.0 adds Java interfaces and native support for OpenGL ES 3.1. Key new functionality provided in OpenGL ES 3.1 includes:

  • Compute shaders
  • Separate shader objects
  • Indirect draw commands
  • Multisample and stencil textures
  • Shading language improvements
  • Extensions for advanced blend modes and debugging
  • Backward compatibility with OpenGL ES 2.0 and 3.0

The Java interface for OpenGL ES 3.1 on Android is provided with GLES31. When using OpenGL ES 3.1, be sure that you declare it in your manifest file with the <uses-feature> tag and the android:glEsVersion attribute. For example:

<manifest><uses-feature android:glEsVersion=”0x00030001″ />


  1. Camera API for advanced camera capabilities

Android 5.0 introduces the new android.hardware.camera2 API to facilitate fine-grain photo capture and image processing. You can now programmatically access the camera devices available to the system with getCameraIdList() and connect to a specific device with openCamera(). To start capturing images, create a CameraCaptureSession and specify the Surface objects to send captured images. The CameraCaptureSession can be configured to take single shots or multiple images in a burst.

  1. Bluetooth Low Energy :

Android 4.3 introduced platform support for Bluetooth Low Energy (Bluetooth LE) in the central role. In Android 5.0, an Android device can now act as a Bluetooth LE peripheral device. Apps can use this capability to make their presence known to nearby devices. For instance, you can build apps that allow a device to function as a pedometer or health monitor and communicate its data with another Bluetooth LE device.

The new android.bluetooth.le APIs enable your apps to broadcast advertisements, scan for responses, and form connections with nearby Bluetooth LE devices. To use the new advertising and scanning features, add the BLUETOOTH_ADMIN permission in your manifest. When users update or download your app from the Play Store, they are asked to grant the following permission to your app: “Bluetooth connection information: Allows the app to control Bluetooth, including broadcasting to or getting information about nearby Bluetooth devices.”

  1. Scheduling jobs :

In addition to new features, Android 5.0 emphasizes improvements in battery life. Use the new APIs and tool to understand and optimize your app’s power consumption.

Android 5.0 provides a new JobScheduler API that lets you optimize battery life by defining jobs for the system to run asynchronously at a later time or under specified conditions (such as when the device is charging). Job scheduling is useful in such situations as:

  • The app has non-user-facing work that you can defer.
  • The app has work you””d prefer to do when the unit is plugged in.
  • The app has a task that requires network access or a Wi-Fi connection.
  • The app has a number of tasks that you want to run as a batch on a regular schedule.

Use the JobInfo.Builder class to configure how the scheduled task should run. You can schedule the task to run under specific conditions, such as:

  • Start when the device is charging
  • Start when the device is connected to an unmetered network
  • Start when the device is idle
  • Finish before a certain deadline or with a minimum delay

For example, you can add code like this to run your task on an unmetered network:

JobInfo uploadTask = new JobInfo.Builder(mJobId,  mServiceComponent /* JobService component */).setRequiredNetworkCapabilities(JobInfo.NetworkType.UNMETERED)

JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);

If the device has stable power (that is, it has been plugged in for more than 2 minutes and the battery is at a healthy level), the system will run any scheduled job that is ready to run, even if the job’s deadline has not expired.

  1. Developer tools for battery usage :

The new dumpsys batterystats command generates interesting statistical data about battery usage on a device, organized by unique user ID (UID). The statistics include:


  • History of battery related events
  • Global statistics for the device
  • Approximate power use per UID and system component
  • Per-app mobile ms per packet
  • System UID aggregated statistics
  • App UID aggregated statistics


Use the –help option to learn about the various options for tailoring the output. For example, to print battery usage statistics for a given app package since the device was last charged, run this command:


$ adb shell dumpsys batterystats –charged <package-name>


Quick Look Up for Inheritance in Java

Quick Look Up for Inheritance in Java

 Encapsulation, IS-A, HAS-A

❑ The goal of encapsulation is to hide implementation behind an interface
(or API).

❑ Encapsulated code has two features:

❑ Instance variables are kept protected (usually with the private modifier).

❑ Getter and setter methods provide access to instance variables.

❑ IS-A refers to inheritance.

❑ IS-A is expressed with the keyword extends.

❑ “IS-A,” “inherits from,” “is derived from,” and “is a subtype of” are all equivalent expressions.

❑ HAS-A means an instance of one class “has a” reference to an instance of another class.

Overriding and Overloading

❑ Methods can be overridden or overloaded; constructors can be overloaded but not overridden.

❑ Abstract methods must be overridden by the first concrete (nonabstract) subclass.

❑ With respect to the method it overrides, the overriding method

❑ Must have the same argument list

❑ Must have the same return type

❑ Must not have a more restrictive access modifier

❑ May have a less restrictive access modifier

❑ Must not throw new or broader checked exceptions

❑ May throw fewer or narrower checked exceptions, or any unchecked exception

❑ Final methods cannot be overridden.

❑ Only inherited methods may be overridden.

❑ A subclass uses super.overriddenMethodName to call the superclass version of an overridden method.

❑ Overloading means reusing the same method name, but with different arguments.

❑ Overloaded methods

❑ Must have different argument lists

❑ May have different return types, as long as the argument lists are also different

❑ May have different access modifiers

❑ May throw different exceptions

❑ Methods from a superclass can be overloaded in a subclass.

❑ Polymorphism applies to overriding, not to overloading

❑ Object type determines which overridden method is used at runtime.

❑ Reference type determines which overloaded method will be used at compile time.

Instantiation and Constructors

❑ Objects are constructed:

❑ You cannot create a new object without invoking a constructor.

❑ Each superclass in an object’s inheritance tree will have a constructor called.

❑ Every class, even abstract classes, has at least one constructor.

❑ Constructors must have the same name as the class.

❑ Constructors do not have a return type. If there is a return type, then it is simply a method with the same name as the class, and not a constructor.

❑ Constructor execution occurs as follows:

❑ The constructor calls its superclass constructor, which calls its superclass constructor, and so on all the way up to the Object constructor.

❑ The Object constructor executes and then returns to the calling constructor, which runs to completion and then returns to its calling constructor, and so on back down to the completion of the constructor of the actual instance being created.

❑ Constructors can use any access modifier (even private!).

❑ The compiler will create a default constructor if you don’t create any constructors in your class.

❑ The default constructor is a no-arg constructor with a no-arg call to super().

❑ The first statement of every constructor must be a call to either this()

(an overloaded constructor) or super().

❑ The compiler will add a call to super() if you do not, unless you have already put in a call to this().

❑ Instance methods and variables are only accessible after the super constructor runs.

❑ Abstract classes have constructors that are called when a concrete subclass is instantiated.

❑ Interfaces do not have constructors.

❑ If your superclass does not have a no-arg constructor, you must create a constructor and insert a call to super() with arguments matching those of the superclass constructor.

❑ Constructors are never inherited, thus they cannot be overridden.

❑ A constructor can be directly invoked only by another constructor (using a call to super() or this()).

❑ Issues with calls to this():

❑ May appear only as the first statement in a constructor.

❑ The argument list determines which overloaded constructor is called.

❑ Constructors can call constructors can call constructors, and so on, but sooner or later one of them better call super() or the stack will explode.

❑ this() and super() cannot be in the same constructor. You can have one or the other, but never both.

Return Types

❑ Overloaded methods can change return types; overridden methods cannot.

❑ Object reference return types can accept null as a return value.

❑ An array is a legal return type, both to declare and return as a value.

❑ For methods with primitive return types, any value that can be implicitly converted to the return type can be returned.

❑ Nothing can be returned from a void, but you can return nothing. You’re allowed to simply say return, in any method with a void return type, to bust out of a method early. But you can’t return nothing from a method with a non-void return type.

❑ For methods with an object reference return type, a subclass of that type can be returned.

❑ For methods with an interface return type, any implementer of that interface can be returned.