public class DreamService
extends Service implements Window.Callback
| java.lang.Object | ||||
| android.content.Context | ||||
| android.content.ContextWrapper | ||||
| android.app.Service | ||||
| android.service.dreams.DreamService | ||||
扩展此类以实现自定义梦想(作为“Daydream”提供给用户)。
梦想是在充电设备闲置或停靠在桌子底座上时启动的交互式屏幕保护程序。 梦想为应用程序表达自己提供了另一种形式,专为展览/精益体验而量身定制。
DreamService生命周期如下所示:
onAttachedToWindow() 将其用于初始设置,例如调用 setContentView() 。
onDreamingStarted() 你的梦想已经开始,所以你应该在这里开始动画或其他行为。
onDreamingStopped() 用这个来停止你在 onDreamingStarted()开始的东西。
onDetachedFromWindow() 使用它来拆除资源(例如,分离处理程序和监听程序)。
另外,onCreate和onDestroy(来自Service接口)也将被调用,但是初始化和拆卸应该通过覆盖上面的钩子来完成。
为了使系统可用,应在清单中声明您的 DreamService ,如下所示:
<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>
如果使用<meta-data>元素指定,则使用单独的XML文件中的<dream>元素定义梦想的其他信息。 目前,您可以提供的唯一附加信息是允许用户配置梦想行为的设置活动。 例如:
RES / XML / my_dream.xml
<dream xmlns:android="http://schemas.android.com/apk/res/android"
android:settingsActivity="com.example.app/.MyDreamSettingsActivity" />
这使得设置按钮可以在系统设置中的梦想列表旁边使用,当按下按钮打开指定的活动时。
要指定您的梦想布局,请拨打setContentView(View) ,通常在onAttachedToWindow()回拨期间。 例如:
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);
}
}
当针对api level 21及以上版本时,您必须在清单文件中声明服务并具有BIND_DREAM_SERVICE权限。 例如:
<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 Dream发布自己的信息的名称。 |
String |
SERVICE_INTERFACE 必须声明为由服务处理的 |
Inherited constants |
|---|
android.app.Service
|
android.content.Context
|
android.content.ComponentCallbacks2
|
Public constructors |
|
|---|---|
DreamService() |
|
公共方法(Public methods) |
|
|---|---|
void |
addContentView(View view, ViewGroup.LayoutParams params) 将视图添加到Dream的窗口,留下其他内容视图。 |
boolean |
dispatchGenericMotionEvent(MotionEvent event) 被调用来处理通用运动事件。 |
boolean |
dispatchKeyEvent(KeyEvent event) 被调用来处理关键事件。 |
boolean |
dispatchKeyShortcutEvent(KeyEvent event) 被调用来处理键快捷键事件。 |
boolean |
dispatchPopulateAccessibilityEvent(AccessibilityEvent event) 打电话来处理 |
boolean |
dispatchTouchEvent(MotionEvent event) 被调用来处理触摸屏事件。 |
boolean |
dispatchTrackballEvent(MotionEvent event) 被称为处理轨迹球事件。 |
View |
findViewById(int id) 查找由 |
final void |
finish() 停止梦想并脱离窗口。 |
Window |
getWindow() 为梦想检索当前 |
WindowManager |
getWindowManager() 检索当前 |
boolean |
isFullscreen() 返回此梦是否处于全屏模式。 |
boolean |
isInteractive() 返回这个梦是否是互动的。 |
boolean |
isScreenBright() 返回这个梦是否在梦中保持屏幕亮度。 |
void |
onActionModeFinished(ActionMode mode) 当动作模式完成时调用。 |
void |
onActionModeStarted(ActionMode mode) 当动作模式已启动时调用。 |
void |
onAttachedToWindow() 当窗口已连接到窗口管理器时调用。 |
final IBinder |
onBind(Intent intent) 将通信信道返回给服务。 |
void |
onContentChanged() 只要屏幕的内容视图发生更改(由于致电 |
void |
onCreate() 当这个梦被构建时调用。 |
boolean |
onCreatePanelMenu(int featureId, Menu menu) 初始化面板'featureId'的菜单内容。 |
View |
onCreatePanelView(int featureId) 实例化视图以显示在“featureId”的面板中。 |
void |
onDestroy() 由系统调用以通知服务它已不再使用并正在被删除。 |
void |
onDetachedFromWindow() 当窗口已连接到窗口管理器时调用。 |
void |
onDreamingStarted() 当梦的窗口已经创建并且可见并且动画现在可以开始时调用。 |
void |
onDreamingStopped() 当窗口被移除之前,当这个梦被停止时,通过外部请求或调用finish()来调用。 |
boolean |
onMenuItemSelected(int featureId, MenuItem item) 当用户选择了面板的菜单项时调用。 |
boolean |
onMenuOpened(int featureId, Menu menu) 用户打开面板菜单时调用。 |
void |
onPanelClosed(int featureId, Menu menu) 当面板关闭时调用。 |
boolean |
onPreparePanel(int featureId, View view, Menu menu) 准备要显示的面板。 |
boolean |
onSearchRequested() 当用户指示开始搜索的愿望时调用。 |
boolean |
onSearchRequested(SearchEvent event) 当用户指示开始搜索的愿望时调用。 |
void |
onWakeUp() 当梦想被要求停止并唤醒时调用。 |
void |
onWindowAttributesChanged(WindowManager.LayoutParams attrs) 这在每当当前窗口属性改变时被调用。 |
void |
onWindowFocusChanged(boolean hasFocus) 这个钩子在窗口焦点改变时被调用。 |
ActionMode |
onWindowStartingActionMode(ActionMode.Callback callback) 当此窗口的动作模式正在启动时调用。 |
ActionMode |
onWindowStartingActionMode(ActionMode.Callback callback, int type) 当此窗口的动作模式正在启动时调用。 |
void |
setContentView(View view) 将视图设置为此Dream的内容视图。 |
void |
setContentView(int layoutResID) 使布局资源膨胀并将其设置为此Dream的内容视图。 |
void |
setContentView(View view, ViewGroup.LayoutParams params) 将视图设置为此Dream的内容视图。 |
void |
setFullscreen(boolean fullscreen) 在梦想窗口上控制 |
void |
setInteractive(boolean interactive) 将此梦想标记为交互式以接收输入事件。 |
void |
setScreenBright(boolean screenBright) 将这个梦想标志为在梦中保持屏幕亮度。 |
final void |
wakeUp() 轻轻唤醒梦。 |
Protected methods |
|
|---|---|
void |
dump(FileDescriptor fd, PrintWriter pw, String[] args) 将服务的状态打印到给定的流中。 |
继承方法(Inherited methods) |
|
|---|---|
android.app.Service
|
|
android.content.ContextWrapper
|
|
android.content.Context
|
|
java.lang.Object
|
|
android.content.ComponentCallbacks2
|
|
android.view.Window.Callback
|
|
android.content.ComponentCallbacks
|
|
String DREAM_META_DATA
Dream发布自己的信息的名称。 此元数据必须引用包含<标记的XML资源。dream>
常量值:“android.service.dream”
String SERVICE_INTERFACE
Intent必须声明为由服务处理。
常量值:“android.service.dreams.DreamService”
void addContentView (View view, ViewGroup.LayoutParams params)
将视图添加到Dream的窗口,留下其他内容视图。
注意:需要一个窗口,请勿在 onAttachedToWindow()之前 onAttachedToWindow()
| 参数(Parameters) | |
|---|---|
view |
View: The desired content to display. |
params |
ViewGroup.LayoutParams: Layout parameters for the view. |
boolean dispatchGenericMotionEvent (MotionEvent event)
被调用来处理通用运动事件。 至少您的实现必须调用superDispatchGenericMotionEvent(MotionEvent)来执行标准处理。
| 参数(Parameters) | |
|---|---|
event |
MotionEvent: The generic motion event. |
| 返回(Returns) | |
|---|---|
boolean |
boolean Return true if this event was consumed. |
boolean dispatchKeyEvent (KeyEvent event)
被调用来处理关键事件。 至少您的实现必须调用superDispatchKeyEvent(KeyEvent)来执行标准密钥处理。
| 参数(Parameters) | |
|---|---|
event |
KeyEvent: The key event. |
| 返回(Returns) | |
|---|---|
boolean |
boolean Return true if this event was consumed. |
boolean dispatchKeyShortcutEvent (KeyEvent event)
被调用来处理键快捷键事件。 至少你的实现必须调用superDispatchKeyShortcutEvent(KeyEvent)来执行标准的快捷键处理。
| 参数(Parameters) | |
|---|---|
event |
KeyEvent: The key shortcut event. |
| 返回(Returns) | |
|---|---|
boolean |
True if this event was consumed. |
boolean dispatchPopulateAccessibilityEvent (AccessibilityEvent event)
打电话来处理 AccessibilityEvent的人口。
| 参数(Parameters) | |
|---|---|
event |
AccessibilityEvent: The event. |
| 返回(Returns) | |
|---|---|
boolean |
boolean Return true if event population was completed. |
boolean dispatchTouchEvent (MotionEvent event)
被调用来处理触摸屏事件。 至少你的实现必须调用superDispatchTouchEvent(MotionEvent)来完成标准的触摸屏处理。
| 参数(Parameters) | |
|---|---|
event |
MotionEvent: The touch screen event. |
| 返回(Returns) | |
|---|---|
boolean |
boolean Return true if this event was consumed. |
boolean dispatchTrackballEvent (MotionEvent event)
被称为处理轨迹球事件。 至少你的实现必须调用superDispatchTrackballEvent(MotionEvent)来执行标准的轨迹球处理。
| 参数(Parameters) | |
|---|---|
event |
MotionEvent: The trackball event. |
| 返回(Returns) | |
|---|---|
boolean |
boolean Return true if this event was consumed. |
View findViewById (int id)
查找由 onCreate()中处理的XML中的id属性标识的视图。
注意:需要一个窗口,请勿在 onAttachedToWindow()之前 onAttachedToWindow()
| 参数(Parameters) | |
|---|---|
id |
int
|
| 返回(Returns) | |
|---|---|
View |
The view if found or null otherwise. |
void finish ()
停止梦想并脱离窗口。
当梦想结束时,系统将被允许完全进入睡眠状态,除非有理由让它醒来,例如最近的用户活动或唤醒锁定。
Window getWindow ()
为梦想检索当前Window 。 行为与getWindow()类似。
| 返回(Returns) | |
|---|---|
Window |
The current window, or null if the dream is not started. |
WindowManager getWindowManager ()
为梦想检索当前WindowManager 。 行为类似于getWindowManager() 。
| 返回(Returns) | |
|---|---|
WindowManager |
The current window manager, or null if the dream is not started. |
boolean isFullscreen ()
返回此梦是否处于全屏模式。 默认为false。
| 返回(Returns) | |
|---|---|
boolean |
|
也可以看看:
boolean isInteractive ()
返回这个梦是否是互动的。 默认为false。
| 返回(Returns) | |
|---|---|
boolean |
|
也可以看看:
boolean isScreenBright ()
返回这个梦是否在梦中保持屏幕亮度。 默认为false,允许屏幕在必要时变暗。
| 返回(Returns) | |
|---|---|
boolean |
|
也可以看看:
void onActionModeFinished (ActionMode mode)
当动作模式完成时调用。 适当的模式回调方法已经被调用。
| 参数(Parameters) | |
|---|---|
mode |
ActionMode: The mode that was just finished. |
void onActionModeStarted (ActionMode mode)
当动作模式已启动时调用。 适当的模式回调方法已经被调用。
| 参数(Parameters) | |
|---|---|
mode |
ActionMode: The new mode that has just been started. |
void onAttachedToWindow ()
当窗口已连接到窗口管理器时调用。 有关更多信息,请参阅View.onAttachedToWindow() 。
IBinder onBind (Intent intent)
将通信信道返回给服务。 如果客户端无法绑定到服务,可能会返回null。 返回IBinder通常是一个复杂的界面已经described using aidl 。
请注意,与其他应用程序组件不同,此处返回的IBinder接口调用可能不会发生在进程的主线程上 。 有关主线程的更多信息可以在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 ()
只要屏幕的内容视图发生变化(由于调用 Window.setContentView或 Window.addContentView ), Window.setContentView调用此钩子。
boolean onCreatePanelMenu (int featureId,
Menu menu)
初始化面板'featureId'的菜单内容。 如果onCreatePanelView()返回null,这会被调用,为您提供一个标准菜单,您可以在其中放置项目。 它仅在面板显示时才被调用一次。
您可以安全地按住 菜单 (以及从中创建的任何项目),根据需要对其进行修改,直到下次为此功能调用onCreatePanelMenu()。
| 参数(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)
实例化视图以显示在“featureId”的面板中。 您可以返回null,在这种情况下,将为您创建默认内容(通常是菜单)。
| 参数(Parameters) | |
|---|---|
featureId |
int: Which panel is being created. |
| 返回(Returns) | |
|---|---|
View |
view The top-level view to place in the panel. |
void onDestroy ()
由系统调用以通知服务它已不再使用并正在被删除。 这个服务应该清理它所拥有的任何资源(线程,注册接收者等)。 返回后,将不会有更多的调用这个服务对象,它实际上已经死了。 不要直接调用这个方法。
void onDetachedFromWindow ()
当窗口已连接到窗口管理器时调用。 有关更多信息,请参阅View.onDetachedFromWindow() 。
void onDreamingStopped ()
当窗口被移除之前,当这个梦被停止时,通过外部请求或调用finish()来调用。
boolean onMenuItemSelected (int featureId,
MenuItem item)
当用户选择了面板的菜单项时调用。
| 参数(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)
用户打开面板菜单时调用。 当菜单从一种类型更改为另一种时(例如,从图标菜单变为扩展菜单),也可能会调用此功能。
| 参数(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)
当面板关闭时调用。 如果正在打开另一个逻辑后续面板(并且该面板正在关闭以为后面的面板腾出空间),则不会调用此方法。
| 参数(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)
准备要显示的面板。 在每次显示面板窗口之前调用它。
| 参数(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 ()
当用户指示开始搜索的愿望时调用。
| 返回(Returns) | |
|---|---|
boolean |
true if search launched, false if activity refuses (blocks) |
boolean onSearchRequested (SearchEvent event)
当用户指示开始搜索的愿望时调用。
| 参数(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 ()
当梦想被要求停止并唤醒时调用。
默认实现只需调用finish()即可立即结束梦想。 子类可以重写此函数以执行平滑的退出转换,然后再调用finish() 。
请注意,梦想只会在短时间内(现在约五秒钟)才会醒来。 如果梦想没有及时完成,那么一旦时间允许,系统将强制完成它。
void onWindowAttributesChanged (WindowManager.LayoutParams attrs)
这在每当当前窗口属性改变时被调用。
| 参数(Parameters) | |
|---|---|
attrs |
WindowManager.LayoutParams
|
void onWindowFocusChanged (boolean hasFocus)
这个钩子在窗口焦点改变时被调用。 有关更多信息,请参阅View.onWindowFocusChangedNotLocked(boolean) 。
| 参数(Parameters) | |
|---|---|
hasFocus |
boolean: Whether the window now has focus. |
ActionMode onWindowStartingActionMode (ActionMode.Callback callback)
当此窗口的动作模式正在启动时调用。 给回调提供了一个机会,以自己独特而美丽的方式处理动作模式。 如果此方法返回null,则系统可以选择一种呈现模式的方式或根本不选择启动模式。 这相当于onWindowStartingActionMode(android.view.ActionMode.Callback, int)和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)
当此窗口的动作模式正在启动时调用。 给回调提供了一个机会,以自己独特而美丽的方式处理动作模式。 如果此方法返回null,则系统可以选择一种呈现模式的方式或根本不选择启动模式。
| 参数(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)
将视图设置为此Dream的内容视图。 在活动中的行为与setContentView(android.view.View)行为类似,包括使用MATCH_PARENT作为视图的布局高度和宽度。
注意:这需要一个窗口,所以您通常应该在 onAttachedToWindow()期间调用它,并且从不提前(在 onCreate()期间 不能调用它)。
| 参数(Parameters) | |
|---|---|
view |
View
|
void setContentView (int layoutResID)
使布局资源膨胀并将其设置为此Dream的内容视图。 表现类似于setContentView(int) 。
注意:需要一个窗口,不要在 onAttachedToWindow()之前 onAttachedToWindow()
| 参数(Parameters) | |
|---|---|
layoutResID |
int: Resource ID to be inflated. |
void setContentView (View view, ViewGroup.LayoutParams params)
将视图设置为此Dream的内容视图。 在活动中与setContentView(android.view.View, android.view.ViewGroup.LayoutParams)行为类似。
注意:这需要一个窗口,所以通常应该在 onAttachedToWindow()期间调用它,并且从不提前(在 onCreate()期间 不能调用它)。
| 参数(Parameters) | |
|---|---|
view |
View: The desired content to display. |
params |
ViewGroup.LayoutParams: Layout parameters for the view. |
void setFullscreen (boolean fullscreen)
在梦想窗口上控制 FLAG_FULLSCREEN 。
| 参数(Parameters) | |
|---|---|
fullscreen |
boolean: If true, the fullscreen flag will be set; else it will be cleared. |
void setInteractive (boolean interactive)
将此梦想标记为交互式以接收输入事件。
非交互式梦想(默认)会在第一个输入事件中消失。
互动梦想应该叫 finish()解雇自己。
| 参数(Parameters) | |
|---|---|
interactive |
boolean: True if this dream will handle input events. |
void setScreenBright (boolean screenBright)
将这个梦想标志为在梦中保持屏幕亮度。
| 参数(Parameters) | |
|---|---|
screenBright |
boolean: True to keep the screen bright while dreaming. |
void wakeUp ()
轻轻唤醒梦。
致电onWakeUp()让梦想有机会执行退出过渡。 当转换结束时,梦想应该调用finish() 。
void dump (FileDescriptor fd, PrintWriter pw, String[] args)
将服务的状态打印到给定的流中。 如果您运行“adb shell dumpsys activity service <yourservicename>”(注意,要使此命令生效,服务必须正在运行,并且您必须指定完全限定的服务名称),则会调用此命令。 这与“dumpsys <servicename>”不同,后者仅适用于命名系统服务,并在使用ServiceManager注册的IBinder接口上调用dump(FileDescriptor, String[])方法。
| 参数(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. |