`
k_lb
  • 浏览: 798567 次
  • 性别: Icon_minigender_1
  • 来自: 郑州
社区版块
存档分类
最新评论
  • kitleer: 据我所知,国内有款ETL调度监控工具TaskCTL,支持ket ...
    kettle调度

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>
分享到:
评论

相关推荐

    python光学仿真PyQt5基础框架教程.pdf

    python光学仿真 光学仿真PyQt5基础框架教程 基础框架教程 前⼏天为了⾃⼰搞⼀个光学仿真集成GUI界⾯,于是去研究了⼀下PyQt5,不得不说这个模块的使⽤性远远超过了tkinter,强烈推 荐,于是准备出⼀个专栏,记录⼀下...

    福优林@Qt5小白变大牛初级篇word---第5章.pdf

    QWidget类是所有用户界面对象的基类,被称为基础窗口部件。 像主窗口、对话框、标签、还有按钮、文本输入框等都是窗口部件。 这些部件可以接受用户输入,显示数据和状态信息,并且在屏幕上绘制自己。 5.3.2 窗口与...

    Android开发5:应用程序窗口小部件App Widgets的实现(附demo)

    本次主要是实现一个Android应用,实现静态广播、动态广播两种改变 widget内容的方法,即在上篇博文中实验的基础上进行修改,所以此次实验的重点是AppWidget小部件的实现啦~ 首先,我们简单说一下Widget是一个啥玩意~...

    Ext 学习中文手册

    无论你是Ext库的新手,抑或是想了解Ext的人,本篇文章的内容都适合你。本文将简单地介绍Ext的几个基本概念,和如何快速地做出一个动态的页面并运行起来,假设读者已具备了一些JavaScript经验和初级了解HTML Dom。 目...

    use gwt dev Ajaxt--GWT 开发Ajax更方便(学习文档)

    在这篇文件里,我将要讨论最基础的GWT,并且展示Java开发人员如何使用GWT来创建一个简单的AJAX应用以从一个远程API获取查询结果并且显示在浏览器上。 介绍Google Web Toolkit(GWT) Google Web Toolkit是一个由Java...

    Python中PyQt5/PySide2的按钮控件使用实例

    在之前的文章中,我们介绍了PyQt5和PySide2中主窗口控件MainWindow的使用、窗口控件的4中基础布局管理。从本篇开始,我们来了解一下...在PyQt5/PySide2中,按钮控件名为QPushButton(),其位于QtWidgets子模块之下。在

    Url重写篇视频------本讲将通过实例比较ASP.NET下的三种典型URL重写方案

    Url重写系列篇: ----主讲:天涯浪子 本讲将通过实例比较ASP.NET下的三种典型URL重写方案——ISAPI重写(使用开源组件IIRF),ASP.NET2.0内置的urlMappings和基于自定义HTTPModule的URL重写(使用NBear.Web中的...

    python图形开发GUI库pyqt5的详细使用方法及各控件的属性与方法

    本文主要列出来python图形开发GUI库pyqt5的窗体,控件属性与方法如果你想看看python图形开发GUI库pyqt5的基础使用方法可以查看我们的另一篇文章 python图形开发GUI库pyqt5的基本使用方法详解 基本框架 import sys ...

    Android基础之获取LinearLayout的宽高

    LinearLayout是线性布局控件,它包含的子控件将以横向或竖向的方式排列,按照相对位置来排列所有的widgets或者其他的containers,超过边界时,某些控件将缺失或消失。有的时候,我们需要想获取LinearLayout宽高,下面...

    android精品源码

    Android 电量显示Widgets插件实现.zip Android 程序启动界面Demo.zip Android 精仿QQ登录界面源码.rar Android 网络监视器源码.zip android 遮罩层效果.zip Android_Home.rar Android之GPS定位.zip Android代码调用...

    Django实战之用户认证(用户登录与注销)

    上一篇中,我们已经打开了Django自带的用户认证模块,并配置了数据库连接,创建了相应的表,本篇我们将在Django自带的用户认证的基础上,实现自己个性化的用户登录和注销模块。 首先,我们自己定义一个用户登录表单...

    Eclipse_Swt_Jface_核心应用_部分19

    5.1.5 Widgets不是Controls 55 5.2 按钮(Button) 58 5.2.1 普通按钮(SWT.PUSH) 58 5.2.2 切换按钮(SWT.TOGGLE) 59 5.2.3 箭头按钮(SWT.ARROW) 60 5.2.4 单选按钮(SWT.RADIO) 60 5.2.5 多选...

Global site tag (gtag.js) - Google Analytics