Instance of the desktop Gizmo-----Digital Clock

Source: Internet
Author: User

To implement a desktop component of a digital clock, developers need to define 8 ImageView on the program interface, 6 ImageView for hours, minutes, seconds, and two imageview to display the colon between hours, minutes, and seconds.

In order for the desktop component to display the current time in real time, the program needs to update the 6 ImageView on the program interface every 1 seconds, allowing them to display the current hour, minute, and second digits.

Import Java.text.SimpleDateFormat;
Import Java.util.Date;
Import Java.util.Timer;
Import Java.util.TimerTask;

Import Android.appwidget.AppWidgetManager;
Import Android.appwidget.AppWidgetProvider;
Import Android.content.ComponentName;
Import Android.content.Context;
Import Android.os.Handler;
Import Android.os.Message;
Import Android.widget.RemoteViews;

Public class DigitalClock extends appwidgetprovider{
Private Timer timer = new timer ();
private Appwidgetmanager Appwidgetmanager;
private context context;
//To define the 0~9 LCD digital picture as an array
private int[] digits = new int[]{
r.drawable.su01,
r.drawable.su02,
r.drawable.su03,
r.drawable.su04,
r.drawable.su05,
r.drawable.su06,
r.drawable.su07,
r.drawable.su08,
r.drawable.su09,
R.drawable.su10,
};
ImageView defines an array of hours, minutes, seconds
private int[] digitviews = new int[]{
R.id.image01,
R.id.image02,
R.id.image04,
R.id.image05,
R.id.image07,
R.id.image08,
};
@Override
Public void OnUpdate (context context, Appwidgetmanager Appwidgetmanager,
int[] appwidgetids) {
This.appwidgetmanager = Appwidgetmanager;
This.context = context;
//define Timers
timer = new timer ();
//Start a recurring schedule
Timer.schedule (New TimerTask () {

@Override
Public void Run () {
//Send Empty message, notify UI update
handler.sendemptymessage (0x123);
}
},0,1000);
}
private Handler Handler = new Handler () {
Public void Handlemessage (Message msg) {
if (msg.what = = 0x123) {
remoteviews views =
New Remoteviews (Context.getpackagename (), r.layout.activity_digital_clock);
//define SimpleDateFormat object
SimpleDateFormat df = new SimpleDateFormat ("Hhmmss");
//Formats the current time into HHMMSS form
String timestr = Df.format (New Date ());
for (int i = 0; i< timestr.length (); i++) {        
//Convert the I-digit string to the corresponding number
int num = Timestr.charat (i)-;
//Set the first picture to the corresponding LCD digital picture
Views.setimageviewresource (Digitviews[i], digits[num]);
}
//Wrap the Appwidgetprovider subclass instance into a ComponentName object
componentname componentname = new ComponentName (context, digitalclock.class);
//Call Appwidgetprovider to add remoteviews to ComponentName
Appwidgetmanager.updateappwidget (componentname, views);
}
super.handlemessage (msg);
}
};
}

In the above program, the bold code will dynamically update the LCD digital image displayed by the 6 ImageView according to the program's time string, so that the current time can be displayed by the LCD number.

Add the following snippet in the Androidmanifest.xml file to define the widget for that desktop:

<receiver
android:name= ". DigitalClock "
android:label= "@string/app_name"
>
<!--Use this broadcastreceiver as a desktop Gizmo --
<intent-filter >
<action android:name= "Android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<!--specify Meta-data of the desktop Gizmo
<meta-data
android:name= "Android.appwidget.provider"
android:resource= "@xml/appwidget_provider"
/>

</receiver>

Instance of the desktop Gizmo-----Digital Clock

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.