Widgets BASICS (Part 1)

Source: Internet
Author: User
Tags home screen

Reference:Http://developer.android.com/guide/topics/appwidgets/index.html

I. PrefaceThe so-called App Widgets is the meaning of a micro-application. It can be embedded in other applications (such as the home screen) and regularly update its View. These views are used as Widgets on the user interface. You can use the App Widget provider to publish the App Widgets. A component of an application that can accommodate other App Widgets, which is called the App Widget host. Figure 1Is a music App Widget. Figure 1

In the following article, we will describe how to use App Widget provider to publish App widgets.Ii. Basic PrinciplesTo create an App Widget, you need the following three parts:AppwidgetproviderinfoAppWidgetProviderInfo is used to describe App Widgets metadata (metadata), such as App Widgets layout, update frequency, and AppWidgetProvider class. It should be defined in XML.AppwidgetproviderAppWidgetProvider defines some basic methods through which you can easily interact with App widgets. AppWidgetProvider is based on broadcast events. When the App Widget is updated, enabled, disabled, or deleted, you will receive the corresponding broadcast in AppWidgetProvider.View layout FileIn order for the App Widget to be displayed, we also need to provide a layout file for the App Widget. In addition, you can implement an Activity for configuring the App Widget. this Activity is optional. When you add an App Widget, The Acitivity will be started. It allows you to set App widgets when an App Widget is created. The settings here refer to the settings related to the transaction of the App Widget, not the content of AppWidgetProviderInfo.3. Declare app widgets in manifestFirst, declare an AppWidgetProvider class in the AndroidManifest. xm file of your application, suchExample 1.Example 1 <Cycler 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 "/> </er ER> <er ER> the android: name attribute of the element must be set, this attribute indicates which AppWidgetProvidere will be used to provide App widgets. The <intent-filter> element must contain the android: name attribute as "android. appwidget. action. APPWIDGET_UPDATE "Action, that is, <action android: name =" android. appwidget. action. APPWIDGET_UPDATE "/>. This attribute indicates that AppWidgetProvider can receive ACTION_APPWIDGET_UPDATE broadcasts. This broadcast is the only broadcast that you must declare to accept. AppWidgetManager can automatically put all other apps
The Widget broadcast is sent to AppWidgetProvider. In the <meta-data> element, you must specify the AppWidgetProviderInfo resource file and define the following two attributes:* Android: Name-It is used to define the name of the metadata element. You must set this attribute to "android. appwidget. provider" to indicate that the <meta-data> element is used to describe the location of the appwidgetproviderinfo resource file. * Android: Resource -This attribute is used to describe the location of the appwidgetproviderinfo resource file.4. Compile the appwidgetproviderinfo configuration fileAppwidgetproviderinfo is used to define the basic attributes of an app widget, such as the minimum size displayed, its initial layout resources, update frequency, and (optional) configuration activity, this activity will be started when its app widget is created. Appwidgetproviderinfo must be defined in an XML resource file with only a single <appwidget-provider> element. This file must be placed in the Res/XML directory.Example 2: <Appwidget-provider xmlns: Android = "http://schemas.android.com/apk/res/android" Android: minwidth = "294dp" Android: minheight = "72dp" Android: updateperiodmillis = "86400000" Android: previewimage = "@ drawable/preview" Android: initiallayout = "@ layout/example_appwidget" Android: Configure = "com. example. android. exampleappwidgetconfigure "Android: resizemode =" Horizontal | vertical "> </appwidget-provider> The following describes <appwidget-provider> attributes:MinwidthAndMinheightAttribute is used to describe the App
Widgets must occupy at least a large amount of space on the screen. When the default Home screen is placed in App Widgets, it is based on a grid instead of a pixel. the grid here refers to a rectangular area with certain pixels. Because the layout direction of the home screen may change (the size of the grid will change), you should consider the worst case where the grid has a length of 74 pixels and a height. However, you must subtract two pixels to avoid an error when calculating the number of grids. You should use the following formula to calculate android: minWidth and android: minHeight :( number of cells * 74)-2 according to the above formula, if you want your App Widget to occupy the width of four grids and the height of one grid, android: minWidth and android: minHeight should be "294dp" and "72dp" respectively. Note: to enable your App widgets to run on various platforms, the size of your App widgets should not exceed 4x4 mesh. For more information about the design of App widgets, see App
Widget Design GuidelinesUpdateperiodmillisAttribute is used to indicate that the App Widget Framework Requests the onUpdate () method of AppWidgetProvider to update the App
Widget frequency. However, this frequency cannot be completely guaranteed. We recommend that you minimize the update frequency. Sometimes it may only be updated once an hour to save the battery. You can also provide a configuration for the user to set the update frequency. Some people may want to update the stock price in 15 minutes, while others only want to update the stock four times in one day. Note: If the time for updating the App Widget is reached when the phone is in sleep state, the device will wake up to update the App Widget. If the update frequency is less than one hour, it will not cause obvious battery consumption. If your phone updates frequently or you do not want to update App widgets when the phone is in sleep state, use an alarm. You can useAlarmManagerTo set an Intent Alarm that regularly sends AppWidgetProvider, and set the Alarm typeELAPSED_REALTIMEOrRTC, these two types of alarm will be sent only when the system is in awake status.In additionAndroid: updateperiodmillis is set to "0". When you use alarmmanager to control screen flushing, you can ignore the refreshing when the system is locked. The code for judging the screen lock is as follows:

        android.app.KeyguardManager kgm = (android.app.KeyguardManager) getSystemService(KEYGUARD_SERVICE);        boolean isLocked = kgm.inKeyguardRestrictedInputMode();
Note: Updates
Requested updatePeriodMillisWill
Not be delivered more than once every 30 minutes. For more information, see http://developer.android.com/reference/android/appwidget/AppWidgetProviderInfo.html#updatePeriodMillis InitiallayoutProperty is used to set the layout file of the App Widget. The configure attribute is used to indicate which configure Activity will be started when the App Widget is added to the Home Screen. This is an optional attribute. For more information about times, please read the following article. PreviewimageThe property is added only in Android 3.0. It is used to indicate the preview image of the App Widget. It is used to display when the user selects the icon of the App Widget and intends to add the App Widget, so that you can understand the interface of the App Widget. If the preview icon is not provided, your App will be displayed.
Widget startup icon. This attribute is consistent with the android: previewImage attribute of the <viewer ER> element in AndroidManifest. xml. For more information, see the following section. The AutoadvanceviewidAttribute specifies the view ID of the app widget subview that shocould be
Auto-advanced by the widget's host. Introduced in Android 3.0. ResizemodeProperty is added only in Android 3.1. It is used to indicate the rules for adjusting the size of APP widgets. With this attribute, you can set the direction in which the app widget size can be adjusted, which can be vertical, horizontal, or both vertical and horizontal. You can hold down the app widget to display the size adjustment handle, and drag the handle in the horizontal or vertical direction to adjust the size of the app widget in the vertical or horizontal direction. The value of the resizemode attribute can be "horizontal ",
"Vertical", "NONE" and "Horizontal | vertical" IconThe property indicates the icon that your appwidget displays in the appwidget picker list. It should be consistent with the Android: icon attribute of the <javaser> element in androidmanifest. xml. LabelThe property indicates the name of your appwidget displayed in the appwidget picker list. It should be consistent with the Android: Label attribute of the 5. Compile the app widget layout FileYou must define the layout file of your app widget in the XML file and save it in the Res/layout/directory. You can use the following view in the app widget, but please read the app when you start the layout of your app widget
Widget design guidelines. If you are familiar with how to write layout files in XML, writing the app widget layout file will be very simple. Because the layout of APP widgets is based on remoteviews objects, it does not support all view. remoteviews objects and their subclasses:

  • FrameLayout
  • LinearLayout
  • RelativeLayout
  • AnalogClock
  • Button
  • Chronometer
  • ImageButton
  • ImageView
  • ProgressBar
  • TextView
  • ViewFlipper

Next

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.