标签:
1、首先,我们先阅读下官方文档,后面活给出Demo程序
App Widgets are miniature application views that can be embedded in other applications (such as the Home screen) and receive periodic(周期的,定期的) updates. These views are referred to as Widgets in the user interface, and you can publish one with an App Widget provider. An application component that is able to hold other App Widgets is called an App Widget host. The screenshot below shows the Music App Widget.
桌面应用小工具是一个迷你的应用程序视图,它可以被嵌入在其他的应用程序中(如主屏幕)接收定期的更新。在用户接口中这些被归类为窗口小部件,你可以发布一个应用程序窗口小部件提供者。一个能够去控制其他应用程序窗口小部件的应用程序组件被称为应用程序窗口小部件宿主(后面给出app widget host 的官方文档的解析)。下面的屏幕快照展示了一个音乐窗口小部件
This document describes how to publish an App Widget using an App Widget provider. For a discussion of creating your ownAppWidgetHostto host app widgets, see App Widget Host.
本文档描述了如何使用窗口小部件提供者去发布一个窗口小部件。讨论创建你自己的AppWidgetHost 去红纸窗口小部件。看 App Widget Host
Widget Design
窗口小部件设计
For information about how to design your app widget, read theWidgets design guide.
对于如何去设计你自己的窗口小部件程序,请阅读这Widget的设计向导
基本的
To create an App Widget, you need the following:
去创建一个窗口小部件程序,你需要以下的东西
Additionally, you can implement an App Widget configuration Activity. This is an optionalActivitythat launches when the user adds your App Widget and allows him or her to modify App Widget settings at create-time.
The following sections describe how to set up each of these components.
此外,你可以实现应用程序窗口小部件配置Activity。当用户添加你的应用程序窗口小部件并允许他或者她去修改应用程序窗口小部件创建时间的设置时这是一个可选的触发activity,
应用程序小部件Manifest文件的说明
First, declare theAppWidgetProviderclass in your application‘sAndroidManifest.xmlfile. For example:
第一,在你的应用程序AndroidManifest.xml文件中声明这个AppWidgetProvider 类,例如
<receiver android:name="ExampleAppWidgetProvider" > <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/example_appwidget_info" /> </receiver>
The<receiver>element requires theandroid:nameattribute, which specifies theAppWidgetProviderused by the App Widget.
这个<receiver>元素要求这个android:name属性,它制定了应用程序小部件所使用的AppWidgetProvider
The<intent-filter>element must include an<action>element with theandroid:nameattribute. This attribute specifies that theAppWidgetProvideraccepts theACTION_APPWIDGET_UPDATEbroadcast. This is the only broadcast that you must explicitly(明确的,明白的) declare. TheAppWidgetManagerautomatically sends all other App Widget broadcasts to the AppWidgetProvider as necessary.
这个<intent-filter>元素必须包括一个有android:name属性的<action>元素,这个属性制定AppWidgetProvider接收这ACTION_APPWIDGET_UPDATE广播,你必须唯一的明确的声明这个广播。这AppWidgetManager自动发送所有其他应用程序小部件的广播到AppWidgetProvider,这是必要的。
The<meta-data>element specifies theAppWidgetProviderInforesource and requires the following attributes:
这<meta-data>元素制定这AppWidgetProviderInfo 资源和要求以下的属性:
android:name制定这个元素据名称,用android.appwidget.provider去标识数据作为AppWidgetProviderInfo的描述符
android:resource指定这AppWidgetProviderInfo资源的位置
增加AppWidgetProviderInfo 元数据
(明天继续翻译)
TheAppWidgetProviderInfodefines the essential qualities of an App Widget, such as its minimum layout dimensions, its initial layout resource, how often to update the App Widget, and (optionally) a configuration Activity to launch at create-time. Define the AppWidgetProviderInfo object in an XML resource using a single<appwidget-provider>element and save it in the project‘sres/xml/folder.
For example:
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="40dp" android:minHeight="40dp" android:updatePeriodMillis="86400000" android:previewImage="@drawable/preview" android:initialLayout="@layout/example_appwidget" android:configure="com.example.android.ExampleAppWidgetConfigure" android:resizeMode="horizontal|vertical" android:widgetCategory="home_screen"> </appwidget-provider>
Here‘s a summary of the<appwidget-provider>attributes:
See the App Widget Design Guidelines for more information on sizing your App Widgets.
Note: To make your app widget portable across devices, your app widget‘s minimum size should never be larger than 4 x 4 cells.
See the App Widget Design Guidelines for more information on sizing your App Widgets.
Note: If the device is asleep when it is time for an update (as defined byupdatePeriodMillis), then the device will wake up in order to perform the update. If you don‘t update more than once per hour, this probably won‘t cause significant problems for the battery life. If, however, you need to update more frequently and/or you do not need to update while the device is asleep, then you can instead perform updates based on an alarm that will not wake the device. To do so, set an alarm with an Intent that your AppWidgetProvider receives, using theAlarmManager. Set the alarm type to eitherELAPSED_REALTIMEorRTC, which will only deliver the alarm when the device is awake. Then setupdatePeriodMillisto zero ("0").
See theAppWidgetProviderInfoclass for more information on the attributes accepted by the<appwidget-provider>element.
App Widget Host— The AppWidgetHost provides the interaction with the AppWidget service for apps, like the home screen, that want to embed app widgets in their UI. An AppWidgetHost must have an ID that is unique within the host‘s own package. This ID remains persistent across all uses of the host. The ID is typically a hard-coded value that you assign in your application.
窗口小部件宿主--这AppWidgetHost(例如主屏幕) 提供了与窗口小部件服务程序的交互,使窗口小部件程序能够嵌入在它的UI界面中。一个窗口小部件宿主在自己的包中必须有一个唯一的ID。ID将一直持久化在所有使用的主机里面。ID通常是一个你在应用程序中分配的硬编码的值。
标签:
原文地址:http://my.oschina.net/zaizaiangels/blog/531130