Tuesday, 27 November 2012

DreamService class (support for DayDream)

In it's latest 4.2 version, Android developers introduced a new featured called DayDream which is basically an interactive screensaver. As you may already guess, this features activates when the phone is in the idle state but it also activates when the phone is charging.


The developers have the possibility of creating custom "dreams" (screensavers) by extending the DreamService class. This class provides several methods which can be overridden to implement custom functionality.

These are quite simple to understand. The onAttachedToWindow() serves as a typical init method, where you can initialize various stuff for you "dream". However, you must'n start any animation now. You can do this by overriding the onDreamingStarted() method.

The other methods you can override are onDreamingStopped() and onDetachedFromWindow() which are the "end" versions of the first two.

DreamService also has a method called setInteractive which let's you specify wheter your "dream" is an interactive one or not. A "dream" is interactive if it's not interrupted by the user's touch.

You can write a simple static "dream" by creating a new Activity layout and then calling setContentView(R.layout.yourdreamlayoutid) inside the onAttachedToWindow method described above.

For more documentation, check the official documentation.

No comments:

Post a Comment