Android: development idea and demo of appwidget Widgets

Source: Internet
Author: User
Tags home screen

Development Idea and demo of appwidget Widgets

This articleArticleLet's talk about the development process of the "Window widget" (also called the main screen widget) in the Android system. What Is A Window widget?

It refers to the independent view displayed on the home screen (but filled with data ). The data content of these views is updated by the background process. Remoteviews is used

Display widgets and assign a broadcast receiver to update these remoteviews. You can create a widget separately or embed Multiple widgets in the app.

For example, <Figure 1> indicates that the Renren client has a widget and a Google Play widget.

 

<Figure 1>

Next, let's take the "birthday reminder" example to briefly describe the development idea of the widget.

I. First, you must declare an appwidget-provider control.

InRes → define a file in XMLAppwidgetproviderinfo. xml file.

 
<? XML version = "1.0" encoding = "UTF-8"?> <Appwidget-provider xmlns: Android = "http://schemas.android.com/apk/res/android"Android: minwidth= "150dp" // width (this is exquisite, and there is a comment at the end of the article *)Android: minheight= "120dp" // longAndroid: updateperiodmillis= "86400000" // update frequency in MSAndroid: initiallayout= "@ Layout/bday_widget" // layout FileAndroid: Configure= "Com. Carman. birthdayremind. configurebdaywidgetactivity"> // configuration activity (optional) </appwidget-provider>
Android: initiallayout is the interface you want to display. It is designed based on your needs.

II. Implementation of the appwidgetprovider class

To develop widgets, you must inherit the subclass of appwidgetprovider. The Implementation defines the basic method to allow you to program and connect to the app widget, which is based on broadcast events. When the app widget is updated, enabled, disabled, or deleted, you will receive broadcast notifications.

Bdaywidgetprovider. Class

 Public   Class Bdaywidgetprovider Extends  Appwidgetprovider {@ override  Public   Void Ondeleted (context, Int  [] Appwidgetids ){  //  Todo auto-generated method stub // When  APP widget instance  Called When deleting from the host.  } @ Override  Public   Void  Ondisabled (context ){When yourAPP widgetIs called when the last instance is deleted from the host. For example, you can add n instances to Renren's widget.  } @ Override  Public  Void  Onenabled (context ){//WhenAPP widgetThe instance is called when it is created for the first time.  }  @ Override  Public   Void  Onupdate (context, appwidgetmanager,  Int  [] Appwidgetids ){ //This method is called to update the interval.APP widget, Used for the intervalAppwidgetproviderinfoInUpdateperiodmillisAttribute definition (see addAppwidgetproviderinfoMetadata ).} 
 
@ OverridePublic VoidOnreceive (context, intent) {// This is called when each broadcast is received, and before the preceding callback function. You usually do not need to implement this method, because the defaultAppwidgetproviderFilter allAp p widgetBroadcast and call the preceding method properly.}
 
}

To achieve complex requirements, we need to expand the methods in the above classes.. I will only talk about the principle here, so I will not talk about it much.

3. Register and add a broadcast in manifest.

<Cycler Android: Name = ". bdaywidgetprovider"> <meta-data Android: Name = "android. appwidget. provider" Android: Resource = "@ XML/Appwidgetproviderinfo"/> <Intent-filter> <action Android: Name =" android. appwidget. Action. appwidget_update "/> </intent-filter> </Cycler>

<Intent-filter> The element must containAndroid: NameAttribute<Action>Element. This element specifiesAppwidgetproviderAcceptAction_appwidget_updateBroadcast. This is the only broadcast that you must explicitly declare. When necessary,AppwidgetmanagerAll otherAPP widgetBroadcastAppwidgetprovider.

 

* [Note]: defines the length and width of a widget.

The desktop location of the default app widgets window is based on the cell grid with the exact height and width. If the minimum length of the app widget and

If the size does not match, the app widget will contract to the nearest unit size (See App widget design guidelines for more information on desktop unit size)

Because the desktop layout direction (thus, the unit size) can change, according to the thumb rule, you should assume that the unit size is 74 pixels high and wide in the worst case. However, you must

Subtract 2 from the last dimension to take into account any integer rounding error generated during pixel calculation. To find the minimum width and height irrelevant to the pixel density, use this formula:
(Number of cells * 74)-2
Following this formula, you should use 72dp for each unit height, and 294dp for four unit widths.

 

You can create a simple widget in the preceding three steps. The principles are clear. Let's talk about my own birthdayremind functions:

Birthdayremind can be used to input the name and date in the configuration file to calculate the countdown of a person's birthday. The small window is updated every day, the number is reduced by 1, and a "buy" link is made,

You can call the link to buy a birthday present. Run the command as shown in <Figure 2>. If you are interested, you can download it and play it.

Birthdayremind Source Address: http://pan.baidu.com/share/link? Consumer id = 23016 & UK = 1442961174

<Figure 2>

 

It can be reproduced, but please indicate the source. Thank you!

Author: Carman 15:27:10

mailbox: carman_loneliness@163.com

Related Article

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.