public
class
DreamService
extends Service
implements
Window.Callback
java.lang.Object | ||||
↳ | android.content.Context | |||
↳ | android.content.ContextWrapper | |||
↳ | android.app.Service | |||
↳ | android.service.dreams.DreamService |
Extend this class to implement a custom dream (available to the user as a "Daydream").
Dreams are interactive screensavers launched when a charging device is idle, or docked in a desk dock. Dreams provide another modality for apps to express themselves, tailored for an exhibition/lean-back experience.
The DreamService
lifecycle is as follows:
onAttachedToWindow()
Use this for initial setup, such as calling setContentView()
.
onDreamingStarted()
Your dream has started, so you should begin animations or other behaviors here.
onDreamingStopped()
Use this to stop the things you started in onDreamingStarted()
.
onDetachedFromWindow()
Use this to dismantle resources (for example, detach from handlers and listeners).
In addition, onCreate and onDestroy (from the Service interface) will also be called, but initialization and teardown should be done by overriding the hooks above.
To be available to the system, your DreamService
should be declared in the
manifest as follows:
<service android:name=".MyDream" android:exported="true" android:icon="@drawable/my_icon" android:label="@string/my_dream_label" > <intent-filter> <action android:name="android.service.dreams.DreamService" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> <!-- Point to additional information for this dream (optional) --> <meta-data android:name="android.service.dream" android:resource="@xml/my_dream" /> </service>
If specified with the <meta-data>
element,
additional information for the dream is defined using the
<dream>
element in a separate XML file.
Currently, the only addtional
information you can provide is for a settings activity that allows the user to configure
the dream behavior. For example:
<dream xmlns:android="http://schemas.android.com/apk/res/android" android:settingsActivity="com.example.app/.MyDreamSettingsActivity" />
This makes a Settings button available alongside your dream's listing in the system settings, which when pressed opens the specified activity.
To specify your dream layout, call setContentView(View)
, typically during the
onAttachedToWindow()
callback. For example:
public class MyDream extends DreamService { @Override public void onAttachedToWindow() { super.onAttachedToWindow(); // Exit dream upon user touch setInteractive(false); // Hide system UI setFullscreen(true); // Set the dream layout setContentView(R.layout.dream); } }
When targeting api level 21 and above, you must declare the service in your manifest file
with the BIND_DREAM_SERVICE
permission. For example:
<service android:name=".MyDream" android:exported="true" android:icon="@drawable/my_icon" android:label="@string/my_dream_label" android:permission="android.permission.BIND_DREAM_SERVICE"> <intent-filter> <action android:name=”android.service.dreams.DreamService” /> <category android:name=”android.intent.category.DEFAULT” /> </intent-filter> </service>
Constants | |
---|---|
String |
DREAM_META_DATA
Name under which a Dream publishes information about itself. |
String |
SERVICE_INTERFACE
The |
Inherited constants |
---|
From
class
android.app.Service
|
From
class
android.content.Context
|
From
interface
android.content.ComponentCallbacks2
|
Public constructors | |
---|---|
DreamService()
|
Public methods | |
---|---|
void
|
addContentView(View view, ViewGroup.LayoutParams params)
Adds a view to the Dream's window, leaving other content views in place. |
boolean
|
dispatchGenericMotionEvent(MotionEvent event)
Called to process generic motion events. |
boolean
|
dispatchKeyEvent(KeyEvent event)
Called to process key events. |
boolean
|
dispatchKeyShortcutEvent(KeyEvent event)
Called to process a key shortcut event. |
boolean
|
dispatchPopulateAccessibilityEvent(AccessibilityEvent event)
Called to process population of |
boolean
|
dispatchTouchEvent(MotionEvent event)
Called to process touch screen events. |
boolean
|
dispatchTrackballEvent(MotionEvent event)
Called to process trackball events. |
View
|
findViewById(int id)
Finds a view that was identified by the id attribute from the XML that
was processed in |
final
void
|
finish()
Stops the dream and detaches from the window. |
Window
|
getWindow()
Retrieves the current |
WindowManager
|
getWindowManager()
Retrieves the current |
boolean
|
isFullscreen()
Returns whether or not this dream is in fullscreen mode. |
boolean
|
isInteractive()
Returns whether or not this dream is interactive. |
boolean
|
isScreenBright()
Returns whether or not this dream keeps the screen bright while dreaming. |
void
|
onActionModeFinished(ActionMode mode)
Called when an action mode has been finished. |
void
|
onActionModeStarted(ActionMode mode)
Called when an action mode has been started. |
void
|
onAttachedToWindow()
Called when the window has been attached to the window manager. |
final
IBinder
|
onBind(Intent intent)
Return the communication channel to the service. |
void
|
onContentChanged()
This hook is called whenever the content view of the screen changes
(due to a call to
|
void
|
onCreate()
Called when this Dream is constructed. |
boolean
|
onCreatePanelMenu(int featureId, Menu menu)
Initialize the contents of the menu for panel 'featureId'. |
View
|
onCreatePanelView(int featureId)
Instantiate the view to display in the panel for 'featureId'. |
void
|
onDestroy()
Called by the system to notify a Service that it is no longer used and is being removed. |
void
|
onDetachedFromWindow()
Called when the window has been attached to the window manager. |
void
|
onDreamingStarted()
Called when the dream's window has been created and is visible and animation may now begin. |
void
|
onDreamingStopped()
Called when this Dream is stopped, either by external request or by calling finish(), before the window has been removed. |
boolean
|
onMenuItemSelected(int featureId, MenuItem item)
Called when a panel's menu item has been selected by the user. |
boolean
|
onMenuOpened(int featureId, Menu menu)
Called when a panel's menu is opened by the user. |
void
|
onPanelClosed(int featureId, Menu menu)
Called when a panel is being closed. |
boolean
|
onPreparePanel(int featureId, View view, Menu menu)
Prepare a panel to be displayed. |
boolean
|
onSearchRequested()
Called when the user signals the desire to start a search. |
boolean
|
onSearchRequested(SearchEvent event)
Called when the user signals the desire to start a search. |
void
|
onWakeUp()
Called when the dream is being asked to stop itself and wake. |
void
|
onWindowAttributesChanged(WindowManager.LayoutParams attrs)
This is called whenever the current window attributes change. |
void
|
onWindowFocusChanged(boolean hasFocus)
This hook is called whenever the window focus changes. |
ActionMode
|
onWindowStartingActionMode(ActionMode.Callback callback)
Called when an action mode is being started for this window. |
ActionMode
|
onWindowStartingActionMode(ActionMode.Callback callback, int type)
Called when an action mode is being started for this window. |
void
|
setContentView(View view)
Sets a view to be the content view for this Dream. |
void
|
setContentView(int layoutResID)
Inflates a layout resource and set it to be the content view for this Dream. |
void
|
setContentView(View view, ViewGroup.LayoutParams params)
Sets a view to be the content view for this Dream. |
void
|
setFullscreen(boolean fullscreen)
Controls |
void
|
setInteractive(boolean interactive)
Marks this dream as interactive to receive input events. |
void
|
setScreenBright(boolean screenBright)
Marks this dream as keeping the screen bright while dreaming. |
final
void
|
wakeUp()
Wakes the dream up gently. |
Protected methods | |
---|---|
void
|
dump(FileDescriptor fd, PrintWriter pw, String[] args)
Print the Service's state into the given stream. |
Inherited methods | |
---|---|
From
class
android.app.Service
| |
From
class
android.content.ContextWrapper
| |
From
class
android.content.Context
| |
From
class
java.lang.Object
| |
From
interface
android.content.ComponentCallbacks2
| |
From
interface
android.view.Window.Callback
| |
From
interface
android.content.ComponentCallbacks
|
String DREAM_META_DATA
Name under which a Dream publishes information about itself.
This meta-data must reference an XML resource containing
a <
tag.
dream
>
Constant Value: "android.service.dream"
String SERVICE_INTERFACE
The Intent
that must be declared as handled by the service.
Constant Value: "android.service.dreams.DreamService"
void addContentView (View view, ViewGroup.LayoutParams params)
Adds a view to the Dream's window, leaving other content views in place.
Note: Requires a window, do not call before onAttachedToWindow()
Parameters | |
---|---|
view |
View :
The desired content to display. |
params |
ViewGroup.LayoutParams :
Layout parameters for the view.
|
boolean dispatchGenericMotionEvent (MotionEvent event)
Called to process generic motion events. At the very least your
implementation must call
superDispatchGenericMotionEvent(MotionEvent)
to do the
standard processing.
Parameters | |
---|---|
event |
MotionEvent :
The generic motion event. |
Returns | |
---|---|
boolean |
boolean Return true if this event was consumed. |
boolean dispatchKeyEvent (KeyEvent event)
Called to process key events. At the very least your
implementation must call
superDispatchKeyEvent(KeyEvent)
to do the
standard key processing.
Parameters | |
---|---|
event |
KeyEvent :
The key event. |
Returns | |
---|---|
boolean |
boolean Return true if this event was consumed. |
boolean dispatchKeyShortcutEvent (KeyEvent event)
Called to process a key shortcut event.
At the very least your implementation must call
superDispatchKeyShortcutEvent(KeyEvent)
to do the
standard key shortcut processing.
Parameters | |
---|---|
event |
KeyEvent :
The key shortcut event. |
Returns | |
---|---|
boolean |
True if this event was consumed. |
boolean dispatchPopulateAccessibilityEvent (AccessibilityEvent event)
Called to process population of AccessibilityEvent
s.
Parameters | |
---|---|
event |
AccessibilityEvent :
The event. |
Returns | |
---|---|
boolean |
boolean Return true if event population was completed. |
boolean dispatchTouchEvent (MotionEvent event)
Called to process touch screen events. At the very least your
implementation must call
superDispatchTouchEvent(MotionEvent)
to do the
standard touch screen processing.
Parameters | |
---|---|
event |
MotionEvent :
The touch screen event. |
Returns | |
---|---|
boolean |
boolean Return true if this event was consumed. |
boolean dispatchTrackballEvent (MotionEvent event)
Called to process trackball events. At the very least your
implementation must call
superDispatchTrackballEvent(MotionEvent)
to do the
standard trackball processing.
Parameters | |
---|---|
event |
MotionEvent :
The trackball event. |
Returns | |
---|---|
boolean |
boolean Return true if this event was consumed. |
View findViewById (int id)
Finds a view that was identified by the id attribute from the XML that
was processed in onCreate()
.
Note: Requires a window, do not call before onAttachedToWindow()
Parameters | |
---|---|
id |
int
|
Returns | |
---|---|
View |
The view if found or null otherwise. |
void finish ()
Stops the dream and detaches from the window.
When the dream ends, the system will be allowed to go to sleep fully unless there is a reason for it to be awake such as recent user activity or wake locks being held.
Window getWindow ()
Retrieves the current Window
for the dream.
Behaves similarly to getWindow()
.
Returns | |
---|---|
Window |
The current window, or null if the dream is not started. |
WindowManager getWindowManager ()
Retrieves the current WindowManager
for the dream.
Behaves similarly to getWindowManager()
.
Returns | |
---|---|
WindowManager |
The current window manager, or null if the dream is not started. |
boolean isFullscreen ()
Returns whether or not this dream is in fullscreen mode. Defaults to false.
Returns | |
---|---|
boolean |
See also:
boolean isInteractive ()
Returns whether or not this dream is interactive. Defaults to false.
Returns | |
---|---|
boolean |
See also:
boolean isScreenBright ()
Returns whether or not this dream keeps the screen bright while dreaming. Defaults to false, allowing the screen to dim if necessary.
Returns | |
---|---|
boolean |
See also:
void onActionModeFinished (ActionMode mode)
Called when an action mode has been finished. The appropriate mode callback method will have already been invoked.
Parameters | |
---|---|
mode |
ActionMode :
The mode that was just finished.
|
void onActionModeStarted (ActionMode mode)
Called when an action mode has been started. The appropriate mode callback method will have already been invoked.
Parameters | |
---|---|
mode |
ActionMode :
The new mode that has just been started.
|
void onAttachedToWindow ()
Called when the window has been attached to the window manager.
See View.onAttachedToWindow()
for more information.
IBinder onBind (Intent intent)
Return the communication channel to the service. May return null if
clients can not bind to the service. The returned
IBinder
is usually for a complex interface
that has been described using
aidl.
Note that unlike other application components, calls on to the IBinder interface returned here may not happen on the main thread of the process. More information about the main thread can be found in Processes and Threads.
Parameters | |
---|---|
intent |
Intent :
The Intent that was used to bind to this service,
as given to Context.bindService . Note that any extras that were included with
the Intent at that point will not be seen here. |
Returns | |
---|---|
IBinder |
Return an IBinder through which clients can call on to the service. |
void onContentChanged ()
This hook is called whenever the content view of the screen changes
(due to a call to
Window.setContentView
or
Window.addContentView
).
boolean onCreatePanelMenu (int featureId, Menu menu)
Initialize the contents of the menu for panel 'featureId'. This is called if onCreatePanelView() returns null, giving you a standard menu in which you can place your items. It is only called once for the panel, the first time it is shown.
You can safely hold on to menu (and any items created from it), making modifications to it as desired, until the next time onCreatePanelMenu() is called for this feature.
Parameters | |
---|---|
featureId |
int :
The panel being created. |
menu |
Menu :
The menu inside the panel. |
Returns | |
---|---|
boolean |
boolean You must return true for the panel to be displayed; if you return false it will not be shown. |
View onCreatePanelView (int featureId)
Instantiate the view to display in the panel for 'featureId'. You can return null, in which case the default content (typically a menu) will be created for you.
Parameters | |
---|---|
featureId |
int :
Which panel is being created. |
Returns | |
---|---|
View |
view The top-level view to place in the panel. |
void onDestroy ()
Called by the system to notify a Service that it is no longer used and is being removed. The service should clean up any resources it holds (threads, registered receivers, etc) at this point. Upon return, there will be no more calls in to this Service object and it is effectively dead. Do not call this method directly.
void onDetachedFromWindow ()
Called when the window has been attached to the window manager.
See View.onDetachedFromWindow()
for more information.
void onDreamingStarted ()
Called when the dream's window has been created and is visible and animation may now begin.
void onDreamingStopped ()
Called when this Dream is stopped, either by external request or by calling finish(), before the window has been removed.
boolean onMenuItemSelected (int featureId, MenuItem item)
Called when a panel's menu item has been selected by the user.
Parameters | |
---|---|
featureId |
int :
The panel that the menu is in. |
item |
MenuItem :
The menu item that was selected. |
Returns | |
---|---|
boolean |
boolean Return true to finish processing of selection, or false to perform the normal menu handling (calling its Runnable or sending a Message to its target Handler). |
boolean onMenuOpened (int featureId, Menu menu)
Called when a panel's menu is opened by the user. This may also be called when the menu is changing from one type to another (for example, from the icon menu to the expanded menu).
Parameters | |
---|---|
featureId |
int :
The panel that the menu is in. |
menu |
Menu :
The menu that is opened. |
Returns | |
---|---|
boolean |
Return true to allow the menu to open, or false to prevent the menu from opening. |
void onPanelClosed (int featureId, Menu menu)
Called when a panel is being closed. If another logical subsequent panel is being opened (and this panel is being closed to make room for the subsequent panel), this method will NOT be called.
Parameters | |
---|---|
featureId |
int :
The panel that is being displayed. |
menu |
Menu :
If onCreatePanelView() returned null, this is the Menu
being displayed in the panel.
|
boolean onPreparePanel (int featureId, View view, Menu menu)
Prepare a panel to be displayed. This is called right before the panel window is shown, every time it is shown.
Parameters | |
---|---|
featureId |
int :
The panel that is being displayed. |
view |
View :
The View that was returned by onCreatePanelView(). |
menu |
Menu :
If onCreatePanelView() returned null, this is the Menu
being displayed in the panel. |
Returns | |
---|---|
boolean |
boolean You must return true for the panel to be displayed; if you return false it will not be shown. |
boolean onSearchRequested ()
Called when the user signals the desire to start a search.
Returns | |
---|---|
boolean |
true if search launched, false if activity refuses (blocks) |
boolean onSearchRequested (SearchEvent event)
Called when the user signals the desire to start a search.
Parameters | |
---|---|
event |
SearchEvent :
A SearchEvent describing the signal to
start a search. |
Returns | |
---|---|
boolean |
true if search launched, false if activity refuses (blocks) |
void onWakeUp ()
Called when the dream is being asked to stop itself and wake.
The default implementation simply calls finish()
which ends the dream
immediately. Subclasses may override this function to perform a smooth exit
transition then call finish()
afterwards.
Note that the dream will only be given a short period of time (currently about five seconds) to wake up. If the dream does not finish itself in a timely manner then the system will forcibly finish it once the time allowance is up.
void onWindowAttributesChanged (WindowManager.LayoutParams attrs)
This is called whenever the current window attributes change.
Parameters | |
---|---|
attrs |
WindowManager.LayoutParams
|
void onWindowFocusChanged (boolean hasFocus)
This hook is called whenever the window focus changes. See
View.onWindowFocusChangedNotLocked(boolean)
for more information.
Parameters | |
---|---|
hasFocus |
boolean :
Whether the window now has focus.
|
ActionMode onWindowStartingActionMode (ActionMode.Callback callback)
Called when an action mode is being started for this window. Gives the
callback an opportunity to handle the action mode in its own unique and
beautiful way. If this method returns null the system can choose a way
to present the mode or choose not to start the mode at all. This is equivalent
to onWindowStartingActionMode(android.view.ActionMode.Callback, int)
with type TYPE_PRIMARY
.
Parameters | |
---|---|
callback |
ActionMode.Callback :
Callback to control the lifecycle of this action mode |
Returns | |
---|---|
ActionMode |
The ActionMode that was started, or null if the system should present it |
ActionMode onWindowStartingActionMode (ActionMode.Callback callback, int type)
Called when an action mode is being started for this window. Gives the callback an opportunity to handle the action mode in its own unique and beautiful way. If this method returns null the system can choose a way to present the mode or choose not to start the mode at all.
Parameters | |
---|---|
callback |
ActionMode.Callback :
Callback to control the lifecycle of this action mode |
type |
int :
One of TYPE_PRIMARY or TYPE_FLOATING . |
Returns | |
---|---|
ActionMode |
The ActionMode that was started, or null if the system should present it |
void setContentView (View view)
Sets a view to be the content view for this Dream.
Behaves similarly to setContentView(android.view.View)
in an activity,
including using MATCH_PARENT
as the layout height and width of the view.
Note: This requires a window, so you should usually call it during
onAttachedToWindow()
and never earlier (you cannot call it
during onCreate()
).
Parameters | |
---|---|
view |
View
|
void setContentView (int layoutResID)
Inflates a layout resource and set it to be the content view for this Dream.
Behaves similarly to setContentView(int)
.
Note: Requires a window, do not call before onAttachedToWindow()
Parameters | |
---|---|
layoutResID |
int :
Resource ID to be inflated. |
void setContentView (View view, ViewGroup.LayoutParams params)
Sets a view to be the content view for this Dream.
Behaves similarly to
setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
in an activity.
Note: This requires a window, so you should usually call it during
onAttachedToWindow()
and never earlier (you cannot call it
during onCreate()
).
Parameters | |
---|---|
view |
View :
The desired content to display. |
params |
ViewGroup.LayoutParams :
Layout parameters for the view. |
void setFullscreen (boolean fullscreen)
Controls FLAG_FULLSCREEN
on the dream's window.
Parameters | |
---|---|
fullscreen |
boolean :
If true, the fullscreen flag will be set; else it
will be cleared.
|
void setInteractive (boolean interactive)
Marks this dream as interactive to receive input events.
Non-interactive dreams (default) will dismiss on the first input event.
Interactive dreams should call finish()
to dismiss themselves.
Parameters | |
---|---|
interactive |
boolean :
True if this dream will handle input events.
|
void setScreenBright (boolean screenBright)
Marks this dream as keeping the screen bright while dreaming.
Parameters | |
---|---|
screenBright |
boolean :
True to keep the screen bright while dreaming.
|
void wakeUp ()
Wakes the dream up gently.
Calls onWakeUp()
to give the dream a chance to perform an exit transition.
When the transition is over, the dream should call finish()
.
void dump (FileDescriptor fd, PrintWriter pw, String[] args)
Print the Service's state into the given stream. This gets invoked if
you run "adb shell dumpsys activity service <yourservicename>"
(note that for this command to work, the service must be running, and
you must specify a fully-qualified service name).
This is distinct from "dumpsys <servicename>", which only works for
named system services and which invokes the dump(FileDescriptor, String[])
method
on the IBinder
interface registered with ServiceManager.
Parameters | |
---|---|
fd |
FileDescriptor :
The raw file descriptor that the dump is being sent to. |
pw |
PrintWriter :
The PrintWriter to which you should dump your state. This will be
closed for you after you return. |
args |
String :
additional arguments to the dump request.
|