Widgets基础篇(上)

文章参照自:<wbr><a href="http://developer.android.com/guide/topics/appwidgets/index.html" target="_blank" rel="nofollow">http://developer.android.com/guide/topics/appwidgets/index.html</a> <div> <div><strong><span style="font-size:16px">一、前言</span></strong></div> <div><span style="color:#003366">所谓App Widgets就是微型应用程序的意思,它可以嵌入在其他应用程序(如主屏幕),并能定期更新其View。</span></div> <div><span style="color:#003366">这些View被当成用户界面的小部件,您可以使用App Widget provider来发布App Widgets。</span></div> <div> <span style="color:#003366">一个能容纳其他的App Widgets的应用程序的组件,我们称之为App Widget host。</span><strong>图1</strong><span style="color:#003366">就是一个音乐App Widget的截图。</span> </div> <div><strong>图1</strong></div> </div> <div> <div style="font-weight:bold"><img alt="Widgets基础篇(上) - hubingforever - 民主与科学" src="http://developer.android.com/images/appwidget.png" style="margin:0px 10px 0px 0px"></div> <span style="color:#000080">在下面的文章中,我们将讲述如何使用App Widget provider来发布App Widget</span> </div> <div><strong><span style="font-size:16px">二、基本原理</span></strong></div> <div><span style="color:#003366">为了创建一个App Widget,你需要以下三部分:</span></div> <div><strong>AppWidgetProviderInfo</strong></div> <div><span style="color:#000080">AppWidgetProviderInfo用于对App Widgets的元数据(metadata)进行描述,如App Widgets的布局,更新频率,和AppWidgetProvider类。它应该是在XML中定义。</span></div> <div><strong>AppWidgetProvider</strong></div> <div> <div style="color:rgb(0,0,128)">AppWidgetProvider定义了一些基本方法,通过这些方法你可以很方便和App Widget进行交互。</div> <div style="color:rgb(0,0,128)">AppWidgetProvider基于广播事件。当App Widget进行更新,启用,禁用和删除时,在AppWidgetProvider中,您将收到其对应的广播。</div> <div><strong>视图布局文件</strong></div> <div style="color:rgb(0,0,128)">为了让App Widget能进行显示,我们还需要为App Widget的提供一个布局文件</div> <div><span style="color:#003366">另外,你还可以实现一个用于对App Widget进行配置的Activity.该Activity是可选的,当用户添加App Widget时,该Acitivity将被启动。通过它可以在App Widget被创建时,做一些对App Widget的设置。这里的设置是指和App Widget的事务相关的设置,不是指AppWidgetProviderInfo的内容。</span></div> </div> <div><strong><span style="font-size:16px">三、在Manifest中声明App Widget</span></strong></div> <div> <span style="color:#003366">首先,需要在你的应用程序的</span><span style="color:#0000ff">AndroidManifest.xm</span><span style="color:#003366">文件中声明一个</span><span style="font-family:monospace"><a href="http://developer.android.com/reference/android/appwidget/AppWidgetProvider.html" rel="nofollow">AppWidgetProvider</a></span><span style="color:#003366">类,</span><span style="font-size:12px">比如<strong>示例1.</strong></span> </div> <div><span style="font-size:12px"><span style="line-height:20px"><strong>示例1</strong></span></span></div> <div> <div style="font-size:12px; color:rgb(0,0,255)">&lt;receiver android:name="ExampleAppWidgetProvider" &gt;</div> <div style="font-size:12px; color:rgb(0,0,255)"> &lt;intent-filter&gt;</div> <div style="font-size:12px; color:rgb(0,0,255)"> &lt;action android:name="android.appwidget.action.APPWIDGET_UPDATE" /&gt;</div> <div style="font-size:12px; color:rgb(0,0,255)"> &lt;/intent-filter&gt;</div> <div style="font-size:12px; color:rgb(0,0,255)"> &lt;meta-data android:name="android.appwidget.provider"</div> <div style="font-size:12px; color:rgb(0,0,255)"> android:resource="@xml/example_appwidget_info" /&gt;</div> <div style="font-size:12px; color:rgb(0,0,255)">&lt;/receiver&gt;</div> <div style="font-size:12px"> <div><span style="color:#000080">&lt;receiver&gt;元素的android:name属性必须要进行设置, 该属性说明了我将使用哪个AppWidgetProvidere来提供App Widget。</span></div> <div><span style="color:#000080">&lt;intent-filter&gt;元素必须包含android:name属性为"android.appwidget.action.APPWIDGET_UPDATE"的Action,</span></div> <div><span style="color:#000080">即&lt;action android:name="android.appwidget.action.APPWIDGET_UPDATE" /&gt;。该属性说明了AppWidgetProvider可以接收<a href="http://developer.android.com/reference/android/appwidget/AppWidgetManager.html#ACTION_APPWIDGET_UPDATE" target="_blank" rel="nofollow">ACTION_APPWIDGET_UPDATE</a>广播。</span></div> <div><span style="color:#000080">该广播是唯一你必须要声明接受的广播。<a href="http://developer.android.com/reference/android/appwidget/AppWidgetManager.html" target="_blank" rel="nofollow">AppWidgetManager</a>能自动的把其他所有的App Widget广播发送到AppWidgetProvider</span></div> </div> <div style="font-size:12px"> <div style="color:rgb(0,0,128)">在&lt;meta-data&gt;元素中,必须要指定AppWidgetProviderInfo资源文件,必须要定义以下2个属性:</div> <div> <span style="color:#000080"> </span> <strong><span style="color:#ff6600">* android:name</span></strong><span style="color:#000080"> - 它用于定义metadata元素的名字. 必须把该属性设置为“android.appwidget.provider”以表明该&lt;meta-data&gt;元素是用于描述AppWidgetProviderInfo资源文件位置的.</span> </div> <div> <span style="color:#0080"> <strong> </strong></span><span style="color:#ff6600"><strong>* android:resource</strong></span><span style="color:#000080"><strong></strong>-该属性用于说明AppWidgetProviderInfo资源文件的位置。</span> </div> </div> <div><strong><span style="font-size:16px">四、编写AppWidgetProviderInfo配置文件</span></strong></div> <div> <div style="font-size:12px; color:rgb(0,0,128)">AppWidgetProviderInfo用于定义App Widget的基本属性,如显示的最小尺寸,其初始布局资源,更新频率,</div> <div style="font-size:12px; color:rgb(0,0,128)">和(可选)configuration Activity,该Activity将在其App Widget被创建时被启动。</div> <div style="font-size:12px; color:rgb(0,0,128)">AppWidgetProviderInfo的定义必须在一个只有单一的&lt;appwidget-provider&gt;元素的XML资源文件中进行,该文件必须放在res/xml目录下。</div> <div style="font-size:12px"> <strong>示例2</strong><span style="color:#000080">:</span> </div> <div style="font-size:12px"> <div><span style="color:#0000ff">&lt;appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"</span></div> <div><span style="color:#0000ff"> android:minWidth="294dp"</span></div> <div><span style="color:#0000ff"> android:minHeight="72dp"</span></div> <div><span style="color:#0000ff"> android:updatePeriodMillis="86400000"</span></div> <div><span style="color:#0000ff"> android:previewImage="@drawable/preview"</span></div> <div><span style="color:#0000ff"> android:initialLayout="@layout/example_appwidget"</span></div> <div><span style="color:#0000ff"> android:configure="com.example.android.ExampleAppWidgetConfigure"</span></div> <div><span style="color:#0000ff"> android:resizeMode="horizontal|vertical"&gt;</span></div> <div><span style="color:#0000ff">&lt;/appwidget-provider&gt;</span></div> </div> <div><span style="font-size:12px"><span style="line-height:20px"><span style="color:#000080">以下是关于</span><span style="color:#ff6600">&lt;appwidget-provider&gt;</span>一些<span style="color:#000080">属性的介绍:</span></span></span></div> </div> </div> <div> <span style="font-size:12px; color:#ff6600"><strong>minWidth</strong></span><span style="font-size:12px; color:#000080">和</span><span style="font-size:12px; color:#ff6600"><strong>minHeight</strong></span><span style="font-size:12px; color:#000080">属性用于说明App Widget在屏幕上至少要占用多大的空间 。</span> </div> <div><span style="font-size:12px; color:#000080">默认的Home screen在放置App Widgets时,是以网格为基本单位,而不是以像素,这里的网格是指拥有一定的像素的长方形区域。</span></div> <div><span style="font-size:12px; color:#000080">因为home屏幕的布局方向可能改变(网格的大小就会随之改变),所以你应该考虑最坏的情况,此时网格拥有74个像素的长和高。</span></div> <div><span style="font-size:12px; color:#000080">然而你必须减去2个像素,以便避免在计算网格数量时,对像素进行取整,发生错误。</span></div> <div><span style="font-size:12px"><span style="color:#000080">你应该使用下面的公式计算</span><span style="font-size:12px; color:#00ff; line-height:20px">android:minWidth</span><span style="font-size:12px; color:#0080; line-height:20px">和</span><span style="font-size:12px; color:#00ff; line-height:20px">android:minHeight</span><span style="font-size:12px; line-height:20px"><span style="color:#000080">:</span></span></span></div> <div><span style="font-size:12px; color:#000080"><span style="line-height:20px">(number of cells * 74) - 2</span></span></div> <div><span style="font-size:12px"><span style="color:#000080"><span style="line-height:20px">根据上面的公式,如果想你的App Widget占用</span></span><span style="font-size:12px; color:#0080; line-height:20px">4个网格的宽,</span><span style="color:#000080"><span style="line-height:20px">1个网格的高话,那么</span></span><span style="font-size:12px; color:#00ff; line-height:20px">android:minWidth</span><span style="font-size:12px; color:#0080; line-height:20px">和</span><span style="font-size:12px; color:#00ff; line-height:20px">android:minHeigh</span><span style="font-size:12px; line-height:20px"><span style="color:#000080">t应该分别为</span></span><span style="font-size:12px; color:#00ff; line-height:20px">"294dp"</span><span style="font-size:12px; line-height:20px"><span style="color:#000080">和</span></span><span style="font-size:12px; color:#00ff; line-height:20px">"72dp"</span></span></div> <div> <span style="font-size:12px; color:#000080"><span style="line-height:20px">注意:为了让你的App Widget能在各类平台上运行,你的App Widget不要超过4X4网格的尺寸。关于App Widget的设计的更多内容请参考</span></span><a href="http://developer.android.com/guide/practices/ui_guidelines/widget_design.html#sizes" rel="nofollow">App Widget Design Guidelines</a> </div> <div> <strong><span style="color:#ff6600">updatePeriodMillis</span></strong><span style="color:#000080">属性用于说明App Widget框架请求</span><span style="color:#0000ff">AppWidgetProvider</span><span style="color:#000080">的</span><span style="color:#0000ff">onUpdate()</span><span style="color:#000080">方法来更新</span><span style="color:#0000ff">App Widget</span><span style="color:#000080">的频率。但是这个频率是无法完全保证的,我们建议尽量减少更新的频率。有时可能一小时才更新一次,以便节约电池。你也可以提供一个配置,让用户自己设置更新的频率。有些人可能想15分钟就更新股票的价格,而有些人仅想1天就更新股票4次。</span> </div> <div> <span style="color:#000080">注意:如果手机在处于休眠状态时,而对App Widget进行更新的时间有到了,这时设备将醒来以便进行App Widget的更新。如果更新频率低于一个小时一次的话,并不会引起明显的电池消耗。如果你的更新非常频繁或你不想手机在处于休眠时还进行App Widget更新的话,请通过一个alarm来进行更新。你可以用</span><code><a href="http://developer.android.com/reference/android/app/AlarmManager.html" rel="nofollow">AlarmManager</a></code><span style="color:#0080">来设置一个定期发送</span><span style="color:#00ff">AppWidgetProvider</span><span style="color:#000080">的Intent的Alarm,且把Alarm的类型设置为</span><code><a href="http://developer.android.com/reference/android/app/AlarmManager.html#ELAPSED_REALTIME" rel="nofollow">ELAPSED_REALTIME</a></code> or <code><a href="http://developer.android.com/reference/android/app/AlarmManager.html#RTC" rel="nofollow">RTC</a>,<span style="color:#000080">这两种类型的Alarm只有在系统处于awake状态才会发送。另外此时,要把</span></code><span style="font-size:12px"><span style="font-size:12px; color:#00ff; line-height:20px">android:updatePeriodMillis</span><span style="font-size:12px; line-height:20px"><span style="color:#000080">设置为</span></span><span style="font-size:12px; color:#00ff; line-height:20px">"0"</span></span> </div> <div><span style="line-height:20px"><span style="font-size:12px"><span style="color:#ff660"><strong>initialLayout</strong></span><span style="color:#000080">属性用于设置App Widget的布局文件。</span></span></span></div> <div><span style="font-size:12px"><span style="color:#000080">configure属性用于说明在App Widget在被添加到Home Screen时,哪个configure Activity将首先启动。这是一个可选属性,关于次的更多内容请阅读后文。</span></span></div> <div><span style="font-size:12px"><span style="color:#ff6600"><strong>previewImage</strong></span><span style="color:#000080">属性在Android 3.0版本中才被添加,它用于指明 App Widget的预览图片,它用于在用户选中该App Widget的图标,打算添加该App Widget时,进行显示,以便用户了解该App Widget的界面。如果没提供预览图标的话,显示的将是你的App Widget的启动图标。该属性和AndroidManifest.xml中的&lt;receiver&gt;元素的android:previewImage的属性一致。关于此的更多内容请参照后文。</span></span></div> <div><span style="font-size:12px"><span style="color:#000080">The </span><strong><span style="color:#ff6600">autoAdvanceViewId</span></strong><span style="color:#000080"> attribute specifies the view ID of the app widget subview that should be auto-advanced by the widget's host. Introduced in </span><span style="color:#0000ff">Android 3.0</span><span style="color:#000080">.</span></span></div> <div><span style="font-size:12px"><span style="color:#ff6600"><strong>resizeMode</strong></span><span style="color:#000080">属性在Android 3.1中才被添加,它用于说明App Widget重新调整大小的规则。通过该属性,你可以设置在什么方向允许调整App Widget的大小,可以是垂直、水平、或同时垂直和水平两个方向。用户可以按住App Widget来显示大小调整handle,通过在水平或垂直方向拖动handle来调整</span></span></div> <div><span style="font-size:12px"><span style="color:#000080">App Widget在垂直或水平方向的尺寸。</span><span style="color:#ff6600">resizeMode</span><span style="color:#000080">属性的值可以是"</span><span style="color:#0000ff">horizontal</span><span style="color:#000080">", "</span><span style="color:#0000ff">vertical</span><span style="color:#000080">", "</span><span style="color:#0000ff">none</span><span style="color:#000080">"和"</span><span style="color:#0000ff">horizontal</span>|<span style="color:#0000ff">vertical</span><span style="color:#000080">"</span></span></div> <div><span style="font-size:12px"><span style="line-height:20px"><span style="color:#ff6600"><strong>icon</strong></span><span style="color:#000080">属性用于说明你的AppWidget在AppWidget picker列表中显示的图标,它应该和AndroidManifest.xml中的&lt;receiver&gt;元素的android:</span><span style="color:#ff6600">icon</span><span style="color:#000080">的属性一致</span></span></span></div> <div><span style="font-size:12px"><strong><span style="color:#ff6600">label</span></strong><span style="color:#000080">属性用于说明你的AppWidget在AppWidget picker列表中显示的名字,它应该和AndroidManifest.xml中的&lt;receiver&gt;元素的android:</span><span style="color:#ff6600">label</span><span style="color:#000080">的属性一致。</span></span></div> <div> <span style="font-size:12px"><span style="color:#000080">关于</span><span style="font-size:12px; line-height:20px"><span style="color:#ff6600; line-height:20px">&lt;appwidget-provider&gt;</span><span style="color:#000080; line-height:20px">属性的更多内容请参照</span></span></span><span style="font-family:monospace"><a href="http://developer.android.com/reference/android/appwidget/AppWidgetProviderInfo.html" rel="nofollow">AppWidgetProviderInfo</a></span> </div> <div><span style="font-size:16px"><strong>五、编写App Widget布局文件</strong></span></div> <div> <span style="font-size:12px"><span style="color:#000080">你必须在XML文件中定义你的App Widget的布局文件,并把它保存在res/layout/目录下。你可以在 App Widget中使用如下的View,但是请你在开始你的App Widget的布局时请阅读</span></span><a href="http://developer.android.com/guide/practices/ui_guidelines/widget_design.html" rel="nofollow">App Widget Design Guidelines</a>.</div> <div> <span style="font-size:12px; color:#0080; line-height:20px"><span style="font-size:12px">如果你熟悉在XML中如何进行布局文件的编写,那么编写App Widget布局文件将非常的简单。因为App Widget的布局是基于</span></span><span style="font-family:monospace"><a href="http://developer.android.com/reference/android/widget/RemoteViews.html" rel="nofollow">RemoteViews</a>对象,</span><span style="font-size:12px; line-height:20px"><span style="font-size:12px; color:#000080">所以它并不能支持所有的View.</span></span> </div> <div><span style="font-size:12px; color:#000080"><span style="line-height:20px">RemoteViews对象支持如下的一些View及其子类:</span></span></div> <div> <span style="font-size:12px; color:#000080"><span style="line-height:20px"></span></span> <ul> <li><code><a href="http://developer.android.com/reference/android/widget/FrameLayout.html" rel="nofollow">FrameLayout</a></code></li> <li><code><a href="http://developer.android.com/reference/android/widget/LinearLayout.html" rel="nofollow">LinearLayout</a></code></li> <li><code><a href="http://developer.android.com/reference/android/widget/RelativeLayout.html" rel="nofollow">RelativeLayout</a></code></li> </ul> <ul> <li><code><a href="http://developer.android.com/reference/android/widget/AnalogClock.html" rel="nofollow">AnalogClock</a></code></li> <li><code><a href="http://developer.android.com/reference/android/widget/Button.html" rel="nofollow">Button</a></code></li> <li><code><a href="http://developer.android.com/reference/android/widget/Chronometer.html" rel="nofollow">Chronometer</a></code></li> <li><code><a href="http://developer.android.com/reference/android/widget/ImageButton.html" rel="nofollow">ImageButton</a></code></li> <li><code><a href="http://developer.android.com/reference/android/widget/ImageView.html" rel="nofollow">ImageView</a></code></li> <li><code><a href="http://developer.android.com/reference/android/widget/ProgressBar.html" rel="nofollow">ProgressBar</a></code></li> <li><code><a href="http://developer.android.com/reference/android/widget/TextView.html" rel="nofollow">TextView</a></code></li> <li><code><a href="http://developer.android.com/reference/android/widget/ViewFlipper.html" rel="nofollow">ViewFlipper</a></code></li> </ul> </div> </wbr>

你可能感兴趣的:(widget)