Example MIDlet : Basics « J2ME « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » J2ME » BasicsScreenshots 
Example MIDlet
Example MIDlet

/*
J2ME in a Nutshell
By Kim Topley
ISBN: 0-596-00253-X

*/

import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class ExampleMIDlet extends MIDlet {
    
    // Flag to indicate first call to startApp
    private boolean started = false;
    
    // Background thread
    private Thread thread;
    
    // Timer interval
    private int timerInterval;
    
    // Timer
    private Timer timer;
    
    // Task to run via the timer
    private TimerTask task;
    
    // Required public constructor. Can be
    // omitted if nothing to do and no other
    // constructors are created.
    public ExampleMIDlet() {
        System.out.println("Constructor executed");
        
        // Get the timer interval from the 
        // manifest or JAD file.
        String interval = getAppProperty("Timer-Interval");
        timerInterval = Integer.parseInt(interval)
        System.out.println("Timer interval is " + interval);
    }
        
    protected void startApp() throws MIDletStateChangeException {
        if (!started) {
            // First invocation. Create and start
            // a timer.
            started = true;            
            System.out.println("startApp called for the first time");
            startTimer();
        else {
            // Resumed after pausing. 
            System.out.println("startApp called following pause");
        }
        
        // In all cases, start a background thread.
        synchronized (this) {
            if (thread == null) {
                thread = new Thread() {
                    public void run() {
                        System.out.println("Thread running");
                        while (thread == this) {
                            try {
                                Thread.sleep(1000);
                                System.out.println("Thread still active");
                            catch (InterruptedException ex) {
                            }
                        }
                        System.out.println("Thread terminating");
                    }
                };
            }
        }
        thread.start();
    }

    protected void pauseApp() {
        // Called from the timer task to
        // do whatever is necessary to 
        // pause the MIDlet.
        // Tell the background thread to stop.
        System.out.println("pauseApp called.");
        synchronized (this) {
            if (thread != null) {
                thread = null;
            }
        }
    }

    protected void destroyApp(boolean unconditional
                            throws MIDletStateChangeException {
        // Called to destroy the MIDlet.
        System.out.println("destroyApp called - unconditional = " 
                            + unconditional);
        if (thread != null) {
            Thread bgThread = thread;
            thread = null;      // Signal thread to die
            try {
                bgThread.join();
            catch (InterruptedException ex) {
            }
        }
        stopTimer();
    }
    
    // Starts a timer to run a simple task
    private void startTimer() {
        
        // Create a task to be run
        task = new TimerTask() {
            private boolean isPaused;
            private int count;
        
            public void run() {
                // Pause or resume the MIDlet.
                System.out.println("Timer scheduled");
                if (count++ == 4) {
                    // Terminate the MIDlet
                    try {
                        ExampleMIDlet.this.destroyApp(true);
                    catch (MIDletStateChangeException ex) {
                        // Ignore pleas for mercy!
                    }
                    ExampleMIDlet.this.notifyDestroyed();
                    return;
                }
                if (isPaused) {
                    System.out.println(">> Resuming MIDlet");
                    ExampleMIDlet.this.resumeRequest();
                    isPaused = false;
                else {
                    System.out.println(">> Pausing MIDlet");
                    isPaused = true;
                    ExampleMIDlet.this.pauseApp();
                    ExampleMIDlet.this.notifyPaused();
                }                
            }
        };
        
        // Create a timer and schedule it to run
        timer = new Timer();
        timer.schedule(task, timerInterval, timerInterval)
        System.out.println("Timer started.");
    }
    
    // Stops the timer
    private void stopTimer() {
        if (timer != null) {
            System.out.println("Stopping the timer");
            timer.cancel();
        }
    }
}



           
       
Related examples in the same category
1. Hello Midlet
2. Simple Midlet DemoSimple Midlet Demo
3. Basic MIDlet Shell
4. Welcome MIDletWelcome MIDlet
5. Welcome BackWelcome Back
6. Example jad file
7. MIDlet lifecycleMIDlet lifecycle
8. Goodbye WorldGoodbye World
9. MIDlet State TransitionsMIDlet State Transitions
ww___w__.j___ava_2_s.c_o_m_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.