The learning and use of Android-developed app Widgets

Source: Internet
Author: User
Tags unique id home screen

  • (The following is my understanding of the official documents, limited ability, wrong also hope to point out)

    1, first of all, we first read the official documents, back to live to show the demo program

    App Widgets (widget, widgets)

    APP Widgets is miniature application views so can be embedded on other applications (such as the Home screen) and recei ve periodic (Periodic, periodic) updates. These views is referred to as Widgets in the user interface, and you can publish one with an App Widget provider. An application component, which is able-to-hold the other app Widgets are called an app Widget host. The screenshot below shows the Music App Widget.

    The Desktop app gadget is a mini app view that can be embedded in other applications (such as the home screen) to receive regular updates. In the user interface these are categorized as widgets, and you can publish an application widget provider. An application component that is able to control the widgets of other applications is called the application widgets host (the parsing of the official documents given by the app widget host later). The following screen shot shows a music widget

    This document describes the publish an app widget using a app widget provider. For a discussion of creating your Ownappwidgethostto host app widgets, see app Widget host.

    This document describes how to use a widget provider to publish a widget. Discuss creating your own appwidgethost to go to the red Paper widget. See App Widget Host

    Widget Design

    Window Widget Design

    For information on how to design your app widgets, read thewidgets design Guide.

    for how to design your own widget program, please read the widget's Design wizard

    The Basics

    of Basic

    To create a App Widget, you need the following:

    to create a widget program, you need the following things


    AppwidgetproviderinfoobjectAppwidgetproviderinfo ObjectDescribes the metadata for an app widget, such as the app widget ' s layout, update frequency, and the Appwidgetprovider CLA Ss. This should is defined in XML.describes the elements of a widget program, such as the layout of the program, the update frequency, and the Appwidgetprovider class, which should be defined in XMLAppwidgetproviderclass implementationimplementation of the Appwidgetprovider classDefines the basic methods that allow you to programmatically (programmatic, programmatic way) interface with the APP Widget, based on broadcast Events. Through it, you'll receive broadcasts when the APP Widget is updated, enabled, disabled and deleted.allows you to write an interface based on broadcast events to define the basic method of the widget program, through it, in front of the widget program update, enable, disable, delete, you will receive a broadcast notification. View layoutView LayoutDefines the initial layout for the APP Widget, defined in XML.to define an initialization layout for a widget program in XML

    Additionally, you can implement an APP Widget configuration Activity. This is a optionalactivitythat launches when the user adds your app widget and allows him or she to modify app widget set Tings at Create-time.

    The following sections describe how to set up each of the these components.

    In addition, you can implement the application widgets configuration activity. This is an optional trigger activity when the user adds your application widget and allows him or her to modify the settings of the application widget creation time

    Declaring an App Widget in the Manifest

    description of the application widget manifest file

    First, declare theappwidgetproviderclass in your application ' sandroidmanifest.xmlfile. For example:

    First, declare this Appwidgetprovider class in your application Androidmanifest.xml file, for example

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

    This <receiver> element requires this android:name attribute, which formulates the appwidgetprovider used by the application widget

    The<intent-filter>element must include an<action>element with Theandroid:nameattribute. This attribute specifies that theappwidgetprovideraccepts theaction_appwidget_updatebroadcast. This are the only broadcast and must explicitly (clear, understood) declare. Theappwidgetmanagerautomatically sends all other APP widgets broadcasts to the appwidgetprovider as necessary.

    This <intent-filter> element must include an <action> element with a Android:name attribute, and this property formulates Appwidgetprovider receive this Action_appwidget _update broadcast, you must have the only clear statement of this broadcast. This appwidgetmanager automatically sends all other application widgets to the broadcast to Appwidgetprovider, which is necessary.

    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.

    This <meta-data> element formulates this Appwidgetproviderinfo resource and requires the following attributes:

    Android:name formulates this element according to the name, using Android.appwidget.provider to identify the data as Appwidgetproviderinfo descriptor

      • Android:resource-specifies Theappwidgetproviderinforesource location.

    Android:resource Specify the location of this Appwidgetproviderinfo resource

    Adding the Appwidgetproviderinfo Metadata

    add appwidgetproviderinfo meta data

    (continue translation tomorrow)

    Theappwidgetproviderinfodefines The essential qualities of an App Widget, such as it minimum layout dimensions, its Initi Al layout resource, how often to update the App widgets, 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:initiall ayout= "@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 widgets consumes by D Efault . The default Home screen positions App Widgets in its window based on a grid of cells that has a defined height and width. If the values for a app widget ' s minimum width or height don ' t match the dimensions of the cells, then the App widget di Mensions round up to the nearest cell size.

      See the app Widgets 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 is 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 widgets to a size that may be smaller than the default widget size Def Ined by Theminwidthandminheightattributes. Introduced in Android 3.1.

      See the app Widgets Design guidelines for more information on sizing your app Widgets.

    • Theupdateperiodmillisattribute defines how often the APP Widget framework should request a update from Theappwidgetprovi Derby 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 PO Ssible-perhaps no more than once a 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 E Very minutes, or maybe only four times a day.

      Note: If the device is asleep when it was time for a update (as defined Byupdateperiodmillis), then the device would wake up in O Rder 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 don't need to update while the device is asleep and then you can Instead perform updates based on a alarm that would not wake the device. To does so, set a alarm with a Intent that your appwidgetprovider receives, using Thealarmmanager. Set the alarm type to EITHERELAPSED_REALTIMEORRTC, which would 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 she to configure APP Widget Properties. This was optional (read Creating an App Widget Configuration activitybelow).
    • Thepreviewimageattribute Specifies a preview of what the app widget would look like after it's configured, which the user s EES 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 being auto-advanced by the widget ' s Host. Introduced in Android 3.0.
    • Theresizemodeattribute specifies the rules by which a widgets can be resized. You have the attribute to make homescreen widgets resizeable-horizontally, vertically, or on both axes. Users Touch-hold a widgets to show their resize handles, then drag the horizontal and/or vertical handles 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 widgets can be resized. This field have 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 widgets can be resized. This field have 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 SC Reen (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, tha t want to embed app widgets in their UI. An appwidgethost must has an ID which is a 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, the assign in your application.

    Widget Host-this appwidgethost (for example, the home screen) provides interaction with the widget service program, allowing the widget program to be embedded in its UI interface. A window widget host must have a unique ID in its own package. The ID will persist throughout all used hosts. The ID is usually a hard-coded value that you assign in your application.

The learning and use of Android-developed app Widgets

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.