Timer and sticker : Timer « J2ME « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » J2ME » Timer 




Timer and sticker
Timer and sticker

/*
 * TimerMIDlet.java Copyright (c) 2000 Sun Microsystems, Inc. All Rights
 * Reserved.
 
 * Author: Srikanth Raju
 
 * This software is the confidential and proprietary information of Sun
 * Microsystems, Inc. ("Confidential Information"). You shall not disclose such
 * Confidential Information and shall use it only in accordance with the terms
 * of the license agreement you entered into with Sun.
 
 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
 * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */

import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.Ticker;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class TimerMIDlet extends MIDlet implements CommandListener {

  private Display display = null;

  private Ticker stockTicker = null;

  private String[] _stocks = "SUNW""ORCL""NOK""MOT" };

  private String[] _prices = "9""11""9.25""14.5" };

  private static final Command exitCommand = new Command("Exit",
      Command.STOP, 0);

  private static final Command backCommand = new Command("Back",
      Command.BACK, 0);

  private static final Command doneCommand = new Command("Done", Command.OK,
      0);

  private Timer stockRefresh = null;

  private StockRefreshTask stockRefreshTask = null;

  private int refresh_interval = 10000// 1000 = 1 second

  private List menu = null;

  private ChoiceGroup _updatesChoices = null;

  private Form _updatesForm = null;

  private String _currentMenu = null;

  public TimerMIDlet() {
  }

  public void startApp() throws MIDletStateChangeException {

    display = Display.getDisplay(this);
    menu = new List("Stock Menu", Choice.IMPLICIT);
    menu.append("Updates"null);
    menu.append("Add Stocks"null);
    menu.append("Remove Stocks"null);
    menu.addCommand(exitCommand);
    menu.setCommandListener(this);
    // Make the ticker
    stockTicker = new Ticker(makeTickerString());
    menu.setTicker(stockTicker);

    display.setCurrent(menu);
    _currentMenu = "Stock Menu";

    _updatesForm = new Form("Updates");
    _updatesChoices = new ChoiceGroup("Update Interval:", Choice.EXCLUSIVE);
    _updatesChoices.append("Continuous"null)// will be 10 seconds
    _updatesChoices.append("15 minutes"null);
    _updatesChoices.append("30 minutes"null);
    _updatesChoices.append("1 hour"null);
    _updatesChoices.append("3 hours"null);

    _updatesForm.setTicker(stockTicker);
    _updatesForm.append(_updatesChoices);
    _updatesForm.addCommand(backCommand);
    _updatesForm.addCommand(doneCommand);
    _updatesForm.setCommandListener(this);

    //Set up and start the timer to refresh the stock quotes
    stockRefreshTask = new StockRefreshTask();
    stockRefresh = new Timer();
    stockRefresh.schedule(stockRefreshTask, 0, refresh_interval);
  }

  public String makeTickerString() {
    String retString = new String();
    for (int i = 0; i < _stocks.length; i++) {
      retString += _stocks[i];
      retString += " @ ";
      retString += _prices[i];
      retString += " ";
    }
    return retString;
  }

  public void pauseApp() {
    // free memory used by these objects
    display = null;
    stockRefresh = null;
    stockRefreshTask = null;
  }

  public void destroyApp(boolean unconditional)
      throws MIDletStateChangeException {

    notifyDestroyed();

  }

  public void commandAction(Command c, Displayable d) {

    if (c == exitCommand) {
      try {
        destroyApp(false);

      catch (MIDletStateChangeException msce) {
        System.out.println("Error in detroyApp(false) ");
        msce.printStackTrace();
      }
      notifyDestroyed();
    else if (c == backCommand) {
      _currentMenu = "Stock Menu";
      display.setCurrent(menu);
    else if (c == doneCommand) {
      switch (_updatesChoices.getSelectedIndex()) {
      case 0:
        refresh_interval = 10000;
        break;
      case 1:
        refresh_interval = 900000;
        break;
      case 2:
        refresh_interval = 1800000;
        break;
      case 3:
        refresh_interval = 3600000;
        break;
      case 4:
        refresh_interval = 10800000;
        break;
      default:
        break;
      }
      stockRefreshTask.cancel();
      stockRefreshTask = new StockRefreshTask();
      stockRefresh.schedule(stockRefreshTask, 0, refresh_interval);
      display.setCurrent(menu);
      _currentMenu = "Stock Menu";

    else {
      List shown = (Listdisplay.getCurrent();
      switch (shown.getSelectedIndex()) {
      case 0// Updates
        display.setCurrent(_updatesForm);
        _currentMenu = "Updates";
        break;

      case 1// Add Stock
        System.out.println("Add Stock... ");
        _currentMenu = "Add Stock";
        break;
      case 2// Remove Stock
        System.out.println("Remove Stock... ");
        _currentMenu = "Remove Stock";
        break;
      }
    }
  }

  /**
   * This is an extention of the TimerTask class which runs when called by
   * Timer. It refreshes the stock info for each stock from the quote server
   * and checks to see if any of the alerts should be fired.
   */
  class StockRefreshTask extends TimerTask {

    /**
     * Execute the Timer's Task
     */
    public void run() {

      try {
        // Just return if the database is empty
        if (_stocks.length == 0)
          return;
        else {
          for (int i = 0; i < _stocks.length; i++) {
            System.out.println("Stock price for symbol: "
                + _stocks[i" is: " + _prices[i]);
          }
        }
      catch (Exception e) {
        System.out.println("error('UPDATE FAILED', 2000)");
      }
    }
  }
}
           
       














Related examples in the same category
1.Timer and animationTimer and animation
2.Timer TemplateTimer Template
3.Demonstrate simple animation using a Timer and TimerTaskDemonstrate simple animation using a Timer and TimerTask
4.A simple class that shows an example of using a Timer and a TimerTask.A simple class that shows an example of using a Timer and a TimerTask.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.