📄 apr01_ericg.txt
字号:
After you've defined the alert, you display it by calling
Display.setCurrent. You pass the method both a reference to the
alert and a reference to a displayable element. A displayable
element is a top-level user interface object that extends the
Displayable class -- your application will have at least one
of these whether it's using the low-level or high-level UI APIs.
The system immediately activates the displayable element when
the alert times out or is dismissed. To return to the
displayable element that was active just before the alert was
displayed, simply do the following:
Display display = ....; // assign on startup
Alert msg = ....; // create an alert
display.setCurrent( alert, display.getCurrent() );
Notice that the setCurrent method is invoked on an instance of
the Display class. You can obtain the class when the MIDlet
starts by calling the static Display.getDisplay method, passing
the MIDlet instance as its only parameter. For example:
public MyApp extends MIDlet
{
private Display display;
public MyApp(){
display = Display.getDisplay( this );
}
public Display getDisplay(){
return display;
}
// rest of MIDlet goes here
}
It's simplest to save the Display instance as a member of your
main application class and make it available to the other classes
in your application.
Note that calling Display.setCurrent simply changes what is
displayed on the screen -- it doesn't halt the current thread of
execution, even if the alert is modal. You typically call it
in response to some event; you should exit the method you're in
as soon as possible to let the system's event processing to
continue unhindered.
The sound associated with an alert must be one of five predefined
instances of the AlertType class: INFO, WARNING, ERROR, ALARM, or
CONFIRMATION. The device associates different sounds with each
class, if possible, though some devices may not even support
sound.
You can play any sound at any time by calling the playSound
method, as in:
AlertType.ERROR.playSound( display );
The icon associated with an alert must be an instance of the Image
class. The icon should be as small as possible and look good when
mapped to monochrome.
Now what about tickers? A ticker is a user interface control that
displays a single line of text, scrolling it onscreen at regular
intervals just like an old-fashioned movie marquee. To use a
ticker, you create an instance of the Ticker class, passing the
string you want displayed into the constructor. For example:
import javax.microedition.ldcdui.*;
Ticker ticker = new Ticker( "Hello, world!" );
To display the ticker, you associate it with a top-level window
created by the high-level UI, that is, any class that extends
the Screen class. You associate the ticker by calling the
setTicker method, as in:
Form f = new Form( "A Title" );
f.setTicker( ticker );
The ticker is displayed in an appropriate area of the window.
You can share the same ticker across different windows; the
system then attempts to keep it in the same position.
The only thing you can do with a ticker is change its text by
calling the setString method. To stop displaying a ticker,
remove it from the top-level window by passing null to the
setTicker method.
Here's a simple stock tracking MIDlet that demonstrates the use
of alerts and tickers. Note that the stock values are generated
randomly for example purposes. In the real world, you would use
the HttpConnection class to obtain stock values from a web site.
Note particularly how the alert text can be changed while the
alert is still active.
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
public class StockWatcher extends MIDlet {
Display display;
Ticker ticker = new Ticker( "" );
Command exitCommand = new Command(
"Exit", Command.EXIT, 1 );
Timer timer = new Timer();
StockChecker checker = new StockChecker();
TickerForm form = new TickerForm();
Alert alert = new Alert( "Stock Alert!" );
public StockWatcher() {
display = Display.getDisplay( this );
alert.setTimeout( Alert.FOREVER );
}
protected void destroyApp( boolean unconditional ) {
}
protected void startApp() {
display.setCurrent( form );
timer.schedule( checker, 0, 30000 );
}
protected void pauseApp() {
}
public void exit(){
timer.cancel();
destroyApp( true );
notifyDestroyed();
}
// Display a simple form to hold the ticker
class TickerForm extends Form implements CommandListener {
public TickerForm(){
super( "Stock Watch" );
setTicker( ticker );
addCommand( exitCommand );
setCommandListener( this );
}
public void commandAction( Command c, Displayable d ){
exit();
}
}
// Check the stock values and put up an alert if
// they're beyond certain limits....
class StockChecker extends TimerTask {
Random generator = new Random();
int sybsValue = 20000;
int sunwValue = 30000;
int ibmValue = 40000;
StringBuffer buf = new StringBuffer();
public void run(){
String values = getStockValues();
ticker.setString( values );
if( sybsValue < 18000 || sybsValue > 22000 ||
sunwValue < 28000 || sunwValue > 32000 ||
ibmValue < 38000 || ibmValue > 42000 ){
alert.setString( values );
}
if( !alert.isShown() ){
display.setCurrent( alert, form );
}
}
private String getStockValues(){
sybsValue = randomStockValue( sybsValue );
sunwValue = randomStockValue( sunwValue );
ibmValue = randomStockValue( ibmValue );
buf.setLength( 0 );
appendValue( "SYBS", sybsValue );
appendValue( "SUNW", sunwValue );
appendValue( "IBM", ibmValue );
return buf.toString();
}
// Generate a random stock value... in the
// real world you'd use HTTP to obtain the
// stock value from a broker's website.
private int randomStockValue( int oldVal ){
int incr1 = ( generator.nextInt() % 2 );
int incr2 = ( generator.nextInt() % 16 );
if( incr1 < 1 ){
oldVal -= incr1 * 1000;
} else {
oldVal += ( incr1 - 2 ) * 1000;
}
if( incr2 < 8 ){
oldVal -= incr2 * 250;
} else {
oldVal += incr2 * 250;
}
return oldVal;
}
private void appendValue( String stock, int val ){
buf.append( stock );
buf.append( ' ' );
buf.append( Integer.toString( val / 1000 ) );
buf.append( '.' );
buf.append( Integer.toString( val % 1000 ) );
buf.append( ' ' );
}
}
}
. . . . . . . . . . . . . . . . . . . . . . .
- NOTE
Sun respects your online time and privacy. The Java Developer
Connection mailing lists are used for internal Sun
Microsystems(tm) purposes only. You have received this email
because you elected to subscribe. To unsubscribe, go to the
Subscriptions page (http://developer.java.sun.com/subscription/),
uncheck the appropriate checkbox, and click the Update button.
- SUBSCRIBE
To subscribe to a JDC newsletter mailing list, go to the
Subscriptions page (http://developer.java.sun.com/subscription/),
choose the newsletters you want to subscribe to, and click Update.
- FEEDBACK
Comments? Send your feedback on the J2ME Tech Tips to:
jdc-webmaster@sun.com
- ARCHIVES
You'll find the J2ME Tech Tips archives at:
http://java.sun.com/jdc/J2METechTips/index.html
- COPYRIGHT
Copyright 2001 Sun Microsystems, Inc. All rights reserved.
901 San Antonio Road, Palo Alto, California 94303 USA.
This document is protected by copyright. For more information,
see:
http://java.sun.com/jdc/copyright.html
- LINKS TO NON-SUN SITES
The J2ME Tech Tips may provide, or third parties may provide,
links to other Internet sites or resources. Because Sun has no
control over such sites and resources, You acknowledge and
agree that Sun is not responsible for the availability of such
external sites or resources, and does not endorse and is not
responsible or liable for any Content, advertising, products,
or other materials on or available from such sites or resources.
Sun will not be responsible or liable, directly or indirectly,
for any damage or loss caused or alleged to be caused by or in
connection with use of or reliance on any such Content, goods or
services available on or through any such site or resource.
J2ME Tech Tips
April 16, 2001
Sun, Sun Microsystems, Java, Java Developer Connection, and J2ME
are trademarks or registered trademarks of Sun Microsystems, Inc.
in the United States and other countries.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -