`
kanwoerzi
  • 浏览: 1646054 次
文章分类
社区版块
存档分类
最新评论

Widgets高级篇(三)

阅读更多
本文翻译整理自:<wbr style="line-height:25px"><span style="line-height:25px; font-family:Arial,Helvetica,simsun,u5b8bu4f53"><a rel="nofollow" href="http://developer.android.com/guide/topics/appwidgets/index.html#implementing_collections" style="color:rgb(207,121,28); line-height:22px; text-decoration:none">http://developer.android.com/guide/topics/appwidgets/index.html#implementing_collections</a></span> <div style="line-height:25px"><span style="line-height:25px">3.6、设置数据项View的行为</span></div> <div style="line-height:25px"><span style="color:#003366; line-height:25px">前文已经告诉你如何绑定您的数据到你的app widget collection。但是如何为数据项View添加动态的行为呢?本部分将讲述关于次的详细内容。</span></div> <div style="line-height:25px"> <span style="color:#003366; line-height:25px">在普通的App Widgets中,我们可以通过</span><span style="line-height:13px; color:rgb(0,112,0); font-family:monospace; font-size:13px"><a rel="nofollow" href="http://developer.android.com/reference/android/widget/RemoteViews.html#setOnClickPendingIntent(int,%20android.app.PendingIntent)" style="color:rgb(0,102,153); line-height:23px; text-decoration:none">setOnClickPendingIntent()</a></span><span style="line-height:25px; color:rgb(0,51,102)">来设置View控件Click时,发送的Intent.通过次方法,我们就可以实现当用户点击一个按钮时,就启动一个Activity.但是这种方法对</span><span style="line-height:25px; color:rgb(0,51,102)">app widget collection的数据集的数据项View并不适用(</span><span style="color:#000080; line-height:25px">你可以像Gmail app widget中一样通过setOnClickPendingIntent()来设置global button的click行为来启动一个应用程序, 但是对数据集的数据项View不能使用这种方法</span><span style="line-height:25px; color:rgb(0,51,102)">)。替代的是首先使用</span><span style="line-height:13px; font-family:monospace; font-size:13px"><a rel="nofollow" href="http://developer.android.com/reference/android/widget/RemoteViews.html#setOnClickFillInIntent(int,%20android.content.Intent)" style="color:rgb(0,102,153); line-height:23px; text-decoration:none">setOnClickFillInIntent()</a><span style="color:#000080; line-height:23px">方法为</span></span><span style="line-height:25px; color:rgb(0,0,128)">数据集的数据项View统一设置其Pending的Intent模板,然后在</span><span style="line-height:13px; font-family:monospace; font-size:13px"><a rel="nofollow" href="http://developer.android.com/reference/android/widget/RemoteViewsService.RemoteViewsFactory.html" style="color:rgb(0,102,153); line-height:23px; text-decoration:none">RemoteViewsFactory</a><span style="color:#000080; line-height:23px">中为数据集的数据项View的Pending的Intent模板的设置填充Intent。这样当数据项View在Click时,发送的Intent将是其</span></span><span style="line-height:20px; color:rgb(0,0,128); font-family:monospace; font-size:13px">Pending的Intent模板和其</span><span style="line-height:20px; color:rgb(0,0,128); font-family:monospace; font-size:13px">Pending的Intent模板填充Intent合成的Intent。</span> </div> <div style="line-height:25px"><span style="line-height:20px; color:rgb(0,0,128); font-family:monospace; font-size:13px"><br style="line-height:23px"></span></div> <div style="line-height:25px"> <span style="line-height:20px; color:rgb(0,0,128); font-family:monospace; font-size:13px">在这里我们将使用</span><span style="line-height:25px; color:rgb(0,0,128); font-family:Arial,Helvetica,simsun,u5b8bu4f53"><a target="_blank" href="http://hubingforever.blog.163.com/blog/static/17104057920116293109787/" style="color:rgb(207,121,28); line-height:22px; text-decoration:none">StackView Widget示例程序</a>来说明如何为数据项View添加Click行为。在</span><span style="line-height:25px; color:rgb(0,0,128); font-family:Arial,Helvetica,simsun,u5b8bu4f53"><a target="_blank" href="http://hubingforever.blog.163.com/blog/static/17104057920116293109787/" style="color:rgb(207,121,28); line-height:22px; text-decoration:none">StackView Widget示例程序</a></span><span style="line-height:25px; color:rgb(0,0,128); font-family:Arial,Helvetica,simsun,u5b8bu4f53">中,当你单击一个数据项View,它将显示一个</span><span style="line-height:16px; color:rgb(51,51,51); font-family:arial,sans-serif; font-size:13px"><code style="line-height:1em; color:rgb(0,112,0); font-family:monospace"><a rel="nofollow" href="http://developer.android.com/reference/android/widget/Toast.html" style="color:rgb(0,102,153); line-height:23px; text-decoration:none">Toast</a>消息</code>"Touched view<em>n</em>,"</span><span style="line-height:25px; color:rgb(0,0,128); font-family:Arial,Helvetica,simsun,u5b8bu4f53">n是它在touched view中的 index (position)。其主要执行流程如下:</span> </div> <div style="line-height:25px"> <span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:#000080; line-height:25px"><span style="line-height:25px">第一步:</span>首先,在StackWidgetProvider(an AppWidgetProvider subclass)中</span><span style="line-height:22px"><span style="color:#000080; line-height:25px">使用</span></span><span style="line-height:13px; font-family:monospace; font-size:13px"><a rel="nofollow" href="http://developer.android.com/reference/android/widget/RemoteViews.html#setOnClickFillInIntent(int,%20android.content.Intent)" style="color:rgb(0,102,153); line-height:20px; text-decoration:none">setOnClickFillInIntent()</a><span style="color:#000080; line-height:20px">方法为</span></span><span style="line-height:22px; color:rgb(0,0,128)">数据集的数据项View统一设置其Pending的Intent模板</span><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:#000080; line-height:25px">,该intent有一个自定义的名叫</span><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:#0000ff; line-height:25px">TOAST_ACTION</span><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:#000080; line-height:25px">的action.</span><span style="line-height:25px; color:rgb(0,0,128); font-family:Arial,Helvetica,simsun,u5b8bu4f53">在RemoteViewsFactory中为数据集的数据项View的Pending的Intent模板的设置填充Intent,</span><span style="line-height:25px; color:rgb(0,0,128); font-family:Arial,Helvetica,simsun,u5b8bu4f53">填充Intent通过</span><span style="line-height:20px; font-family:monospace; font-size:13px; white-space:pre"><span style="color:#0000ff; line-height:23px">Extras</span></span><span style="line-height:20px; font-family:monospace; font-size:13px; white-space:pre"><span style="color:#000080; line-height:23px">把</span></span><span style="color:#ff9900; line-height:25px"><span style="line-height:25px; font-family:Arial,Helvetica,simsun,u5b8bu4f53">数据项View的</span><span style="line-height:25px; font-family:Arial,Helvetica,simsun,u5b8bu4f53">index (position</span></span><span style="line-height:25px; color:rgb(0,0,128); font-family:Arial,Helvetica,simsun,u5b8bu4f53">)携带在其中</span><span style="line-height:25px; color:rgb(0,0,128); font-family:Arial,Helvetica,simsun,u5b8bu4f53">。</span> </div> <div style="line-height:25px"> <span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:#000080; line-height:25px"><span style="line-height:25px">第二步</span>、用户点击数据项View,</span><span style="line-height:25px; color:rgb(0,0,128); font-family:Arial,Helvetica,simsun,u5b8bu4f53">其Pending的Intent模板和其Pending的Intent模板填充Intent被合成一个</span><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:#000080; line-height:25px">Intent,该Intent被激活并被发送。</span> </div> <div style="line-height:25px"> <span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; line-height:25px"></span> <div style="line-height:25px"> <span style="line-height:25px; color:rgb(0,0,128)">第三步</span><span style="color:#000080; line-height:25px">、数据项View发送的Intent广播被</span><span style="color:#0000ff; line-height:25px">StackWidgetProvider</span><span style="color:#000080; line-height:25px">接收,被传递到它的onReceive()中。</span> </div> <div style="line-height:25px"> <span style="line-height:25px; color:rgb(0,0,128)">在</span><span style="color:#0000ff; line-height:25px">onReceive()</span><span style="color:#000080; line-height:25px">方法中,我们从Intent的Extra中提取其</span><span style="color:#ff9900; line-height:25px">数据项view的index (position)</span><span style="color:#000080; line-height:25px">,然后发送Toast消息。</span> </div> <span style="line-height:25px; font-family:Arial,Helvetica,simsun,u5b8bu4f53"><span style="line-height:25px">注意</span><span style="color:#000080; line-height:25px">:</span><a target="_blank" href="http://hubingforever.blog.163.com/blog/static/17104057920116293109787/" style="color:rgb(207,121,28); line-height:22px; text-decoration:none">StackView Widget示例程序</a><span style="color:#000080; line-height:25px">中使用的是把Intent发送到广播</span>(</span><span style="line-height:20px; color:rgb(102,0,102); font-family:monospace; font-size:13px; white-space:pre">StackWidgetProvider</span><span style="line-height:25px; font-family:Arial,Helvetica,simsun,u5b8bu4f53">),<span style="color:#000080; line-height:25px">但是一般的app widget只是简单的启动一</span></span> </div> <div style="line-height:25px"><span style="line-height:25px; font-family:Arial,Helvetica,simsun,u5b8bu4f53"><span style="color:#000080; line-height:25px">个Activity</span></span></div> <div style="line-height:25px"><span style="line-height:25px; font-family:Arial,Helvetica,simsun,u5b8bu4f53"><span style="line-height:25px">3.6.1、设置数据项View的pending intent模板</span></span></div> <div style="line-height:25px"></div> <div style="line-height:25px"></div> <div style="line-height:25px"> <span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; line-height:25px"></span> <div style="line-height:25px"> <div style="line-height:25px"><span style="color:#003366; line-height:25px">在StackWidgetProvider (AppWidgetProvider subclass),数据项View的pending intent模板被统一进行设置.</span></div> <div style="line-height:25px"><span style="color:#003366; line-height:25px">不能对单个的数据项View的进行pending intent模板.你必须对数据集的数据项View的Pending的Intent模板进行统一设置,</span></div> <div style="line-height:25px"><span style="color:#003366; line-height:25px">对于数据集的单个数据项View只能进行填充Intent设置,以便让每个数据项View都有个性化的Click行为。</span></div> </div> <div style="line-height:25px"> <span style="color:#000080; line-height:25px">在StackWidgetProvider中,它接受了用户click数据项View所发送的Intent,并在</span><span style="color:#0000ff; line-height:25px">onReceive()</span><span style="color:#000080; line-height:25px">对其进行了处理。</span> </div> <div style="line-height:25px"><span style="color:#000080; line-height:25px">如果发现intent的action是TOAST_ACTION(即为用户click数据项View所发送的Intent), app widget 将显示一个关于当前数据项View的Toast消息.</span></div> <div style="line-height:25px"><span style="line-height:25px">示例7</span></div> <span style="line-height:normal; color:rgb(51,51,51); font-family:arial,sans-serif; font-size:13px"></span> <pre style="line-height:inherit; color:rgb(0,112,0); font-family:monospace; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); background-color:rgb(250,250,250); padding-top:10px; padding-right:10px; padding-bottom:10px; padding-left:10px; margin-top:0px; margin-bottom:1em; margin-left:1em; overflow-x:auto; overflow-y:auto"><span style="line-height:23px; color:rgb(0,0,136)">public</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(0,0,136)">class</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,0,102)">StackWidgetProvider</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(0,0,136)">extends</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,0,102)">AppWidgetProvider</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,102,0)">{</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(0,0,136)">public</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(0,0,136)">static</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(0,0,136)">final</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,0,102)">String</span><span style="line-height:23px; color:rgb(0,0,0)"> TOAST_ACTION </span><span style="line-height:23px; color:rgb(102,102,0)">=</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(0,136,0)">"com.example.android.stackwidget.TOAST_ACTION"</span><span style="line-height:23px; color:rgb(102,102,0)">;</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(0,0,136)">public</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(0,0,136)">static</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(0,0,136)">final</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,0,102)">String</span><span style="line-height:23px; color:rgb(0,0,0)"> EXTRA_ITEM </span><span style="line-height:23px; color:rgb(102,102,0)">=</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(0,136,0)">"com.example.android.stackwidget.EXTRA_ITEM"</span><span style="line-height:23px; color:rgb(102,102,0)">;</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(102,102,0)">...</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(136,0,0)">// Called when the BroadcastReceiver receives an Intent broadcast.</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(136,0,0)">// Checks to see whether the intent's action is TOAST_ACTION. If it is, the app widget </span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(136,0,0)">// displays a Toast message for the current item.</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(0,102,102)">@Override</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(0,0,136)">public</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(0,0,136)">void</span><span style="line-height:23px; color:rgb(0,0,0)"> onReceive</span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(102,0,102)">Context</span><span style="line-height:23px; color:rgb(0,0,0)"> context</span><span style="line-height:23px; color:rgb(102,102,0)">,</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,0,102)">Intent</span><span style="line-height:23px; color:rgb(0,0,0)"> intent</span><span style="line-height:23px; color:rgb(102,102,0)">)</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,102,0)">{</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(102,0,102)">AppWidgetManager</span><span style="line-height:23px; color:rgb(0,0,0)"> mgr </span><span style="line-height:23px; color:rgb(102,102,0)">=</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,0,102)">AppWidgetManager</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">getInstance</span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(0,0,0)">context</span><span style="line-height:23px; color:rgb(102,102,0)">);</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(0,0,136)">if</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(0,0,0)">intent</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">getAction</span><span style="line-height:23px; color:rgb(102,102,0)">().</span><span style="line-height:23px; color:rgb(0,0,0)">equals</span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(0,0,0)">TOAST_ACTION</span><span style="line-height:23px; color:rgb(102,102,0)">))</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,102,0)">{</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(0,0,136)">int</span><span style="line-height:23px; color:rgb(0,0,0)"> appWidgetId </span><span style="line-height:23px; color:rgb(102,102,0)">=</span><span style="line-height:23px; color:rgb(0,0,0)"> intent</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">getIntExtra</span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(102,0,102)">AppWidgetManager</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">EXTRA_APPWIDGET_ID</span><span style="line-height:23px; color:rgb(102,102,0)">,</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(102,0,102)">AppWidgetManager</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">INVALID_APPWIDGET_ID</span><span style="line-height:23px; color:rgb(102,102,0)">);</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(0,0,136)">int</span><span style="line-height:23px; color:rgb(0,0,0)"> viewIndex </span><span style="line-height:23px; color:rgb(102,102,0)">=</span><span style="line-height:23px; color:rgb(0,0,0)"> intent</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">getIntExtra</span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(0,0,0)">EXTRA_ITEM</span><span style="line-height:23px; color:rgb(102,102,0)">,</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(0,102,102)">0</span><span style="line-height:23px; color:rgb(102,102,0)">);</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(102,0,102)">Toast</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">makeText</span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(0,0,0)">context</span><span style="line-height:23px; color:rgb(102,102,0)">,</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(0,136,0)">"Touched view "</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,102,0)">+</span><span style="line-height:23px; color:rgb(0,0,0)"> viewIndex</span><span style="line-height:23px; color:rgb(102,102,0)">,</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,0,102)">Toast</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">LENGTH_SHORT</span><span style="line-height:23px; color:rgb(102,102,0)">).</span><span style="line-height:23px; color:rgb(0,0,0)">show</span><span style="line-height:23px; color:rgb(102,102,0)">();</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(102,102,0)">}</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(0,0,136)">super</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">onReceive</span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(0,0,0)">context</span><span style="line-height:23px; color:rgb(102,102,0)">,</span><span style="line-height:23px; color:rgb(0,0,0)"> intent</span><span style="line-height:23px; color:rgb(102,102,0)">);</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(102,102,0)">}</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(0,102,102)">@Override</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(0,0,136)">public</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(0,0,136)">void</span><span style="line-height:23px; color:rgb(0,0,0)"> onUpdate</span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(102,0,102)">Context</span><span style="line-height:23px; color:rgb(0,0,0)"> context</span><span style="line-height:23px; color:rgb(102,102,0)">,</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,0,102)">AppWidgetManager</span><span style="line-height:23px; color:rgb(0,0,0)"> appWidgetManager</span><span style="line-height:23px; color:rgb(102,102,0)">,</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(0,0,136)">int</span><span style="line-height:23px; color:rgb(102,102,0)">[]</span><span style="line-height:23px; color:rgb(0,0,0)"> appWidgetIds</span><span style="line-height:23px; color:rgb(102,102,0)">)</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,102,0)">{</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(136,0,0)">// update each of the app widgets with the remote adapter</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(0,0,136)">for</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(0,0,136)">int</span><span style="line-height:23px; color:rgb(0,0,0)"> i </span><span style="line-height:23px; color:rgb(102,102,0)">=</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(0,102,102)">0</span><span style="line-height:23px; color:rgb(102,102,0)">;</span><span style="line-height:23px; color:rgb(0,0,0)"> i </span><span style="line-height:23px; color:rgb(102,102,0)">&lt;</span><span style="line-height:23px; color:rgb(0,0,0)"> appWidgetIds</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">length</span><span style="line-height:23px; color:rgb(102,102,0)">;</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,102,0)">++</span><span style="line-height:23px; color:rgb(0,0,0)">i</span><span style="line-height:23px; color:rgb(102,102,0)">)</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,102,0)">{</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(136,0,0)">// Sets up the intent that points to the StackViewService that will</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(136,0,0)">// provide the views for this collection.</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(102,0,102)">Intent</span><span style="line-height:23px; color:rgb(0,0,0)"> intent </span><span style="line-height:23px; color:rgb(102,102,0)">=</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(0,0,136)">new</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,0,102)">Intent</span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(0,0,0)">context</span><span style="line-height:23px; color:rgb(102,102,0)">,</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,0,102)">StackWidgetService</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,136)">class</span><span style="line-height:23px; color:rgb(102,102,0)">);</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"> intent</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">putExtra</span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(102,0,102)">AppWidgetManager</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">EXTRA_APPWIDGET_ID</span><span style="line-height:23px; color:rgb(102,102,0)">,</span><span style="line-height:23px; color:rgb(0,0,0)"> appWidgetIds</span><span style="line-height:23px; color:rgb(102,102,0)">[</span><span style="line-height:23px; color:rgb(0,0,0)">i</span><span style="line-height:23px; color:rgb(102,102,0)">]);</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(136,0,0)">// When intents are compared, the extras are ignored, so we need to embed the extras</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(136,0,0)">// into the data so that the extras will not be ignored.</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"> intent</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">setData</span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(102,0,102)">Uri</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">parse</span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(0,0,0)">intent</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">toUri</span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(102,0,102)">Intent</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">URI_INTENT_SCHEME</span><span style="line-height:23px; color:rgb(102,102,0)">)));</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(102,0,102)">RemoteViews</span><span style="line-height:23px; color:rgb(0,0,0)"> rv </span><span style="line-height:23px; color:rgb(102,102,0)">=</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(0,0,136)">new</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,0,102)">RemoteViews</span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(0,0,0)">context</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">getPackageName</span><span style="line-height:23px; color:rgb(102,102,0)">(),</span><span style="line-height:23px; color:rgb(0,0,0)"> R</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">layout</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">widget_layout</span><span style="line-height:23px; color:rgb(102,102,0)">);</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"> rv</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">setRemoteAdapter</span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(0,0,0)">appWidgetIds</span><span style="line-height:23px; color:rgb(102,102,0)">[</span><span style="line-height:23px; color:rgb(0,0,0)">i</span><span style="line-height:23px; color:rgb(102,102,0)">],</span><span style="line-height:23px; color:rgb(0,0,0)"> R</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">id</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">stack_view</span><span style="line-height:23px; color:rgb(102,102,0)">,</span><span style="line-height:23px; color:rgb(0,0,0)"> intent</span><span style="line-height:23px; color:rgb(102,102,0)">);</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(136,0,0)">// The empty view is displayed when the collection has no items. It should be a sibling</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(136,0,0)">// of the collection view.</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"> rv</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">setEmptyView</span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(0,0,0)">R</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">id</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">stack_view</span><span style="line-height:23px; color:rgb(102,102,0)">,</span><span style="line-height:23px; color:rgb(0,0,0)"> R</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">id</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">empty_view</span><span style="line-height:23px; color:rgb(102,102,0)">);</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(136,0,0)">// This section makes it possible for items to have individualized behavior.</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(136,0,0)">// It does this by setting up a pending intent template. Individuals items of a collection</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(136,0,0)">// cannot set up their own pending intents. Instead, the collection as a whole sets</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(136,0,0)">// up a pending intent template, and the individual items set a fillInIntent</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(136,0,0)">// to create unique behavior on an item-by-item basis.</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(102,0,102)">Intent</span><span style="line-height:23px; color:rgb(0,0,0)"> toastIntent </span><span style="line-height:23px; color:rgb(102,102,0)">=</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(0,0,136)">new</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,0,102)">Intent</span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(0,0,0)">context</span><span style="line-height:23px; color:rgb(102,102,0)">,</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,0,102)">StackWidgetProvider</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,136)">class</span><span style="line-height:23px; color:rgb(102,102,0)">);</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(136,0,0)">// Set the action for the intent.</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(136,0,0)">// When the user touches a particular view, it will have the effect of</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(136,0,0)">// broadcasting TOAST_ACTION.</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"> toastIntent</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">setAction</span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(102,0,102)">StackWidgetProvider</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">TOAST_ACTION</span><span style="line-height:23px; color:rgb(102,102,0)">);</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"> toastIntent</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">putExtra</span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(102,0,102)">AppWidgetManager</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">EXTRA_APPWIDGET_ID</span><span style="line-height:23px; color:rgb(102,102,0)">,</span><span style="line-height:23px; color:rgb(0,0,0)"> appWidgetIds</span><span style="line-height:23px; color:rgb(102,102,0)">[</span><span style="line-height:23px; color:rgb(0,0,0)">i</span><span style="line-height:23px; color:rgb(102,102,0)">]);</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"> intent</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">setData</span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(102,0,102)">Uri</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">parse</span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(0,0,0)">intent</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">toUri</span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(102,0,102)">Intent</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">URI_INTENT_SCHEME</span><span style="line-height:23px; color:rgb(102,102,0)">)));</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(102,0,102)">PendingIntent</span><span style="line-height:23px; color:rgb(0,0,0)"> toastPendingIntent </span><span style="line-height:23px; color:rgb(102,102,0)">=</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,0,102)">PendingIntent</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">getBroadcast</span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(0,0,0)">context</span><span style="line-height:23px; color:rgb(102,102,0)">,</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(0,102,102)">0</span><span style="line-height:23px; color:rgb(102,102,0)">,</span><span style="line-height:23px; color:rgb(0,0,0)"> toastIntent</span><span style="line-height:23px; color:rgb(102,102,0)">,</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(102,0,102)">PendingIntent</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">FLAG_UPDATE_CURRENT</span><span style="line-height:23px; color:rgb(102,102,0)">);</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"> rv</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">setPendingIntentTemplate</span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(0,0,0)">R</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">id</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">stack_view</span><span style="line-height:23px; color:rgb(102,102,0)">,</span><span style="line-height:23px; color:rgb(0,0,0)"> toastPendingIntent</span><span style="line-height:23px; color:rgb(102,102,0)">);</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><br style="line-height:23px"> appWidgetManager</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">updateAppWidget</span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(0,0,0)">appWidgetIds</span><span style="line-height:23px; color:rgb(102,102,0)">[</span><span style="line-height:23px; color:rgb(0,0,0)">i</span><span style="line-height:23px; color:rgb(102,102,0)">],</span><span style="line-height:23px; color:rgb(0,0,0)"> rv</span><span style="line-height:23px; color:rgb(102,102,0)">);</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(102,102,0)">}</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(0,0,136)">super</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">onUpdate</span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(0,0,0)">context</span><span style="line-height:23px; color:rgb(102,102,0)">,</span><span style="line-height:23px; color:rgb(0,0,0)"> appWidgetManager</span><span style="line-height:23px; color:rgb(102,102,0)">,</span><span style="line-height:23px; color:rgb(0,0,0)"> appWidgetIds</span><span style="line-height:23px; color:rgb(102,102,0)">);</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(102,102,0)">}</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(102,102,0)">}</span></pre> <span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; line-height:25px"></span> <div style="line-height:25px"><span style="line-height:25px">3.6.2、设置数据项View的填充intent</span></div> <div style="line-height:25px"><span style="color:#000080; line-height:25px">你必须在RemoteViewsFactory中对数据集的每个数据项View设置填充intent,以用于区分是哪个数据项View.</span></div> <span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:#000080; line-height:22px">当用户点击数据项View,</span><span style="line-height:22px; color:rgb(0,0,128); font-family:Arial,Helvetica,simsun,u5b8bu4f53">其Pending的Intent模板和其填充Intent被合成一个最终的</span><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:#000080; line-height:22px">Intent,并进行广播发送。</span> </div> <div style="line-height:25px"><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; line-height:25px"><span style="line-height:25px">示例8</span></span></div> <div style="line-height:25px"> <span style="line-height:normal; color:rgb(51,51,51); font-family:arial,sans-serif; font-size:13px"></span> <pre style="line-height:inherit; color:rgb(0,112,0); font-family:monospace; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); background-color:rgb(250,250,250); padding-top:10px; padding-right:10px; padding-bottom:10px; padding-left:10px; margin-top:0px; margin-bottom:1em; margin-left:1em; overflow-x:auto; overflow-y:auto"><span style="line-height:23px; color:rgb(0,0,136)">public</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(0,0,136)">class</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,0,102)">StackWidgetService</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(0,0,136)">extends</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,0,102)">RemoteViewsService</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,102,0)">{</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(0,102,102)">@Override</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(0,0,136)">public</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,0,102)">RemoteViewsFactory</span><span style="line-height:23px; color:rgb(0,0,0)"> onGetViewFactory</span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(102,0,102)">Intent</span><span style="line-height:23px; color:rgb(0,0,0)"> intent</span><span style="line-height:23px; color:rgb(102,102,0)">)</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,102,0)">{</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(0,0,136)">return</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(0,0,136)">new</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,0,102)">StackRemoteViewsFactory</span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(0,0,136)">this</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">getApplicationContext</span><span style="line-height:23px; color:rgb(102,102,0)">(),</span><span style="line-height:23px; color:rgb(0,0,0)"> intent</span><span style="line-height:23px; color:rgb(102,102,0)">);</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(102,102,0)">}</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(102,102,0)">}</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(0,0,136)">class</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,0,102)">StackRemoteViewsFactory</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(0,0,136)">implements</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,0,102)">RemoteViewsService</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(102,0,102)">RemoteViewsFactory</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,102,0)">{</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(0,0,136)">private</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(0,0,136)">static</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(0,0,136)">final</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(0,0,136)">int</span><span style="line-height:23px; color:rgb(0,0,0)"> mCount </span><span style="line-height:23px; color:rgb(102,102,0)">=</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(0,102,102)">10</span><span style="line-height:23px; color:rgb(102,102,0)">;</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(0,0,136)">private</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,0,102)">List</span><span style="line-height:23px; color:rgb(102,102,0)">&lt;</span><span style="line-height:23px; color:rgb(102,0,102)">WidgetItem</span><span style="line-height:23px; color:rgb(102,102,0)">&gt;</span><span style="line-height:23px; color:rgb(0,0,0)"> mWidgetItems </span><span style="line-height:23px; color:rgb(102,102,0)">=</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(0,0,136)">new</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,0,102)">ArrayList</span><span style="line-height:23px; color:rgb(102,102,0)">&lt;</span><span style="line-height:23px; color:rgb(102,0,102)">WidgetItem</span><span style="line-height:23px; color:rgb(102,102,0)">&gt;();</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(0,0,136)">private</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,0,102)">Context</span><span style="line-height:23px; color:rgb(0,0,0)"> mContext</span><span style="line-height:23px; color:rgb(102,102,0)">;</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(0,0,136)">private</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(0,0,136)">int</span><span style="line-height:23px; color:rgb(0,0,0)"> mAppWidgetId</span><span style="line-height:23px; color:rgb(102,102,0)">;</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(0,0,136)">public</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,0,102)">StackRemoteViewsFactory</span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(102,0,102)">Context</span><span style="line-height:23px; color:rgb(0,0,0)"> context</span><span style="line-height:23px; color:rgb(102,102,0)">,</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,0,102)">Intent</span><span style="line-height:23px; color:rgb(0,0,0)"> intent</span><span style="line-height:23px; color:rgb(102,102,0)">)</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,102,0)">{</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"> mContext </span><span style="line-height:23px; color:rgb(102,102,0)">=</span><span style="line-height:23px; color:rgb(0,0,0)"> context</span><span style="line-height:23px; color:rgb(102,102,0)">;</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"> mAppWidgetId </span><span style="line-height:23px; color:rgb(102,102,0)">=</span><span style="line-height:23px; color:rgb(0,0,0)"> intent</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">getIntExtra</span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(102,0,102)">AppWidgetManager</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">EXTRA_APPWIDGET_ID</span><span style="line-height:23px; color:rgb(102,102,0)">,</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(102,0,102)">AppWidgetManager</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">INVALID_APPWIDGET_ID</span><span style="line-height:23px; color:rgb(102,102,0)">);</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(102,102,0)">}</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(136,0,0)">// Initialize the data set.</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(0,0,136)">public</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(0,0,136)">void</span><span style="line-height:23px; color:rgb(0,0,0)"> onCreate</span><span style="line-height:23px; color:rgb(102,102,0)">()</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,102,0)">{</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(136,0,0)">// In onCreate() you set up any connections / cursors to your data source. Heavy lifting,</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(136,0,0)">// for example downloading or creating content etc, should be deferred to onDataSetChanged()</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(136,0,0)">// or getViewAt(). Taking more than 20 seconds in this call will result in an ANR.</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(0,0,136)">for</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(0,0,136)">int</span><span style="line-height:23px; color:rgb(0,0,0)"> i </span><span style="line-height:23px; color:rgb(102,102,0)">=</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(0,102,102)">0</span><span style="line-height:23px; color:rgb(102,102,0)">;</span><span style="line-height:23px; color:rgb(0,0,0)"> i </span><span style="line-height:23px; color:rgb(102,102,0)">&lt;</span><span style="line-height:23px; color:rgb(0,0,0)"> mCount</span><span style="line-height:23px; color:rgb(102,102,0)">;</span><span style="line-height:23px; color:rgb(0,0,0)"> i</span><span style="line-height:23px; color:rgb(102,102,0)">++)</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,102,0)">{</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"> mWidgetItems</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">add</span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(0,0,136)">new</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,0,102)">WidgetItem</span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(0,0,0)">i </span><span style="line-height:23px; color:rgb(102,102,0)">+</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(0,136,0)">"!"</span><span style="line-height:23px; color:rgb(102,102,0)">));</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(102,102,0)">}</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(102,102,0)">...</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(102,102,0)">}</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(102,102,0)">...</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(136,0,0)">// Given the position (index) of a WidgetItem in the array, use the item's text value in </span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(136,0,0)">// combination with the app widget item XML file to construct a RemoteViews object.</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(0,0,136)">public</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,0,102)">RemoteViews</span><span style="line-height:23px; color:rgb(0,0,0)"> getViewAt</span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(0,0,136)">int</span><span style="line-height:23px; color:rgb(0,0,0)"> position</span><span style="line-height:23px; color:rgb(102,102,0)">)</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,102,0)">{</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(136,0,0)">// position will always range from 0 to getCount() - 1.</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(136,0,0)">// Construct a RemoteViews item based on the app widget item XML file, and set the</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(136,0,0)">// text based on the position.</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(102,0,102)">RemoteViews</span><span style="line-height:23px; color:rgb(0,0,0)"> rv </span><span style="line-height:23px; color:rgb(102,102,0)">=</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(0,0,136)">new</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,0,102)">RemoteViews</span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(0,0,0)">mContext</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">getPackageName</span><span style="line-height:23px; color:rgb(102,102,0)">(),</span><span style="line-height:23px; color:rgb(0,0,0)"> R</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">layout</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">widget_item</span><span style="line-height:23px; color:rgb(102,102,0)">);</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"> rv</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">setTextViewText</span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(0,0,0)">R</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">id</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">widget_item</span><span style="line-height:23px; color:rgb(102,102,0)">,</span><span style="line-height:23px; color:rgb(0,0,0)"> mWidgetItems</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,136)">get</span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(0,0,0)">position</span><span style="line-height:23px; color:rgb(102,102,0)">).</span><span style="line-height:23px; color:rgb(0,0,0)">text</span><span style="line-height:23px; color:rgb(102,102,0)">);</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(136,0,0)">// Next, set a fill-intent, which will be used to fill in the pending intent template</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(136,0,0)">// that is set on the collection view in StackWidgetProvider.</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(102,0,102)">Bundle</span><span style="line-height:23px; color:rgb(0,0,0)"> extras </span><span style="line-height:23px; color:rgb(102,102,0)">=</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(0,0,136)">new</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,0,102)">Bundle</span><span style="line-height:23px; color:rgb(102,102,0)">();</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"> extras</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">putInt</span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(102,0,102)">StackWidgetProvider</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">EXTRA_ITEM</span><span style="line-height:23px; color:rgb(102,102,0)">,</span><span style="line-height:23px; color:rgb(0,0,0)"> position</span><span style="line-height:23px; color:rgb(102,102,0)">);</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(102,0,102)">Intent</span><span style="line-height:23px; color:rgb(0,0,0)"> fillInIntent </span><span style="line-height:23px; color:rgb(102,102,0)">=</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(0,0,136)">new</span><span style="line-height:23px; color:rgb(0,0,0)"> </span><span style="line-height:23px; color:rgb(102,0,102)">Intent</span><span style="line-height:23px; color:rgb(102,102,0)">();</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"> fillInIntent</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">putExtras</span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(0,0,0)">extras</span><span style="line-height:23px; color:rgb(102,102,0)">);</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(136,0,0)">// Make it possible to distinguish the individual on-click</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(136,0,0)">// action of a given item</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"> rv</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">setOnClickFillInIntent</span><span style="line-height:23px; color:rgb(102,102,0)">(</span><span style="line-height:23px; color:rgb(0,0,0)">R</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">id</span><span style="line-height:23px; color:rgb(102,102,0)">.</span><span style="line-height:23px; color:rgb(0,0,0)">widget_item</span><span style="line-height:23px; color:rgb(102,102,0)">,</span><span style="line-height:23px; color:rgb(0,0,0)"> fillInIntent</span><span style="line-height:23px; color:rgb(102,102,0)">);</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(102,102,0)">...</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(136,0,0)">// Return the RemoteViews object.</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(0,0,136)">return</span><span style="line-height:23px; color:rgb(0,0,0)"> rv</span><span style="line-height:23px; color:rgb(102,102,0)">;</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(102,102,0)">}</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(102,102,0)">...</span><span style="line-height:23px; color:rgb(0,0,0)"><br style="line-height:23px"></span><span style="line-height:23px; color:rgb(102,102,0)">}</span></pre> </div> </wbr>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics