Creating a Wallpaper Service Engine

The WallpaperService.Engine class is where you create the Live Wallpaper itself.

The Wallpaper Service Engine encapsulates a Surface which is used to display the wallpaper and handle touch events. A Surface is a specialized drawing canvas that supports updates from background threads, making it ideal for creating smooth, dynamic, and interactive graphics. Both the Surface View, and handling touch events, are covered in more detail in Chapter 15.

To implement your own Wallpaper Service engine, extend the WallpaperService.Engine class. Before you can start drawing on the Surface, you must wait for it to complete initialization, indicated by the onSurfaceCreated event handler.

The Wallpaper Service Engine also includes an onTouchEvent callback to provide user-interactivity with the wallpaper, and the onOffsetsChanged handler to notify you that the parent Activity (usually the home screen) has been offset (panned).

Listing 10-28 shows the skeleton code for a Wallpaper Service Engine implementation. Refer to Chapter 15 for more details on how to draw on a Surface and use the onTouchEvent handler and Motion Events.

LISTING 10-28: Wallpaper Service Engine skeleton code download'on public class MyWallpaperServiceEngine extends WallpaperService.Engine { Wrax.com ©Override public void onCreate(SurfaceHolder surfaceHolder) { super.onCreate(surfaceHolder); // TODO Handle initialization.

©Override public void onOffsetsChanged(float xOffset, float yOffset, float xOffsetStep, float yOffsetStep, int xPixelOffset, int yPixelOffset) { super.onOffsetsChanged(xOffset, yOffset, xOffsetStep, yOffsetStep, xPixelOffset, yPixelOffset); // TODO Handle homescreen offset events.

©Override public void onTouchEvent(MotionEvent event) { super.onTouchEvent(event); // TODO Handle touch and motion events.

©Override public void onSurfaceCreated(SurfaceHolder holder) { super.onSurfaceCreated(holder);

// TODO Surface has been created, run the Thread that will // update the display.

SUMMARY

In this chapter you learned how to create App Widgets and Live Folders for your application. In particular you saw how to do the following:

> Implement widgets and add them to your applications.

> Control the update rate of your widgets by setting the minimum refresh rate or using Intents and Alarms.

> Update the UI of your widgets using Remote Views.

> Add interactivity to your widgets.

> Create and register a Live Folder for your application's Content Provider.

> Add a projection to your Content Provider to provide a Live Folder schema.

> Create and use Live Wallpaper.

> Add search to your application and surface search results to the Quick Search Box.

In the following chapter you will explore the audiovisual APIs available in Android. You'll take a look at multimedia playback and recording using the microphone and camera.

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide


Responses

  • malcolm mcgregor
    How implement ontouchevent handler in android?
    7 years ago
  • Gigliola Bergamaschi
    How to add a EULA to live wallpaper app?
    7 years ago
  • Jonas Kohl
    What is the content for wallpaper app html?
    2 years ago

Post a comment