Android平台上的Appwidget是一个类似于windows下一些桌面工具的模块。它主要可以用来在Home 屏幕上显示一些快捷模块,比如:相册浏览、Calendar事件提醒。当然可以做更加复杂和有利于人机交互的模块,来提高用户体验。
Step 1:
看看新建一个Appwidget需要哪些步骤, 可以参考【1】中的介绍,毕竟是Google的开发文档,还是值得一看的。
大致上介绍了创建Appwidet之前需要了解的Appwidget的组成:
create an App Widget, you need the following:
- Describes the metadata for an App Widget, such as the App Widget's layout, update frequency, and the AppWidgetProvider class. This should be defined in XML.
- 描述widget中用到的metadata, 比如初始布局[initialLayout]、更新频率[updatePeriodMillis](这个据说在SDK1.6以后必须大于半个小时才有效,所以如果更新频率比较高,那就自己写一个更新线程)、以及它指定了你的AppWidgetProvider类。
- minWidth 和 minHeight 分别是这个widget的最小宽度 和 最小高度。度量的计算请参照引用【1】中提到的具体方法。
- 可以记住一个简单的公式= (number of cells * 74) - 2
- #configure 是☞一些widget需要在启动的时候配置参数,如果你的widget需要这个,可以创建对应的Activity。有时间继续研究下:)
-
- Defines the basic methods that allow you to programmatically interface with the App Widget, based on broadcast events. Through it, you will receive broadcasts when the App Widget is updated, enabled, disabled and deleted.
- 基于广播事件更新你的界面,通过它,可以得到 Widget更新、第一个对象被创建、无效化和移除的事件。
-
View layout (布局)
- Defines the initial layout for the App Widget, defined in XML.
- 在XML中定义你的widget布局。
-
- 引用文献中提到
-
Creating the App Widget layout is simple if you're familiar with Declaring Layout in XML. However, you must be aware that App Widget layouts are based on
RemoteViews
, which do not support every kind of layout or view widget.创建布局文件是很简单的,但是应该注意的是 App widget是基于RemoteViews的,所以并不是所有类型的layout在widget中都能支持。
以下是基于RemoteViews的布局类:
A RemoteViews object (and, consequently, an App Widget) can support the following layout classes:
以及基于这些类的一级扩展控件
And the following widget classes:
Descendants of these classes are not supported.
!!!(除以上类之外)这些类的子类是不被支持的。
AppWidgetProviderInfo
object
AppwidgetProviderInfo 对象 (这是一个xml文件,点击工程-New-Other...-Android XML File创建,在类型中选择
AppWidgetProvider)
![](http://hi.csdn.net/attachment/201011/30/0_1291107077CQcj.gif)
AppWidgetProvider
class implementation (AppwidgetProvider类的实现)
Step 2:
在了解完Appwidget的构成后,我们就可以开始创建第一个appwidget. 和创建Activity的方法是一样的。 创建完Activity后,修改AndroidManifest.xml
将<application ..></application> 字段中的子节点改成类似于
First, declare the AppWidgetProvider
class in your application's AndroidManifest.xml
file. For example:
<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 the android:name
attribute, which specifies the AppWidgetProvider
used by the App Widget.
<receiver>字段定义的是你的AppWidgetProvider类
The <intent-filter>
element must include an <action>
element with the android:name
attribute. This attribute specifies that the AppWidgetProvider
accepts theACTION_APPWIDGET_UPDATE
broadcast. This is the only broadcast that you must explicitly declare. The AppWidgetManager
automatically sends all other App Widget broadcasts to the AppWidgetProvider as necessary.
这个字段用来说明你的appwidget可以接收 android.appwidget.action.APPWIDGET_UPDATE 广播事件。AppWidegetManager将自动发送所有其他App Widget的广播给AppwidegetProvider类 (没弄懂它想说什么....)
The <meta-data>
element specifies the AppWidgetProviderInfo
resource and requires the following attributes:
android:name
- Specifies the metadata name. Useandroid.appwidget.provider
to identify the data as theAppWidgetProviderInfo
descriptor.android:resource
- Specifies theAppWidgetProviderInfo
resource location
这里是为了说明AppWidgetProviderInfo类和它对应的xml资源位置。
#####################################################################
以下是我做的一个例子,还没完成,跟各位需要的同学共享一下。
大致的目标是做一个HTC sense UI 里的时间空间==
布局文件:
AppwidgetProvider 类
工程文件:http://u.115.com/file/f59f6a69c7
参考资料:
1. App Widgets http://androidappdocs.appspot.com/guide/topics/appwidgets/index.html
2. Android 桌面组件【widget】初探 http://terryblog.blog.51cto.com/1764499/362909
3. Dateformat (有关时间的显示) http://androidappdocs.appspot.com/reference/android/text/format/DateFormat.html