已阅读5页,还剩6页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
app widgetsapp widgets are miniature application views that can be embedded in other applications (such as the home screen) and receive periodic updates. these views are referred to as widgets in the user interface, and you can publish one with an app widget provider. an application component that is able to hold other app widgets is called an app widget host. the screenshot below shows the music app widget.this document describes how to publish an app widget using an app widget provider.the basicsto create an app widget, you need the following:appwidgetproviderinfo objectdescribes the metadata for an app widget, such as the app widgets layout, update frequency, and the appwidgetprovider class. this should be defined in xml.appwidgetprovider class implementation defines the basic methods that allow you to programmatically interface with the app widget, based on broadcast events. through it, you will receive broadcasts when the app widget is updated, enabled, disabled and deleted.view layout defines the initial layout for the app widget, defined in xml.additionally, you can implement an app widget configuration activity. this is an optional activity that launches when the user adds your app widget and allows him or her to modify app widget settings at create-time.the following sections describe how to setup each of these components.declaring an app widget in the manifestfirst, declare the appwidgetprovider class in your applications androidmanifest.xml file. for example : the element requires the android:name attribute, which specifies the appwidgetprovider used by the app widget.the element must include an element with the android:name attribute. this attribute specifies that the appwidgetprovider accepts the action_appwidget_update broadcast. this is the only broadcast that you must explicitly declare. the appwidgetmanager automatically sends all other app widget broadcasts to the appwidgetprovider as necessary.the element specifies the appwidgetproviderinfo resource and requires the following attributes: android:name - specifies the metadata name. use vider to identify the data as the appwidgetproviderinfo descriptor. android:resource - specifies the appwidgetproviderinfo resource location.adding the appwidgetproviderinfo metadata the appwidgetproviderinfo defines the essential qualities of an app widget, such as its minimum layout dimensions, its initial layout resource, how often to update the app widget, and (optionally) a configuration activity to launch at create-time. define the appwidgetproviderinfo object in an xml resource using a single element and save it in the projects res/xml/ folder.for example :appwidget-provider xmlns:android=/apk/res/android android:minwidth=294dp android:minheight=72dp android:updateperiodmillis=86400000 android:initiallayout=layout/example_appwidget android:configure=com.example.android.exampleappwidgetconfigure heres a summary of the attributes: the values for the minwidth and minheight attributes specify the minimum area required by the app widgets layout.the default home screen positions app widgets in its window based on a grid of cells that have a defined height and width. if the values for an app widgets minimum width or height dont match the dimensions of the cells, then the app widget dimensions round up to the nearest cell size. (see the app widget design guidelines for more information on the home screen cell sizes.)because the home screens layout orientation (and thus, the cell sizes) can change, as a rule of thumb, you should assume the worst-case cell size of 74 pixels for the height and width of a cell. however, you must subtract 2 from the final dimension to account for any integer rounding errors that occur in the pixel count. to find your minimum width and height in density-independent pixels (dp), use this formula:(number of cells * 74) 2following this formula, you should use 72 dp for a height of one cell, 294 dp and for a width of four cells. the updateperiodmillis attribute defines how often the app widget framework should request an update from the appwidgetprovider by calling the onupdate() method. the actual update is not guaranteed to occur exactly on time with this value and we suggest updating as infrequently as possibleperhaps no more than once an hour to conserve the battery. you might also allow the user to adjust the frequency in a configurationsome people might want a stock ticker to update every 15 minutes, or maybe only four times a day.note: if the device is asleep when it is time for an update (as defined by updateperiodmillis), then the device will wake up in order to perform the update. if you dont update more than once per hour, this probably wont cause significant problems for the battery life. if, however, you need to update more frequently and/or you do not need to update while the device is asleep, then you can instead perform updates based on an alarm that will not wake the device. to do so, set an alarm with an intent that your appwidgetprovider receives, using the alarmmanager. set the alarm type to either elapsed_realtime or rtc, which will only deliver the alarm when the device is awake. then set updateperiodmillis to zero (0). the initiallayout attribute points to the layout resource that defines the app widget layout. the configure attribute defines the activity to launch when the user adds the app widget, in order for him or her to configure app widget properties. this is optional (read creating an app widget configuration activity below).see the appwidgetproviderinfo class for more information on the attributes accepted by the element.creating the app widget layout you must define an initial layout for your app widget in xml and save it in the projects res/layout/ directory. you can design your app widget using the view objects listed below, but before you begin designing your app widget, please read and understand the app widget design guidelines.creating the app widget layout is simple if youre familiar with declaring layout in xml. however, you must be aware that app widget layouts are based on remoteviews, which do not support every kind of layout or view widget.a remoteviews object (and, consequently, an app widget) can support the following layout classes: framelayout linearlayout relativelayoutand the following widget classes : analogclock button chronometer imagebutton imageview progressbar textviewdescendants of these classes are not supported.using the appwidgetprovider classyou must declare your appwidgetprovider class implementation as a broadcast receiver using the element in the androidmanifest (see declaring an app widget in the manifest above).the appwidgetprovider class extends broadcastreceiver as a convenience class to handle the app widget broadcasts. the appwidgetprovider receives only the event broadcasts that are relevant to the app widget, such as when the app widget is updated, deleted, enabled, and disabled. when these broadcast events occur, the appwidgetprovider receives the following method calls:onupdate(context, appwidgetmanager, int)this is called to update the app widget at intervals defined by the updateperiodmillis attribute in the appwidgetproviderinfo (see adding the appwidgetproviderinfo metadata above). this method is also called when the user adds the app widget, so it should perform the essential setup, such as define event handlers for views and start a temporary service, if necessary. however, if you have declared a configuration activity, this method is not called when the user adds the app widget, but is called for the subsequent updates. it is the responsibility of the configuration activity to perform the first update when configuration is done. (see creating an app widget configuration activity below.) ondeleted(context, int) this is called every time an app widget is deleted from the app widget host. onenabled(context)this is called when an instance the app widget is created for the first time. for example, if the user adds two instances of your app widget, this is only called the first time. if you need to open a new database or perform other setup that only needs to occur once for all app widget instances, then this is a good place to do it. ondisabled(context) this is called when the last instance of your app widget is deleted from the app widget host. this is where you should clean up any work done in onenabled(context), such as delete a temporary database. onreceive(context, intent) this is called for every broadcast and before each of the above callback methods. you normally dont need to implement this method because the default appwidgetprovider implementation filters all app widget broadcasts and calls the above methods as appropriate.note: in android 1.5, there is a known issue in which the ondeleted() method will not be called when it should be. to work around this issue, you can implement onreceive() as described in this group post to receive the ondeleted() callback.the most important appwidgetprovider callback is onupdated() because it is called when each app widget is added to a host (unless you use a configuration activity). if your app widget accepts any user interaction events, then you need to register the event handlers in this callback. if your app widget doesnt create temporary files or databases, or perform other work that requires clean-up, then onupdated() may be the only callback method you need to define. for example, if you want an app widget with a button that launches an activity when clicked, you could use the following implementation of appwidgetprovider:public class exampleappwidgetprovider extends appwidgetprovider public void onupdate(context context, appwidgetmanager appwidgetmanager, int appwidgetids) final int n = appwidgetids.length; / perform this loop procedure for each app widget that belongs to this provider for (int i=0; i broadcast receiver lifecycle for more information). if your app widget setup process can take several seconds (perhaps while performing web requests) and you require that your process continues, consider starting a service in the onupdated() method. from within the service, you can perform your own updates to the app widget without worrying about the appwidgetprovider closing down due to an application not responding (anr) error. see the wiktionary samples appwidgetprovider for an example of an app widget running a service.also see the exampleappwidgetprovider.java sample class.receiving app widget broadcast intentsappwidgetprovider is just a convenience class. if you would like to receive the app widget broadcasts directly, you can implement your own broadcastreceiver or override the onreceive(context, intent) callback. the four intents you need to care about are : action_appwidget_update action_appwidget_deleted action_appwidget_enabled action_appwidget_disabledcreating an app widget configuration activityif you would like the user to configure settings when he or she adds a new app widget, you can create an app widget configuration activity. this activity will be automatically launched by the app widget host and allows the user to configure available settings for the app widget at create-time, such as the app widget color, size, update period or other functionality settings.the configuration activity should be declared as a normal activity in the android manifest file. however, it will be launched by the app widget host with the action_appwidget_configure action, so the activity needs to accept this intent. for example: also, the activity must be declared in the appwidgetproviderinfo xml file, with the android:configure attribute (see adding the appwidgetprovierinfo metadata above). for example, the configuration activity can be declared like this:notice that the activity is declared with a fully-qualified namespace, because it will be referenced from outside your package scope.thats all you need to get started with a configuration activity. now all you need is the actual activity. there are, however, two important things to remember when you implement the activity: the app widget host calls the configuration activity and the configuration activity should always return a result. the result should include the app widget id passed by the intent that launched the activity (saved in the intent extras as extra_appwidget_id). the onupdate() method will not be called when the app widget is created (the system will not send the action_appwidget_update broadcast when a configuration activity is launched). it is the responsibility of the configuration activity to request an update from the appwidgetmanager when the app widget is first created. however, onupdate() will be called for subsequent updatesit is only skipped the first time.see the code snippets in the following section for an example of how to return a result from the configuration and update the app widget.updating the app widget from the configuration activitywhen an app widget uses a configuration activity, it is the responsibility of the activity to update the app widget when configuration is complete. you can do so by requesting an update directly from the appwidgetmanager.heres a summary of the procedu
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年江苏省连云港市单招职业倾向性测试题库带答案
- 2026年遂宁能源职业学院单招职业技能考试必刷测试卷必考题
- 2026年重庆工信职业学院单招综合素质考试必刷测试卷汇编
- 2025年河北邢台临西县招聘编外辅助人员72名参考题库及一套完整答案详解
- 2025年河北秦皇岛市山海关区公开选调全额拨款事业编制工作人员20名参考题库附答案详解ab卷
- 2026年山西管理职业学院单招综合素质考试题库附答案
- 副高职称答辩题库合集及答案
- 2025年延安子长市事业单位定向招聘大学生退役士兵(11人)参考题库附答案详解(轻巧夺冠)
- 2026年西南交通大学希望学院单招职业倾向性考试必刷测试卷附答案
- 2026年江阴职业技术学院单招职业技能考试题库带答案
- 隧道维修安全教育培训课件
- 【《分拆上市的动因绩效研究的国内外文献综述》5700字】
- 第五单元 第二、三章学情评估卷(含答案)-冀少版八年级生物上册
- 市政道路工程测量方案
- 2025年江西工会工作者招聘考试(工会基础知识)历年参考题库含答案详解(5卷)
- 2025河南医疗器械公司会议记录范文
- 知道智慧树电路分析基础(浙江大学)满分测试答案
- 酸雨教学课件
- 供热管理办法佳木斯
- 装修工程的重点和难点及保证措施
- 安全试题100道及答案
评论
0/150
提交评论