You Are Here:

Community: Wiki

This page was last modified on 3 August 2009, at 11:31.

WidSets for Rookie EP 9 : Timer

From Forum Nokia Wiki

This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. The article is believed to be still valid for the original topic scope.

Contents

Introduction

In WidSets widget, you can set up schedule timer both one shot scheduler and periodic timer. This page will show you how to use them.

Timer Basic

schedule function is used for set up timer scheduler. When executed the system calls void timerEvent(Timer timer) function on widget script.

One Shot Scheduler

 Timer schedule(long delay)

This type of timer, timerEvent will be called only once after delay milliseconds passed.

Periodic Timer

 Timer schedule(long delay, long period)

Schedules a timer for repeated fixed-delay execution, beginning after the specified delay. Subsequent executions take place at approximately regular intervals separated by the specified period.

Usage Example

Define Timer object as class member variable.

 Timer g_timer;

Add Timer object construction in openWidget function.

 Shell openWidget()
 {
   ...
   // Delete timer if existed
   if (g_timer != null)
   {
     g_timer.cancel();
     g_timer = null;
   }
   // Construct timer
   g_timer = schedule(1000, 10);
   ...
 }

Add timerEvent function.

 void timerEvent(Timer timer)
 {
   ...
 }

This function will be first called after schedule function has been called for 1 second and will be called every 10 milliseconds after that as scheduled in openWidget.

And don't forget to add timer destruction in stopWidget function and before exit widget or your widget may be has big problem.

 void stopWidget()
 {
   // Delete timer if existed
   if (g_timer != null)
   {
     g_timer.cancel();
     g_timer = null;
   }
 }
 void actionPerformed(Shell shell, Component source, int action)
 {
   //when CMD_BACK event comes in, pop the current shell (this widget)
   if (action == CMD_BACK) {
     // Delete timer if existed
     if (g_timer != null) 
     {
       g_timer.cancel();
       g_timer = null; 
     }
     popShell(shell);
   }
 }

Bouncing Ball Example

This example will show you how to use timer.

Image:WidSets_BouncingBall.png

widget.xml

<?xml version="1.0" encoding="UTF-8"?>
 
<widget spec_version="2.1">
<info>
<name>Bouncing Ball</name>
<version>1.0</version>
<author>Sittiphol Phanvilai</author>
<clientversion>1.0</clientversion>
<shortdescription>Bouncing Ball (Timer Example)</shortdescription>
<longdescription>Example show you how to use Timer</longdescription>
<tags>example timer</tags>
</info>
 
<parameters>
<parameter type="string" name="widgetname" description="Name of widget" editable="no">Bouncing Ball</parameter>
</parameters>
 
<resources>
<code src="bouncing_ball.he"/>
 
<stylesheet>
bg {
color-1: white;
background: solid green;
align: hcenter vcenter;
border: 1 1 1 1;
border-type: rectangle white;
}
 
cenmid
{
align: hcenter vcenter;
}
 
canvas {
color-1: black;
color-2: red;
color-3: white;
font-3: small bold underlined;
}
</stylesheet>
 
<img scale="true" src="mnicon.png"/>
</resources>
 
<layout minimizedheight="45sp">
<view id="viewMini">
<img class="cenmid" src="mnicon.png"/>
</view>
 
<view id="viewMaxi" class="bg">
<script id="hello" class="text"/>
</view>
 
<webview>
<weblabel class="top: 0px; left: 10px;" style="color: black;">${widgetname}</weblabel>
</webview>
</layout>
 
</widget>

bouncing_ball.he

/*
* Copyright (C) 2008 Nokia Corporation.
*
* Licensed under separate Nokia Corporation End-user Software
* Agreement (the "License").
*
* You may not use this Software except in compliance with the License.
* The Software is distributed under the License on "AS IS" basis,
* withouth warranties. See the License for the specific language
* governing rights and limitations under the License.
*
* Modified by Sittiphol Phanvilai (Neois)
*
*/

 
class bouncing_ball
{
//It's nice to store command ids to static constants
const int CMD_BACK = 1;
 
//MenuItems are displayed over phone's soft buttons
//Usually to go back, ok, open options menu etc
MenuItem BACK = new MenuItem(CMD_BACK, "Back");
 
Shell g_shell = null;
Canvas g_canvas = null;
Timer g_timer;
 
int g_BallX = 30;
int g_BallY = 20;
 
int g_incX = 1;
int g_incY = 1;
 
//WidSets framework will call createElement() per
//each script-element it finds from views being
//created by createView()
 
Component createElement(String viewName,
String elementName,
Style style,
Object context)
{
return null;
}
 
void startWidget()
{
//instantiate minimized view in startup
setMinimizedView(createView("viewMini", getStyle("bg")));
}
 
Shell openWidget()
{
//instantiate maximized view when user opens this widget
g_canvas = new Canvas(getStyle("canvas"));
final Shell shell = new Shell(g_canvas);
 
//place canvas over automatic Scrollable created by Shell
shell[0] = shell[0][0];
 
cancelTimer();
g_timer = schedule(10, 10);
 
return (g_shell = shell);
}
 
void stopWidget()
{
cancelTimer();
}
 
MenuItem getSoftKey(Shell shell, Component focused, int key)
{
//return the key we want to display at position=SOFTKEY_BACK
//this usually is the Right Soft Button (RSB), for other key
//positions return null, as we don't want other keys
if (key == SOFTKEY_BACK) {
return BACK;
}
return null;
}
 
void actionPerformed(Shell shell, Component source, int action)
{
//when CMD_BACK event comes in, pop the current shell (this widget)
if (action == CMD_BACK) {
cancelTimer();
popShell(shell);
}
}
 
void paint(Component c, Graphics g, Style style, int width, int height)
{
g.setColor(style.color(0));
g.fillRect(0, 0, width, height);
g.setColor(0xFF8888);
g.fillArc(g_BallX, g_BallY, 10, 10, 0, 360);
}
 
void timerEvent(Timer timer)
{
int w, int h = g_canvas.getSize();
if (g_BallX == 0 || g_BallX == w-10)
g_incX = -g_incX;
if (g_BallY == 0 || g_BallY == h-10)
g_incY = -g_incY;
g_BallX += g_incX;
g_BallY += g_incY;
g_shell.repaint(false);
flushScreen(true);
}
 
void cancelTimer()
{
if (g_timer != null)
{
g_timer.cancel();
g_timer = null;
}
}
 
}

Code Snippet

You can download source code for this tutorial from File:WidSets Canvas Example.zip

See Also

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fWidSetsE5fforE5fAdvanceE5fE45PE5f1E5fE3aE5fE4cifeE5fPicturesE5fProjectX qdcZpublisherQUxhttpE3aE2fE2fswE2enokiaE2ecomE2fidE2fc764fd1cE2d8b06E2d499aE2d9a6aE2d17c3903d5a65E2fforumE5fnokiaE5fcrawlerE5fagentX qdcZtitleQSxWidSetsE20forE20AdvanceE20E45PE201E20E3aE20E4cifeE20PicturesE20ProjectE20E2dE20ForumE20NokiaE20WikiX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qrssZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qfnZdistributionQUxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2fX qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qfnZupdatedQDx2008E2d10E2d03X qmarsZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ