AppWidget 学习总结

Android平台上的Appwidget是一个类似于windows下一些桌面工具的模块。它主要可以用来在Home 屏幕上显示一些快捷模块,比如:相册浏览、Calendar事件提醒。当然可以做更加复杂和有利于人机交互的模块,来提高用户体验。

 

Step 1:

看看新建一个Appwidget需要哪些步骤, 可以参考【1】中的介绍,毕竟是Google的开发文档,还是值得一看的。

大致上介绍了创建Appwidet之前需要了解的Appwidget的组成:

 

create an App Widget, you need the following:

AppWidgetProviderInfo object  AppwidgetProviderInfo 对象 (这是一个xml文件,点击工程-New-Other...-Android XML File创建,在类型中选择 AppWidgetProvider) adt会自动生成/xml/your_providerInfo.xml
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。有时间继续研究下:)

AppWidgetProvider class implementation (AppwidgetProvider类的实现)
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.

!!!(除以上类之外)这些类的子类是不被支持的。

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. Use android.appwidget.provider to identify the data as the AppWidgetProviderInfo descriptor.
  • android:resource - Specifies the AppWidgetProviderInfo 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

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值