My android advanced tour --> Android widget desktop digital clock (digtalclockwidget) instance

Source: Internet
Author: User

Step 1: Create a project digtalclockwidget and import the image with the displayed time to the drawable folder directory:

The images are as follows:

Step 2: Compile the widget UI

A./XML/appwidget_provider.xml

<? XML version = "1.0" encoding = "UTF-8"?> <! -- Specify the basic configuration information of the Desktop component: minwidth: minimum width of the desktop widget. Minwidth: minimum height of the desktop widget. Updateperiodmillis: update frequency initiallayout: initial layout displayed --> <! -- Specify the widget layout interface as @ layout/main --> <appwidget-provider xmlns: Android = "http://schemas.android.com/apk/res/android" Android: initiallayout = "@ layout/main" Android: minheight = "30px" Android: minwidth = "300px" Android: updateperiodmillis = "500"/>

B./layout/Main. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Id = "@ + ID/digit" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: orientation = "horizontal" Android: gravity = "center"> <! -- Define 19 imageviews to display LCD numbers --> <imageview Android: Id = "@ + ID/img01" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"/> <imageview Android: Id = "@ + ID/img02" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"/> <imageview Android: id = "@ + ID/img03" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"/> <imageview Android: Id = "@ + ID/img04" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"/> <imageview Android: Id = "@ + ID/img05" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: src = "@ drawable/digity"/> <imageview Android: Id = "@ + ID/img06" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"/> <imageview Android: Id = "@ + ID/img07" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"/> <imageview Android: id = "@ + ID/img08" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: src = "@ drawable/digity"/> <imageview Android: id = "@ + ID/img09" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"/> <imageview Android: Id = "@ + ID/img10" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"/> <imageview Android: Id = "@ + ID/img11" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: src = "@ drawable/digite"/> <imageview Android: Id = "@ + ID/img12" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"/> <imageview Android: Id = "@ + ID/img13" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"/> <imageview Android: id = "@ + ID/img14" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: src = "@ drawable/digit"/> <imageview Android: id = "@ + ID/img15" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"/> <imageview Android: Id = "@ + ID/img16" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"/> <imageview Android: Id = "@ + ID/img17" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: src = "@ drawable/digit"/> <imageview Android: Id = "@ + ID/img18" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: src = "@ drawable/digit"/> <imageview Android: Id = "@ + ID/img19" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"/> </linearlayout>

Step 3: androidmanifest. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android" package = "CN. roco. widget. clock "Android: versioncode =" 1 "Android: versionname =" 1.0 "> <uses-SDK Android: minsdkversion =" 8 "/> <application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name"> <! -- Specify the broadcast Receiver --> <receiver er Android: Name = "timewidgetprovider"> <intent-filter> <action Android: Name = "android. appwidget. action. appwidget_update "/> </intent-filter> <meta-data Android: Name =" android. appwidget. provider "Android: Resource =" @ XML/appwidget_provider "/> <! -- Specify provider --> </explorer> <! -- Specify the clock service --> <service android: Name = ". timeservice"/> </Application> </manifest>

Step 4: timeservice. Java

Package CN. roco. widget. clock; import Java. text. simpledateformat; import Java. util. date; import Java. util. timer; import Java. util. timertask; import android. app. pendingintent; import android. app. service; import android. appwidget. appwidgetmanager; import android. content. componentname; import android. content. intent; import android.net. uri; import android. OS. ibinder; import android. widget. remoteviews; public class t Imeservice extends Service {// set 0 ~ 9. The LCD digital image is defined as an array private int [] digits = new int [] {R. drawable. digit00, R. drawable. digit01, R. drawable. digit02, R. drawable. digit03, R. drawable. digit04, R. drawable. digit05, R. drawable. digit06, R. drawable. digit07, R. drawable. digit08, R. drawable. digit09,}; // define the imageview that displays the hour, minute, and second of the year, month, and day as an array private int [] digitviews = new int [] {R. id. img01, R. id. img02, R. id. img03, R. id. img04, R. id. img06, R. id. img07, R. id. img09, R. id. img10, R. id. img12, R. id. img13, R. id. img15, R. id. img16, R. id. img18, R. id. img19}; private timer; private timertask = new timertask () {@ overridepublic void run () {/** get and format the current time */simpledateformat SDF = new simpledateformat ("yyyymmddhhmmss"); string time = SDF. format (new date ();/** get the view of the display time */remoteviews views = new remoteviews (getpackagename (), R. layout. main); For (INT I = 0; I <time. length (); I ++) {// converts the I-th numeric character to the corresponding number int num = time. charat (I)-48; // set the I-th image as the corresponding LCD digital image views. setimageviewresource (digitviews [I], digits [num]);}/** call the dialup Program */intent = new intent (intent. action_view, Uri. parse ("Tel: 10086"); pendingintent = pendingintent. getactivity (getapplicationcontext (), 0, intent, 0); views. setonclickpendingintent (R. id. digit, pendingintent);/** display of update time */appwidgetmanager = appwidgetmanager. getinstance (getapplicationcontext (); // wrap the appwidgetprovider subclass instance into the componentname object componentname = new componentname (getapplicationcontext (), timewidgetprovider. class); // call appwidgetmanager to add remoteviews to the componentname appwidgetmanager. updateappwidget (componentname, views) ;};@ overridepublic void oncreate () {super. oncreate (); // defines the timer = new timer (); // starts the periodic scheduling timer. schedule (timertask, 0, 1000) ;}@ overridepublic ibinder onbind (intent) {return NULL ;}@ overridepublic void ondestroy () {super. ondestroy (); timer. cancel (); timer = NULL ;}}

Step 5: timewidgetprovider. Java

Package CN. roco. widget. clock; import android. appwidget. appwidgetmanager; import android. appwidget. appwidgetprovider; import android. content. context; import android. content. intent; public class timewidgetprovider extends appwidgetprovider {@ overridepublic void onupdate (context, appwidgetmanager, int [] appwidgetids) {}/** called when the user deletes widgets from the desktop */@ overridepublic void ondeleted (context, int [] appwidgetids) {}/*** is called only when widgets is added to the desktop for the first time. * When widgets of the same type is added to the desktop, it will not be called */@ overridepublic void onenabled (context) {// start the service context. startservice (new intent (context, timeservice. class);}/** call */@ overridepublic void ondisabled (context) {// stop the service context when the last widgets instance of the same type is deleted. stopservice (new intent (context, timeservice. class ));}}

Step 6: deploy the application to the simulator. After deployment, open the widget.

The application is as follows:

The project source code in: http://pan.baidu.com/share/link? Consumer id = 403033 & UK = 805959799

========================================================== ========================================================== ============================

Author: Ouyang Peng: Welcome to repost. sharing with others is the source of progress!

Reprinted Please retain the original address: http://blog.csdn.net/ouyang_peng

========================================================== ========================================================== ============================

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.