Widgets基础篇(下)

文章翻译整理自:<wbr style="line-height:22px"><a href="http://developer.android.com/guide/topics/appwidgets/index.html" target="_blank" rel="nofollow" style="color:rgb(207,121,28); line-height:22px; text-decoration:none">http://developer.android.com/guide/topics/appwidgets/index.html</a></wbr><wbr><div><strong><span style="font-size:16px">八,如何编写App Widget Configuration Activity</span></strong></div> <div> <span style="color:#003366">如果你想让用户在添加一个新的App Widget时,能对该App Widget进行一些个性化的配置的话,你可以通过编写一个</span><span style="color:#ff9900">App Widget Configuration Activity</span><span style="color:#003366">来实现。在用户添加一个新的App Widget时,</span><span style="color:#ff9900">configuration Activity能够自动被App Widget host启动</span><span style="color:#003366">,</span> </div> <div><span style="color:#003366">在该Activity中,你可以让用户对App Widget进行一些个性化的设置,比如颜色,大小,更新频率以及其他的一些设置。</span></div> <div> <span style="color:#003366">configuration Activity应该像一般的Activity一样,在Android manifest文件中进行申明。App Widget host是通过</span><span style="font-size:13px; line-height:16px; font-family:arial,sans-serif"><code style="color:rgb(0,0,255); line-height:1em; font-family:monospace"><a href="http://developer.android.com/reference/android/appwidget/AppWidgetManager.html#ACTION_APPWIDGET_CONFIGURE" rel="nofollow">ACTION_APPWIDGET_CONFIGURE</a></code><span style="color:#000080"></span></span><span style="font-size:13px; line-height:16px; font-family:arial,sans-serif"><span style="color:#003366">action 来启动configuration Activity</span></span><span style="font-size:13px; color:rgb(51,51,51); line-height:16px; font-family:arial,sans-serif">,</span><span style="color:rgb(0,51,102)">所以configuration Activity必须要能接收该Action.</span> </div> <div> <span style="color:#003366">比如,</span><strong>示例4</strong> </div> <div> <strong>示例4</strong><span style="color:#003366">:</span> </div> <div> <span style="font-size:13px; color:rgb(51,51,51); line-height:normal; font-family:arial,sans-serif"></span> <pre style="border-right:rgb(204,204,204) 1px solid; padding-right:10px; border-top:rgb(204,204,204) 1px solid; overflow-y:auto; padding-left:10px; overflow-x:auto; padding-bottom:10px; margin:0px 0px 1em 1em; border-left:rgb(204,204,204) 1px solid; color:rgb(0,112,0); padding-top:10px; border-bottom:rgb(204,204,204) 1px solid; font-family:monospace; background-color:rgb(250,250,250)"><span style="color:rgb(0,0,136)">&lt;activity</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(136,34,136)">android:name</span><span style="color:rgb(102,102,0)">=</span><span style="color:rgb(0,136,0)">".ExampleAppWidgetConfigure"</span><span style="color:rgb(0,0,136)">&gt;</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(0,0,136)">&lt;intent-filter&gt;</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(0,0,136)">&lt;action</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(136,34,136)">android:name</span><span style="color:rgb(102,102,0)">=</span><span style="color:rgb(0,136,0)">"android.appwidget.action.APPWIDGET_CONFIGURE"</span><span style="color:rgb(0,0,136)">/&gt;</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(0,0,136)">&lt;/intent-filter&gt;</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(0,0,136)">&lt;/activity&gt;</span></pre> </div> <div> <span style="color:#003366">同时在AppWidgetProviderInfo XML文件中,你也必须使用 </span><span style="color:#ff9900">android:configure</span><span style="color:#003366">属性中指明:当用户在添加一个新的App Widget时,哪个Configuration Activity将被启动。具体可以参照 </span><strong>示例5</strong> </div> <div><strong>示例5:</strong></div> <div> <span style="font-size:13px; color:rgb(51,51,51); line-height:normal; font-family:arial,sans-serif"></span> <pre style="border-right:rgb(204,204,204) 1px solid; padding-right:10px; border-top:rgb(204,204,204) 1px solid; overflow-y:auto; padding-left:10px; overflow-x:auto; padding-bottom:10px; margin:0px 0px 1em 1em; border-left:rgb(204,204,204) 1px solid; color:rgb(0,112,0); padding-top:10px; border-bottom:rgb(204,204,204) 1px solid; font-family:monospace; background-color:rgb(250,250,250)"><span style="color:rgb(0,0,136)">&lt;appwidget-provider</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(136,34,136)">xmlns:android</span><span style="color:rgb(102,102,0)">=</span><span style="color:rgb(0,136,0)">"http://schemas.android.com/apk/res/android"</span><span style="color:rgb(0,0,0)"> ... </span><span style="color:rgb(136,34,136)">android:configure</span><span style="color:rgb(102,102,0)">=</span><span style="color:rgb(0,136,0)">"com.example.android.ExampleAppWidgetConfigure"</span><span style="color:rgb(0,0,0)"> ... </span><span style="color:rgb(0,0,136)">&gt;</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(0,0,136)">&lt;/appwidget-provider&gt;</span></pre> </div> <div> <strong>注意:</strong><span style="color:#000080">这里的Activity用的是完整的名字,因为它将在你的APK包外被引用。</span> </div> <div><span style="color:#003366">以上就是启动一个Configuration Activity,所有需要你做的。现在你需要关心的是Activity本身,在实现一个Configuration Activity时,你需要记住两件非常重要的事情。</span></div> <div> <span style="color:#003366"><strong>首先</strong>,</span><span style="color:#000080">App Widget host调用configuration Activity,configuration Activity应该总是能返回一个执行结果。返回结果应该包含同过Intent传给configuration Activity的要添加的App Widget的ID(该ID通过<span style="font-size:13px; line-height:13px; font-family:monospace"><a href="http://developer.android.com/reference/android/appwidget/AppWidgetManager.html#EXTRA_APPWIDGET_ID" rel="nofollow">EXTRA_APPWIDGET_ID</a></span>保存在Intent的extras中)</span> </div> <div> <strong><span style="color:#003366">其次</span></strong><span style="color:#000080">,如果我们App Widget的有configuration Activity,那么当App Widget被创建时,AppWidgetProvider的onUpdate()方法将不会被调用,(当configuration Activity被创建启动的时候,系统将不再发送</span><span style="color:#ff9900">ACTION_APPWIDGET_UPDATE</span><span style="color:#000080">广播)。当App Widget被创建的时候,configuration Activity必须负责请求AppWidgetManager对App Widget进行首次更新。然而以后只要更新时间到了,系统还是会发送</span><span style="color:#ff9900">ACTION_APPWIDGET_UPDATE</span><span style="color:#000080">广播,因此App Widget的</span><span style="color:#0000ff">onUpdate()</span><span style="color:#000080">方法还是会被调用,以进行App Widget更新。系统只是在App Widget被创建的时候,不发送ACTION_APPWIDGET_UPDATE广播。</span> </div> <div><span style="color:#000080">在后面的文章,我们将讲述如何在configuration Activity中返回执行结果和更新App Widget.</span></div> <div><strong>九、如何在configuration Activity中更新App Widget和返回结果</strong></div> <div> <span style="color:#003366">configuration Activity必须负责请求AppWidgetManager对App Widget进行的首次更新。你可以通过</span><span style="font-size:13px; color:rgb(51,51,51); line-height:16px; font-family:arial,sans-serif"><code style="color:rgb(0,112,0); line-height:1em; font-family:monospace"><a href="http://developer.android.com/reference/android/appwidget/AppWidgetManager.html" rel="nofollow" style="color:rgb(0,102,153)">AppWidgetManager</a></code></span><span style="color:rgb(0,51,102)">直接来更新App Widget.</span> </div> <div> <span style="color:#003366">以下是</span><span style="color:rgb(0,0,128)">在configuration Activity中更新App Widget和退出</span><span style="color:rgb(0,0,128)">configuration Activity的主要步骤:</span> </div> <div> <span style="color:rgb(0,0,128)"><strong>第一、</strong>在启动</span><span style="color:#000080">configuration Activity的Intent中得到App Widget的ID。比如,</span><strong>示例6</strong><span style="color:#000080">.</span> </div> <div><strong>示例6:</strong></div> <div> <span style="font-size:13px; color:rgb(51,51,51); line-height:16px; font-family:arial,sans-serif"></span> <pre style="border-right:rgb(204,204,204) 1px solid; padding-right:10px; border-top:rgb(204,204,204) 1px solid; overflow-y:auto; padding-left:10px; overflow-x:auto; padding-bottom:10px; margin:0.5em 0px 0px 1em; border-left:rgb(204,204,204) 1px solid; color:rgb(0,112,0); padding-top:10px; border-bottom:rgb(204,204,204) 1px solid; font-family:monospace; background-color:rgb(250,250,250)"><span style="color:rgb(102,0,102)">Intent</span><span style="color:rgb(0,0,0)"> intent </span><span style="color:rgb(102,102,0)">=</span><span style="color:rgb(0,0,0)"> getIntent</span><span style="color:rgb(102,102,0)">();</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(102,0,102)">Bundle</span><span style="color:rgb(0,0,0)"> extras </span><span style="color:rgb(102,102,0)">=</span><span style="color:rgb(0,0,0)"> intent</span><span style="color:rgb(102,102,0)">.</span><span style="color:rgb(0,0,0)">getExtras</span><span style="color:rgb(102,102,0)">();</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(0,0,136)">if</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(102,102,0)">(</span><span style="color:rgb(0,0,0)">extras </span><span style="color:rgb(102,102,0)">!=</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(0,0,136)">null</span><span style="color:rgb(102,102,0)">)</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(102,102,0)">{</span><span style="color:rgb(0,0,0)"> mAppWidgetId </span><span style="color:rgb(102,102,0)">=</span><span style="color:rgb(0,0,0)"> extras</span><span style="color:rgb(102,102,0)">.</span><span style="color:rgb(0,0,0)">getInt</span><span style="color:rgb(102,102,0)">(</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(102,0,102)">AppWidgetManager</span><span style="color:rgb(102,102,0)">.</span><span style="color:rgb(0,0,0)">EXTRA_APPWIDGET_ID</span><span style="color:rgb(102,102,0)">,</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(102,0,102)">AppWidgetManager</span><span style="color:rgb(102,102,0)">.</span><span style="color:rgb(0,0,0)">INVALID_APPWIDGET_ID</span><span style="color:rgb(102,102,0)">);</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(102,102,0)">}</span></pre> </div> <div><span style="color:#000080"><strong>第二、</strong>进行App Widget配置的处理。</span></div> <div> <span style="color:#000080"><strong>第三、</strong>当App Widget的配置事务被处理完后,调用来</span><span style="font-size:13px; color:rgb(0,112,0); line-height:16px; font-family:monospace; white-space:pre"><span style="color:rgb(102,0,102)">AppWidgetManager</span><span style="color:rgb(102,102,0)">.</span><span style="color:rgb(0,0,0)">getInstance</span><span style="color:rgb(102,102,0)">(</span><span style="color:rgb(0,0,0)">context</span><span style="color:rgb(102,102,0)">)</span></span><span style="color:rgb(0,0,128)">得到AppWidgetManager的一个实例。</span> </div> <div> <span style="color:#000080">比如,</span><strong>示例7</strong><span style="color:#000080">.</span> </div> <div> <strong>示例7</strong><span style="color:#000080">:</span> </div> <div> <span style="font-size:13px; color:rgb(51,51,51); line-height:16px; font-family:arial,sans-serif"></span> <pre style="border-right:rgb(204,204,204) 1px solid; padding-right:10px; border-top:rgb(204,204,204) 1px solid; overflow-y:auto; padding-left:10px; overflow-x:auto; padding-bottom:10px; margin:0.5em 0px 0px 1em; border-left:rgb(204,204,204) 1px solid; color:rgb(0,112,0); padding-top:10px; border-bottom:rgb(204,204,204) 1px solid; font-family:monospace; background-color:rgb(250,250,250)"><span style="color:rgb(102,0,102)">AppWidgetManager</span><span style="color:rgb(0,0,0)"> appWidgetManager </span><span style="color:rgb(102,102,0)">=</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(102,0,102)">AppWidgetManager</span><span style="color:rgb(102,102,0)">.</span><span style="color:rgb(0,0,0)">getInstance</span><span style="color:rgb(102,102,0)">(</span><span style="color:rgb(0,0,0)">context</span><span style="color:rgb(102,102,0)">);</span></pre> </div> <div> <span style="color:#000080"><strong>第四、</strong>调用</span><span style="font-size:13px; color:rgb(51,51,51); line-height:16px; font-family:arial,sans-serif"><code style="color:rgb(0,112,0); line-height:1em; font-family:monospace"><a href="http://developer.android.com/reference/android/appwidget/AppWidgetManager.html#updateAppWidget(int,%20android.widget.RemoteViews)" rel="nofollow" style="color:rgb(0,102,153)">updateAppWidget(int, RemoteViews)</a></code>函数,</span><span style="font-size:13px; line-height:16px; font-family:arial,sans-serif"><span style="color:#000080">通过RemoteViews对象来更新App Widget.比如,</span><strong>示例8。</strong></span> </div> <div><span style="font-size:13px; line-height:20px; font-family:arial,sans-serif"><strong>示例8:</strong></span></div> <div> <span style="font-size:13px; color:rgb(51,51,51); line-height:16px; font-family:arial,sans-serif"></span> <pre style="border-right:rgb(204,204,204) 1px solid; padding-right:10px; border-top:rgb(204,204,204) 1px solid; overflow-y:auto; padding-left:10px; overflow-x:auto; padding-bottom:10px; margin:0.5em 0px 0px 1em; border-left:rgb(204,204,204) 1px solid; color:rgb(0,112,0); padding-top:10px; border-bottom:rgb(204,204,204) 1px solid; font-family:monospace; background-color:rgb(250,250,250)"><span style="color:rgb(102,0,102)">RemoteViews</span><span style="color:rgb(0,0,0)"> views </span><span style="color:rgb(102,102,0)">=</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(0,0,136)">new</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(102,0,102)">RemoteViews</span><span style="color:rgb(102,102,0)">(</span><span style="color:rgb(0,0,0)">context</span><span style="color:rgb(102,102,0)">.</span><span style="color:rgb(0,0,0)">getPackageName</span><span style="color:rgb(102,102,0)">(),</span><span style="color:rgb(0,0,0)"> R</span><span style="color:rgb(102,102,0)">.</span><span style="color:rgb(0,0,0)">layout</span><span style="color:rgb(102,102,0)">.</span><span style="color:rgb(0,0,0)">example_appwidget</span><span style="color:rgb(102,102,0)">);</span><span style="color:rgb(0,0,0)"> appWidgetManager</span><span style="color:rgb(102,102,0)">.</span><span style="color:rgb(0,0,0)">updateAppWidget</span><span style="color:rgb(102,102,0)">(</span><span style="color:rgb(0,0,0)">mAppWidgetId</span><span style="color:rgb(102,102,0)">,</span><span style="color:rgb(0,0,0)"> views</span><span style="color:rgb(102,102,0)">);</span></pre> </div> <div><span style="font-size:13px; line-height:20px; font-family:arial,sans-serif"><span style="color:#000080">第五、把执行结果放在Intent,用Intent返回结果,并结束configuration Activity。比如,</span><strong>示例9</strong><span style="color:#000080">。</span></span></div> <div><span style="font-size:13px; line-height:20px; font-family:arial,sans-serif"><strong style="line-height:20px">示例9:</strong></span></div> <div> <span style="font-size:13px; color:rgb(51,51,51); line-height:16px; font-family:arial,sans-serif"></span> <pre style="border-right:rgb(204,204,204) 1px solid; padding-right:10px; border-top:rgb(204,204,204) 1px solid; overflow-y:auto; padding-left:10px; overflow-x:auto; padding-bottom:10px; margin:0px 0px 1em 1em; border-left:rgb(204,204,204) 1px solid; color:rgb(0,112,0); padding-top:10px; border-bottom:rgb(204,204,204) 1px solid; font-family:monospace; background-color:rgb(250,250,250)"><span style="color:rgb(102,0,102)">Intent</span><span style="color:rgb(0,0,0)"> resultValue </span><span style="color:rgb(102,102,0)">=</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(0,0,136)">new</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(102,0,102)">Intent</span><span style="color:rgb(102,102,0)">();</span><span style="color:rgb(0,0,0)"> resultValue</span><span style="color:rgb(102,102,0)">.</span><span style="color:rgb(0,0,0)">putExtra</span><span style="color:rgb(102,102,0)">(</span><span style="color:rgb(102,0,102)">AppWidgetManager</span><span style="color:rgb(102,102,0)">.</span><span style="color:rgb(0,0,0)">EXTRA_APPWIDGET_ID</span><span style="color:rgb(102,102,0)">,</span><span style="color:rgb(0,0,0)"> mAppWidgetId</span><span style="color:rgb(102,102,0)">);</span><span style="color:rgb(0,0,0)"> setResult</span><span style="color:rgb(102,102,0)">(</span><span style="color:rgb(0,0,0)">RESULT_OK</span><span style="color:rgb(102,102,0)">,</span><span style="color:rgb(0,0,0)"> resultValue</span><span style="color:rgb(102,102,0)">);</span><span style="color:rgb(0,0,0)"> finish</span><span style="color:rgb(102,102,0)">();</span></pre> </div> <div> <span style="font-family:arial,sans-serif; font-size:12px"><span style="line-height:20px"><strong>注意</strong>:<span style="color:#000080">当</span></span></span><span style="color:#000080">configuration Activity首次启动的时候,应该把Activity的返回结果设置为RESULT_CANCELED.</span> </div> <div><span style="color:#000080">这样如果用户中途退出了configuration Activity的话,系统将通知App Widget host该configuration被取消,这样App Widget将不被添加</span></div> </wbr>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值