Android 4.2 includes a new powerful screen saver feature named Daydream.
A "dream" starts when the device is docked or charged and the screen timeouts. By default dreams are non-interactive, which means that once the user touches the screen the dream is dismissed. However dreams can also be interactive and include standard views (controls).
It is worth reading Google's blog about this feature: Android Developers Blog: Daydream: Interactive Screen Savers
The Daydream library allows you to create your own dreams.
The dream is handled by a service. The service name must be DreamService.
The dreams are set under Settings - Display - Daydreams.
Steps required to create a dream
- Add a service named DreamService.
- Add the following code to the manifest editor:
You can modify the label value. This value will appear in the dreams list (use this list during development to start the dream):
You can change the icon by changing the application icon.
- Set minSdkVersion and targetSdkVersion in the manifest editor to 17 (17 is Android 4.2).
- Reference android.jar from platform 17 or above (Tools - Configure Paths). You will probably need to first download it with Android SDK Manager.
Simple dream that draws random circles on the screen:
Important subs:
- Daydream should be initialized in Service_Start.
- SizeChanged event is raised when the main panel is first ready or after the user rotates the screen. In this sub you should build the layout (by adding views to the panel or by initializing the canvas and drawing on the panel).
- DreamStarted event is raised when the dream starts.
- DreamStopped event is raised when the dream stops.
Daydream.Canvas is a placeholder for a canvas. You should initialize it before using it (as done in the above example).
Dreams limitations
Services usually do not interact with UI elements. There are some limitations which you should be aware of:
- Views (and other activity objects) cannot be declared in Sub Process_Globals. You should instead fetch the views from the main panel as done in the WebView example.
- Layout files cannot be loaded.
- Percentage units cannot be used (100%x, 100%y).
Examples
Two examples are attached. The circles example is a dream that randomly draws circles on the screen. The webview example is an interactive dream that loads the URL that the user enters with WebView. The URL is saved in a file.
A "dream" starts when the device is docked or charged and the screen timeouts. By default dreams are non-interactive, which means that once the user touches the screen the dream is dismissed. However dreams can also be interactive and include standard views (controls).
It is worth reading Google's blog about this feature: Android Developers Blog: Daydream: Interactive Screen Savers
The Daydream library allows you to create your own dreams.
The dream is handled by a service. The service name must be DreamService.
The dreams are set under Settings - Display - Daydreams.
Steps required to create a dream
- Add a service named DreamService.
- Add the following code to the manifest editor:
B4X:
AddApplicationText(<service
android:name="anywheresoftware.b4a.objects.DreamServiceWrapper"
android:exported="true"
android:label="Dream Service Example">
<intent-filter>
<action android:name="android.service.dreams.DreamService" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</service>)
You can change the icon by changing the application icon.
- Set minSdkVersion and targetSdkVersion in the manifest editor to 17 (17 is Android 4.2).
- Reference android.jar from platform 17 or above (Tools - Configure Paths). You will probably need to first download it with Android SDK Manager.
Simple dream that draws random circles on the screen:
B4X:
Sub Process_Globals
Private dd As Daydream
Private timer1 As Timer
End Sub
Sub Service_Create
End Sub
Sub Service_Start (StartingIntent As Intent)
dd.Initialize("dd")
timer1.Initialize("timer1", 800)
End Sub
Sub dd_SizeChanged
dd.Canvas.Initialize(dd.Panel)
End Sub
Sub dd_DreamStarted
dd.Interactive = False
timer1.Enabled = True
End Sub
Sub Timer1_Tick
dd.Canvas.DrawCircle(Rnd(0, dd.Panel.Width), _
Rnd(0, dd.Panel.Height), _
Rnd(10dip, 100dip), _
Rnd(0x80000001, 0), _
True, 0)
dd.Panel.Invalidate
End Sub
Sub dd_DreamStopped
timer1.Enabled = False
End Sub
Sub Service_Destroy
End Sub
Important subs:
- Daydream should be initialized in Service_Start.
- SizeChanged event is raised when the main panel is first ready or after the user rotates the screen. In this sub you should build the layout (by adding views to the panel or by initializing the canvas and drawing on the panel).
- DreamStarted event is raised when the dream starts.
- DreamStopped event is raised when the dream stops.
Daydream.Canvas is a placeholder for a canvas. You should initialize it before using it (as done in the above example).
Dreams limitations
Services usually do not interact with UI elements. There are some limitations which you should be aware of:
- Views (and other activity objects) cannot be declared in Sub Process_Globals. You should instead fetch the views from the main panel as done in the WebView example.
- Layout files cannot be loaded.
- Percentage units cannot be used (100%x, 100%y).
Examples
Two examples are attached. The circles example is a dream that randomly draws circles on the screen. The webview example is an interactive dream that loads the URL that the user enters with WebView. The URL is saved in a file.