Build Android Widgets

Source: Internet
Author: User

Build widgets on the Android platform:

1. Create the layout file of the widget under Res/layout: digitalclock. xml

Code

<? XML version = "1.0" encoding = "UTF-8" ?>
< Textview
Xmlns: Android = "Http://schemas.android.com/apk/res/android"
Android: layout_width = "Wrap_content"  
Android: layout_height = "Wrap_content"  
Android: ID = "@ + ID/time"
Android: textsize = "45px"
Android: scrollx = "30px"
Android: scrolly = "30px"
Android: textstyle = "Bold"
Android: textcolor = "# Ff000000"
Android: Background = "@ Drawable/BG"
/>

 

 

2. Create the description file of the widget in RES/xml: est_appwidget.xml

Code

<? XML version = "1.0" encoding = "UTF-8" ?>
< Appwidget-provider Xmlns: Android = "Http://schemas.android.com/apk/res/android"
Android: minwidth = "186dip"
Android: minheight = "212dip"
Android: updateperiodmillis = "1000"
Android: initiallayout = "@ Layout/digitalclock" />

 

 

3. inherit a class (esttime) from appwidgetprovider and override its virtual Method

Code

Package Com. Android. Test. esttime;

Import Android. App. Service;
Import Android. appwidget. appwidgetmanager;
Import Android. appwidget. appwidgetprovider;
Import Android. content. componentname;
Import Android. content. context;
Import Android. content. intent;
Import Android. OS. ibinder;
Import Android. Text. format. time;
Import Android. widget. remoteviews;

Public   Class Esttime Extends Appwidgetprovider {

/** Called when the activity is first created. */
@ Override
Public   Void Onupdate (context,
Appwidgetmanager, Int [] Appwidgetids)
{
Context. startservice ( New Intent (context, UpdateService. Class ));
}

// Service
Public   Static   Class UpdateService Extends Service {
@ Override
Public   Void Onstart (intent, Int Startid ){
Time esttime =   New Time ( " EST " );
Esttime. settonow ();
Remoteviews updateviews =  
New Remoteviews (getpackagename (),
R. layout. digitalclock );
Updateviews. settextviewtext (R. Id. Time, esttime. Format ( " % H: % m " ));

Componentname thiswidget =   New Componentname ( This , Esttime. Class );

Appwidgetmanager Manager = Appwidgetmanager. getinstance ( This );
manager. updateappwidget (thiswidget, updateviews);
}

@ Override
PublicIbinder onbind (intent ){
//Todo auto-generated method stub
Return Null;
}

}

}

 

 

4. Register this widget in androidmanifest. xml

Code

<? XML version = "1.0" encoding = "UTF-8" ?>
< Manifest Xmlns: Android = "Http://schemas.android.com/apk/res/android"
Package = "Com. Android. Test. esttime"
Android: versioncode = "1"
Android: versionname = "1.0" >
< Application Android: icon = "@ Drawable/icon" Android: Label = "@ String/app_name" >
< Cycler Android: Name = ". Esttime"
Android: Label = "@ String/app_name" >
< Intent-Filter >
< Action Android: Name = "Android. appwidget. Action. appwidget_update"   />
</ Intent-Filter >
< Meta-data Android: Name = "Android. appwidget. provider"  
Android: Resource = "@ XML/est_appwidget" />
</ Cycler >
< Service Android: Name = ". Esttime $ UpdateService" />
</ Application >
< Uses-SDK Android: minsdkversion = "3"   />
</ Manifest >  

 

Finally, compile and run. In this way, the newly created weiget is displayed in the widget list popped up by long-pressing on the home.

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.