Working with Live Wallpapers Android

In addition to still image wallpapers, Android supports the notion of a live wallpaper. Instead of displaying a static background image on the Home screen, the user can set an interactive, or live, wallpaper that can display anything that can be drawn on a surface, such as graphics and animations. Live wallpapers were introduced in Android 2.1 (API Level 7). Your applications can provide live wallpapers that use 3D graphics and animations as well as display interesting application content. Some examples of live wallpapers include

  • A 3D display showing an animated scene portraying abstract shapes
  • A service that animates between images found on an online image-sharing service
  • An interactive pond with water that ripples with touch
  • Wallpapers that change based on the actual season,weather, and time of day

Creating a Live Wallpaper

A live wallpaper is similar to an Android Service, but its result is a surface that the host can display. You need to make the following changes to your application in order to support live wallpapers:

  • Provide an XML wallpaper configuration.
  • Provide a WallpaperService implementation.
  • Update the application Android manifest file to register the wallpaper service with the appropriate permissions.

Now let’s look at some of these requirements in greater detail.

Creating a Live Wallpaper Service

The guts of the live wallpaper functionality are provided as part of a WallpaperService implementation, and most of the live wallpaper functionality is driven by its Wallpaper Service.Engine implementation.

Implementing a Wallpaper Service

Your application needs to extend the WallpaperService class. The most important method the class needs to override is the onCreateEngine() method. Here is a sample implementation of a wallpaper service called SimpleDroidWallpaper:

public class SimpleDroidWallpaper extends WallpaperService {
private final Handler handler = new Handler();
@Override
public Engine onCreateEngine() {
return new SimpleWallpaperEngine();
}
class SimpleWallpaperEngine extends WallpaperService.Engine {
// Your implementation of a wallpaper service engine here...
} }

There’s not much to this wallpaper service. The onCreateEngine() method simply returns your application’s custom wallpaper engine, which provides all the functionality for a specific live wallpaper. You could also override the other wallpaper service methods, as necessary. A Handler object is initialized for posting wallpaper draw operations.

Implementing a Wallpaper Service Engine

Now let’s take a closer look at the wallpaper service engine implementation. The wallpaper service engine handles all the details regarding the lifecycle of a specific instance of a live wallpaper. Much like the graphics examples used in Chapter “Using Android 3D Graphics with OpenGL ES,” live wallpaper implementations use a Surface object to draw to the screen.

There are a number of callback methods of interest within the wallpaper service engine:

  • You can override the onCreate() and onDestroy() methods to set up and tear down the live wallpaper. The Surface object is not valid during these parts of the lifecycle.
  • You can override the onSurfaceCreated() and onSurfaceDestroyed() methods (convenience methods for the Surface setup and teardown) to set up and tear down the Surface used for live wallpaper drawing.
  • You should override the onVisibilityChanged() method to handle live wallpaper visibility. When invisible, a live wallpaper must not remain running. This method should be treated much like an Activity pause or resume event.
  • The onSurfaceChanged() method is another convenience method for Surface management.
  • You can override the onOffsetsChanged() method to enable the live wallpaper to react when the user swipes between Home screens.
  • You can override the onTouchEvent() method to handle touch events. The incoming parameter is a MotionEvent object—we talk about the MotionEvent class in detail in the gestures section of Chapter “Handling Advanced User Input.”You also need to enable touch events (off by default) for the live wallpaper using the setTouch Events Enabled() method.

The implementation details of the live wallpaper are up to the developer. Often, a live wallpaper implementation uses OpenGL ES calls to draw to the screen. For example, the sample live wallpaper project included with this book includes a live wallpaper service that creates a Bitmap graphic of a droid, which floats around the screen, bouncing off the edges of the wallpaper boundaries. It also responds to touch events by changing its drift direction. Its wallpaper engine uses a thread to manage drawing operations, posting them back to the system using the Handler object defined in the wallpaper service.

Creating a Live Wallpaper Configuration

Next, your application must provide an XML wallpaper definition. You can store this definition within the project’s resources in the /res/xml directory. For example, here is a simple wallpaper definition called /res/xml/droid_wallpaper.xml:

<?xml version="1.0" encoding="utf-8"?>
<wallpaper xmlns:android="http://schemas.android.com/apk/res/android"
android:thumbnail="@drawable/live_wallpaper_android"
android:description="@string/wallpaper_desc" />

This simple wallpaper definition is encapsulated within the <wallpaper> XML tag. The description and thumbnail attributes are displayed on the wallpaper picker, where the user is prompted to select a specific wallpaper to use.

Configuring the Android Manifest File for Live Wallpapers

Finally, you need to update the application’s Android manifest file to expose the live wallpaper service. Specifically, the WallpaperService needs to be registered using the <service> tag. The <service> tag must include several important bits of information:

  • The WallpaperService class
  • The BIND_WALLPAPER permission
  • An intent filter for the WallpaperService action
  • Wallpaper metadata to reference the live wallpaper configuration

Let’s look at an example. Here is the <service> tag implementation for a simple live wallpaper:

<service
android:label="@string/wallpaper_name"
android:name="SimpleDroidWallpaper"
android:permission="android.permission.BIND_WALLPAPER">
<intent-filter>
<action
android:name="android.service.wallpaper.WallpaperService" />
</intent-filter>
<meta-data
android:name="android.service.wallpaper" android:resource="@xml/droid_wallpaper" />
</service>

In addition to the service definition, you also need to limit installation of your application to API Level 7 and higher (where support for live wallpapers exists) using the <uses-sdk> manifest tag:

<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="8" />

Keep in mind that your live wallpaper might use APIs (such as OpenGL ES 2.0 APIs) that require a higher minSdkVersion than API Level 7.You might also want to use the <uses-feature> tag to specify that your application includes live folder support, for use within Android Market filters:

<uses-feature android:name="android.software.live_wallpaper" />

Installing a Live Wallpaper

After you’ve implemented live wallpaper support within your application, you can set a live wallpaper on your Home screen using the following steps:

  1. Long-press on the Home Screen.
  2. From the menu, choose the Wallpapers option, as shown in Figure (left).
  3. Installing a live wallpaper on the Home screen.

    Installing a live wallpaper on the Home screen.

  4. From the Wallpaper menu, choose the Live wallpapers option, as shown in Figure (middle).
  5. From the Live Wallpaper menu, choose the live wallpaper you want to include, as shown in Figure (right).
  6. After you’ve chosen a wallpaper, it is shown in preview mode. Simply choose the Set Wallpaper button to confirm you want to use that live wallpaper. The live wallpaper is now visible on your Home screen, as shown in the following figure.
  7. A Simple AppWidget on the Home screen that displays the security threat level.

    A Simple AppWidget on the Home screen that displays the security threat level.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Android Topics