Important alert: (current site time 7/15/2013 2:49:48 PM EDT)
 

VB icon

Alerts in J2Me

Email
Submitted on: 8/17/2004 5:21:25 AM
By: Bmaq007 
Level: Intermediate
User Rating: By 1 Users
Compatibility: Java (JDK 1.4)
Views: 14826
(About the author)
 
     Just an example of how the new alert feature in MIDP 2 works
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Alerts in J2Me
// Description:Just an example of how the new alert feature in MIDP 2 works
// By: Bmaq007
//
// Side Effects:Nothing
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=4349&lngWId=2//for details.//**************************************

import java.io.*;
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
// A simple MIDlet for testing various alerts.
public class AlertTest extends MIDlet
 implements CommandListener {
// An abstract class for our alert tests.
public abstract class AlertRunner {
protected static final int ONE_SECOND = 1000;
protected static final int FIVE_SECONDS = 5000;
public AlertRunner( String title ){
_title = title;
}
public abstract Alert doAlert();
public String getTitle(){ return _title; }
public String toString(){ return getTitle(); }
private String _title;
}
// An alert test for a simple timed alert.
public class TimedAlert extends AlertRunner {
public TimedAlert( String title ){
super( title );
}
public Alert doAlert(){
Alert a = new Alert( getTitle() );
a.setString( "Times out after 5 seconds..." );
a.setTimeout( FIVE_SECONDS );
showAlert( a );
return a;
}
}
// An alert test for a simple modal alert.
public class ModalAlert extends AlertRunner {
public ModalAlert( String title ){
super( title );
}
public Alert doAlert(){
Alert a = new Alert( getTitle() );
a.setString( "Waits to be dismissed" );
a.setTimeout( Alert.FOREVER );
showAlert( a );
return a;
}
}
// An alert test that displays an alert with
// a gauge whose value is increased every second.
// The alert can be dismissed only after the gauge
// reaches its maximum value.
public class ProgressAlert extends AlertRunner
 implements CommandListener, Runnable {
private static final int MAX = 10;
public ProgressAlert( String title ){
super( title );
}
public Alert doAlert(){
_gauge = new Gauge( null, false, MAX, 0 );
_gauge.setValue( 0 );
_done = false;
_alert = new Alert( getTitle() );
_alert.setString( "Counts to " + MAX +
" and then lets you dismiss it" );
_alert.setCommandListener( this );
_alert.setIndicator( _gauge );
// Set a _very_ long timeout
_alert.setTimeout( ONE_SECOND * 3600 );
showAlert( _alert );
new Thread( this ).start();
return _alert;
}
public void commandAction( Command c,
Displayable d ){
if( _done || _gauge.getValue() >= MAX ){
showList();
}
}
private void done(){
_alert.addCommand(
 new Command( "Done", Command.OK, 1 ) );
_done = true;
}
// A thread that bumps the value of the counter
// every second.
public void run(){
int val = _gauge.getValue();
Thread thread = Thread.currentThread();
try {
while( val < MAX ){
thread.sleep( ONE_SECOND );
_gauge.setValue( ++val );
}
}
catch( InterruptedException e ){
}
done();
}
private Alert_alert;
private int _counter;
private boolean _done;
private Gauge_gauge;
}
// An alert test that displays a continuously
// running gauge before automatically timing out.
public class BusyAlert extends AlertRunner {
public BusyAlert( String title ){
super( title );
}
public Alert doAlert(){
_gauge = new Gauge( null, false,
Gauge.INDEFINITE,
Gauge.CONTINUOUS_RUNNING );
_alert = new Alert( getTitle() );
_alert.setString( "Runs for 5 seconds and " +
"times out automatically" );
_alert.setIndicator( _gauge );
_alert.setTimeout( FIVE_SECONDS );
showAlert( _alert );
return _alert;
}
private Alert_alert;
private Gauge_gauge;
}
// Standard MIDlet code. Displays a list of
// available alert tests and runs the test once
// it's been chosen.
private Display display;
public static final Command exitCommand =
 new Command( "Exit",
 Command.EXIT, 1 ); 
public AlertTest(){
}
public void commandAction( Command c,
Displayable d ){
if( c == exitCommand ){
exitMIDlet();
} else if( c == List.SELECT_COMMAND ){
int index = _alertList.getSelectedIndex();
_alertRunners[ index ].doAlert();
}
}
protected void destroyApp( boolean unconditional )
throws MIDletStateChangeException {
exitMIDlet();
}
public void exitMIDlet(){
notifyDestroyed();
}
public Display getDisplay(){ return display; }
protected void initMIDlet(){
// The list of alert tests....
_alertRunners = new AlertRunner[] {
new TimedAlert( "Timed alert" ),
new ModalAlert( "Modal alert" ),
new ProgressAlert( "Progress alert" ),
new BusyAlert( "Busy alert" )
};
_alertList = new List( "Alert Testing",
List.IMPLICIT );
_alertList.setCommandListener( this );
for( int i = 0; i < _alertRunners.length; ++i ){
_alertList.append(
 _alertRunners[i].toString(), null );
}
showList();
}
protected void pauseApp(){
}
private void showAlert( Alert a ){
getDisplay().setCurrent( a, _alertList );
}
private void showList(){
getDisplay().setCurrent( _alertList );
}
protected void startApp()
throws MIDletStateChangeException {
if( display == null ){ 
display = Display.getDisplay( this );
initMIDlet();
}
}
private List _alertList;
private AlertRunner[] _alertRunners;
}


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
2/15/2007 7:16:09 AMjohn

Hello,
I saw some coding you posted on the internet. I am really impressed and I wish to solicit for your help. I would love you to assist me in my final year project.
The project is this:
Study and implementation of wireless Application Protocol on project management using J2ME
this project means, how a project manager can monitor the progress of his project using a mobile device(PDA or phone)
The application should provide feature that will allow the project manager to monitor the progress of his project, monitor the various stages of the project and contributions made by each group member. Please, carefully analyze the project and let me know how you think it is supposed to be..
Thanks for everything. You can contact me on my email taurus_attraction@yahoo.com.
Regards
Joe


(If this comment was disrespectful, please report it.)

 
10/24/2007 1:45:32 AMsunny

hi
i am planning to do b.tech project in j2me.please suggest some project to me.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.