[收集]关于背景灯的文章收集
?
Keywords (APIs, classes, methods, functions): javax.microedition.lcdui.Display, javax.microedition.lcdui.Display.flashBacklight() OverviewThis snippet demonstrates how to flash the backlight of the device for a specified duration. In practice, the MIDlet constructs a menu item through which the user can flash the backlight.
SourceFlashing the backlight can be implemented as follows:
// Flash the backlight for 5 secondsDisplay.getDisplay(this).flashBacklight(5000);
Here is a complete example:
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.midlet.MIDlet;
public class ExampleMIDlet extends MIDlet implements CommandListener { private Command flashCommand; private Command exitCommand; private Form mainForm; /** * Constructor. Constructs the object and initializes displayables. */ public ExampleMIDlet() { mainForm = new Form("ExampleMIDlet"); flashCommand = new Command("Flash", Command.SCREEN, 0); mainForm.addCommand(flashCommand); exitCommand = new Command("Exit", Command.EXIT, 0); mainForm.addCommand(exitCommand); mainForm.setCommandListener(this); }? /** * Called when the MIDlet is started. */ public void startApp() { Display.getDisplay(this).setCurrent(mainForm); }? // Other inherited methods omitted for brevity // ...? /** * From CommandListener. * Called by the system to indicate that a command has been invoked on a * particular displayable. * @param command the command that was invoked * @param displayable the displayable where the command was invoked */ public void commandAction(Command command, Displayable displayable) { if (command == flashCommand) { // Flash the backlight for 5 seconds boolean flashAllowed = Display.getDisplay(this).flashBacklight(5000); if (!flashAllowed) { // TODO: Flashing is not allowed. Inform the user. } } else if (command == exitCommand) { // Exit the MIDlet destroyApp(true); notifyDestroyed(); } }Postconditions
If the user selects the Flash menu item, the backlight is flashed for 5 seconds.
?
原文连接:http://wiki.forum.nokia.com/index.php/CS000957_-_Flashing_the_backlight------------------------------------------------------------------------------------------------------------------If your application doesn't demand constant key presses, after a while the screen saver on a J2ME phone will start automatically.
To make sure that the display light is turned on, the setLights method should be called before the screen saver is started and this must be done in a loop since the screen saver is not disabled just interrupted.
import com.nokia.mid.ui.DeviceControl;import javax.microedition.lcdui.*;import javax.microedition.midlet.*;?public class BacklightWorkaround extends MIDlet {? private SimpleCanvas canvas;? /** * Keeps the backlight on by repeatedly setting */ class LightThread extends Thread { public void run() { while(true){? DeviceControl.setLights(0, 100); try { Thread.sleep(5000); } catch (InterruptedException ex) { ex.printStackTrace(); } } } }? private class SimpleCanvas extends Canvas implements CommandListener{ private Command exitCmd; private MIDlet midlet;? public SimpleCanvas(MIDlet midlet) { this.midlet = midlet; exitCmd = new Command("Exit",Command.EXIT, 1); addCommand(exitCmd); setCommandListener(this); } public void paint(Graphics g) { g.drawString("Let there be light.", 0, 0, Graphics.LEFT|Graphics.TOP); }? public void commandAction(Command command, Displayable displayable) { if(command == exitCmd){ midlet.notifyDestroyed(); } } }? public void startApp() { if(canvas == null){ canvas = new SimpleCanvas(this); new LightThread().start(); }? Display.getDisplay(this).setCurrent(canvas); }? public void pauseApp() { }? public void destroyApp(boolean unconditional) { }}??
Method
Description
static void flashLights(long duration)
Flashes the lights.
static void setLights(int num, int level)
Turns the lights on and off.
static void startVibra(int freq, long duration)
Vibrates for a given frequency and time.
static void stopVibra()
Stops vibrating.
The two device elements you can control are the lights and vibration. To temporarily flash the lights on and off, use the flashLights method. For example:
import com.nokia.mid.ui.DeviceControl;… DeviceControl.flashLights(5000);
This code will cause the lights (such as the LEDs) to flash. If there is no support for this feature, then nothing will happen (duh). The integer value specifies the length of time (in milliseconds) to keep the lights on, although the device might override this value if you specify a number that is too large.
The other method relating to lights, setLights, allows you to control any of the lights on the device individually, such as the backlight or the LEDs …in theory. In reality, Nokia only gives you the ability to control the device's backlight (if it has one). To do this, call the method with the light number (the first integer) set to 0 for the backlight and the level integer set to a number between 0 and 100 (where 0 is off and 100 is the brightest level). For devices that don't support graded backlighting, all values between 1 and 100 just translate to on. Here's an example of setLights in action:
DeviceControl.setLights(0, 100);
NOTE
?
Tip
A word of warning about playing with the backlight: There is no method to determine the current backlighting level; therefore, you have no way of restoring the lighting to the level the user previously had. Be careful using this function. A user won't be impressed if, while playing in a dark place, you turn the lights out to reward them for completing a level.
If you really want to add this function to your game, consider making it an option the player can enable or disable. This applies to the vibration function as well.
?
原文连接:http://j2megame.atw.hu/ch06lev1sec122.html
?
?
?
?
?
?
?
?
?
?
?
?