码迷,mamicode.com
首页 > 移动开发 > 详细

Android开发之App Widget的学习及使用

时间:2015-11-17 00:27:53      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:

  • (以下为本人对于官方文档的理解,能力有限,有错还望指出)

    1、首先,我们先阅读下官方文档,后面活给出Demo程序

    App Widgets(小工具,窗口小部件)

     

    App 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.

    桌面应用小工具是一个迷你的应用程序视图,它可以被嵌入在其他的应用程序中(如主屏幕)接收定期的更新。在用户接口中这些被归类为窗口小部件,你可以发布一个应用程序窗口小部件提供者。一个能够去控制其他应用程序窗口小部件的应用程序组件被称为应用程序窗口小部件宿主(后面给出app widget host 的官方文档的解析)。下面的屏幕快照展示了一个音乐窗口小部件

    This document describes how to publish an App Widget using an App Widget provider. For a discussion of creating your ownAppWidgetHostto host app widgets, see App Widget Host.

    本文档描述了如何使用窗口小部件提供者去发布一个窗口小部件。讨论创建你自己的AppWidgetHost 去红纸窗口小部件。看 App Widget Host

    Widget Design

    窗口小部件设计

    For information about how to design your app widget, read theWidgets design guide.

    对于如何去设计你自己的窗口小部件程序,请阅读这Widget的设计向导

    The Basics

    基本的

    To create an App Widget, you need the following:

    去创建一个窗口小部件程序,你需要以下的东西


    AppWidgetProviderInfoobject AppWidgetProviderInfo 对象 Describes the metadata for an App Widget, such as the App Widget‘s layout, update frequency, and the AppWidgetProvider class. This should be defined in XML. 描述了窗口小部件程序的元素据,例如程序的布局,更新频率,还有AppWidgetProvider 类,这应该在xml中定义   AppWidgetProviderclass implementation AppWidgetProvider 类的实现 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. 在xml中为窗口小部件程序定义初始化布局

    Additionally, you can implement an App Widget configuration Activity. This is an optionalActivitythat 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 set up each of these components.

    此外,你可以实现应用程序窗口小部件配置Activity。当用户添加你的应用程序窗口小部件并允许他或者她去修改应用程序窗口小部件创建时间的设置时这是一个可选的触发activity,

    Declaring an App Widget in the Manifest

    应用程序小部件Manifest文件的说明

    First, declare theAppWidgetProviderclass in your application‘sAndroidManifest.xmlfile. For example:

    第一,在你的应用程序AndroidManifest.xml文件中声明这个AppWidgetProvider 类,例如

    <receiver android:name="ExampleAppWidgetProvider" >
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data android:name="android.appwidget.provider"
                   android:resource="@xml/example_appwidget_info" />
    </receiver>

    The<receiver>element requires theandroid:nameattribute, which specifies theAppWidgetProviderused by the App Widget.

    这个<receiver>元素要求这个android:name属性,它制定了应用程序小部件所使用的AppWidgetProvider

    The<intent-filter>element must include an<action>element with theandroid:nameattribute. This attribute specifies that theAppWidgetProvideraccepts theACTION_APPWIDGET_UPDATEbroadcast. This is the only broadcast that you must explicitly(明确的,明白的) declare. TheAppWidgetManagerautomatically sends all other App Widget broadcasts to the AppWidgetProvider as necessary.

    这个<intent-filter>元素必须包括一个有android:name属性的<action>元素,这个属性制定AppWidgetProvider接收这ACTION_APPWIDGET_UPDATE广播,你必须唯一的明确的声明这个广播。这AppWidgetManager自动发送所有其他应用程序小部件的广播到AppWidgetProvider,这是必要的。

    The<meta-data>element specifies theAppWidgetProviderInforesource and requires the following attributes:

    • android:name- Specifies the metadata name. Useandroid.appwidget.providerto identify the data as theAppWidgetProviderInfodescriptor.

    这<meta-data>元素制定这AppWidgetProviderInfo 资源和要求以下的属性:

    android:name制定这个元素据名称,用android.appwidget.provider去标识数据作为AppWidgetProviderInfo的描述符

    android:resource指定这AppWidgetProviderInfo资源的位置

    Adding the AppWidgetProviderInfo Metadata

    增加AppWidgetProviderInfo 元数据

    (明天继续翻译)

    TheAppWidgetProviderInfodefines 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<appwidget-provider>element and save it in the project‘sres/xml/folder.

    For example:

    <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
        android:minWidth="40dp"
        android:minHeight="40dp"
        android:updatePeriodMillis="86400000"
        android:previewImage="@drawable/preview"
        android:initialLayout="@layout/example_appwidget"
        android:configure="com.example.android.ExampleAppWidgetConfigure" 
        android:resizeMode="horizontal|vertical"
        android:widgetCategory="home_screen">
    </appwidget-provider>

    Here‘s a summary of the<appwidget-provider>attributes:

    • The values for theminWidthandminHeightattributes specify the minimum amount of space the App Widget consumes by default. 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 Widget‘s minimum width or height don‘t 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 sizing your App Widgets.

      Note: To make your app widget portable across devices, your app widget‘s minimum size should never be larger than 4 x 4 cells.

    • TheminResizeWidthandminResizeHeightattributes specify the App Widget‘s absolute minimum size. These values should specify the size below which the App Widget would be illegible or otherwise unusable. Using these attributes allows the user to resize the widget to a size that may be smaller than the default widget size defined by theminWidthandminHeightattributes. Introduced in Android 3.1.

      See the App Widget Design Guidelines for more information on sizing your App Widgets.

    • TheupdatePeriodMillisattribute defines how often the App Widget framework should request an update from theAppWidgetProviderby calling theonUpdate()callback method. The actual update is not guaranteed to occur exactly on time with this value and we suggest updating as infrequently as possible—perhaps no more than once an hour to conserve the battery. You might also allow the user to adjust the frequency in a configuration—some 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 byupdatePeriodMillis), then the device will wake up in order to perform the update. If you don‘t update more than once per hour, this probably won‘t 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 theAlarmManager. Set the alarm type to eitherELAPSED_REALTIMEorRTC, which will only deliver the alarm when the device is awake. Then setupdatePeriodMillisto zero ("0").

    • TheinitialLayoutattribute points to the layout resource that defines the App Widget layout.
    • Theconfigureattribute defines theActivityto 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 Activitybelow).
    • ThepreviewImageattribute specifies a preview of what the app widget will look like after it‘s configured, which the user sees when selecting the app widget. If not supplied, the user instead sees your application‘s launcher icon. This field corresponds to theandroid:previewImageattribute in the<receiver>element in theAndroidManifest.xmlfile. For more discussion of usingpreviewImage, see Setting a Preview Image. Introduced in Android 3.0.
    • TheautoAdvanceViewIdattribute specifies the view ID of the app widget subview that should be auto-advanced by the widget‘s host. Introduced in Android 3.0.
    • TheresizeModeattribute specifies the rules by which a widget can be resized. You use this attribute to make homescreen widgets resizeable—horizontally, vertically, or on both axes. Users touch-hold a widget to show its resize handles, then drag the horizontal and/or vertical handles to change the size on the layout grid. Values for theresizeModeattribute include "horizontal", "vertical", and "none". To declare a widget as resizeable horizontally and vertically, supply the value "horizontal|vertical". Introduced in Android 3.1.
    • TheminResizeHeightattribute specifies the minimum height (in dps) to which the widget can be resized. This field has no effect if it is greater thanminHeightor if vertical resizing isn‘t enabled (seeresizeMode). Introduced in Android 4.0.
    • TheminResizeWidthattribute specifies the minimum width (in dps) to which the widget can be resized. This field has no effect if it is greater thanminWidthor if horizontal resizing isn‘t enabled (seeresizeMode). Introduced in Android 4.0.
    • ThewidgetCategoryattribute declares whether your App Widget can be displayed on the home screen (home_screen), the lock screen (keyguard), or both. Only Android versions lower than 5.0 support lock-screen widgets. For Android 5.0 and higher, onlyhome_screenis valid.

    See theAppWidgetProviderInfoclass for more information on the attributes accepted by the<appwidget-provider>element.

    App Widget Host— The AppWidgetHost provides the interaction with the AppWidget service for apps, like the home screen, that want to embed app widgets in their UI. An AppWidgetHost must have an ID that is unique within the host‘s own package. This ID remains persistent across all uses of the host. The ID is typically a hard-coded value that you assign in your application.

    窗口小部件宿主--这AppWidgetHost(例如主屏幕) 提供了与窗口小部件服务程序的交互,使窗口小部件程序能够嵌入在它的UI界面中。一个窗口小部件宿主在自己的包中必须有一个唯一的ID。ID将一直持久化在所有使用的主机里面。ID通常是一个你在应用程序中分配的硬编码的值。

Android开发之App Widget的学习及使用

标签:

原文地址:http://my.oschina.net/zaizaiangels/blog/531130

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!