Android widget learning Summary

Source: Internet
Author: User

1. widget design steps
Three XML files need to be modified, one class:

1) The first XML is the layout XML file (for example, Main. XML), which belongs to this widget. Generally, if you use this part to display the time, it is okay to declare a textview in the layout XML.

2) The second XML is widget_provider.xml, which is mainly used to declare an appwidget. Layout specifies the above main. xml.

3) The third XML is androidmanifest. XML, which registers broadcastreceiver information.

4) the last class is used for some business logic operations.Let it inherit the appwidgetprovider class.Appwidgetprovider has many methods. Generally, we only override the onupdate (context, appwidgetmanager, int []) method.

2.CodeCase

1)Main. xml

 <?  XML version = "1.0" encoding = "UTF-8"  ?>  
< Linearlayout Xmlns: Android = "Http://schemas.android.com/apk/res/android" Android: Orientation = "Vertical" Android: layout_width = "Fill_parent"
Android: layout_height = "Fill_parent" >
< Textview Android: ID = "@ + ID/tvcurrtime" Android: layout_width = "Wrap_content" Android: layout_height = "Wrap_content" Android: Text = "@ String/hello"
Android: textcolor = "@ Color/Black" />
</ Linearlayout >

2)Hello_widget_provider.xml

<?XML version = "1.0" encoding = "UTF-8"?> 
<Appwidget-providerXmlns: Android= "Http://schemas.android.com/apk/res/android"
Android: minwidth= "146dip"Android: minheight= "72dip"Android: initiallayout= "@ Layout/main">
</Appwidget-provider>

3)Androidmanifest. xml

<?  XML version = "1.0" encoding = "UTF-8"  ?>  
< Manifest Xmlns: Android = "Http://schemas.android.com/apk/res/android" Package = "Com. Woody. testwidget" Android: versioncode = "1"
Android: versionname = "1.0" >
< Application Android: icon = "@ Drawable/icon" Android: Label = "@ String/app_name" >
< Cycler Android: Name = ". Hellowidgetprovider" Android: Label = "@ String/app_name" > <! -- Hellowidgetprovider is the class (Business Processing) -->
< Intent-Filter >
< Action Android: Name = "Android. appwidget. Action. appwidget_update" /> <! -- The specified -->
</ Intent-Filter >
< Meta-data Android: Name = "Android. appwidget. provider" Android: Resource = "@ XML/hello_widget_provider"/> <! -- Specify the widget -->
</ Cycler >
</ Application >
</ Manifest >

4)Hellowidgetprovider. Java

 
Public ClassHellowidgetproviderExtendsAppwidgetprovider {

/** called when the activity is first created. */
@ override
Public void onupdate (context, appwidgetmanager, int [] appwidgetids) {
timer = New timer ();
timer. scheduleatfixedrate ( New mytime (context, appwidgetmanager), 1, 1000);
}

Public ClassMytimeExtendsTimertask {
Remoteviews;
Appwidgetmanager;
Componentname thiswidget;
Dateformat format = simpledateformat. gettimeinstance (simpledateformat. Medium, locale. getdefault ());

 
PublicMytime (context, appwidgetmanager ){
This. Appwidgetmanager = appwidgetmanager;
Remoteviews =NewRemoteviews (context. getpackagename (), R. layout. Main );
Thiswidget =NewComponentname (context, hellowidgetprovider.Class);
}
@ Override
Public VoidRun (){
Remoteviews. settextviewtext (R. Id. tvcurrtime, "time =" + format. Format (NewDate ()));
Appwidgetmanager. updateappwidget (thiswidget, remoteviews );
}
}
}

Code explanation:Remoteview is used to describe the view displayed by a broken process. That is to say, this view is displayed by another process. It is inflate in the layout resource file. It also provides some basic operations that can be used to modify the View content.

Appwidget --- remoteview,Appwidgetprovider is a brocasereceiver that only receives the Enable, update, disale, and delete messages, while the real display interface is appwidgethostview (implemented in launcher ).Communicate through remoteview. Remoteview is used to tell launcher what the appwidget you want is.

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.