Nokia3100的手电筒

来源:互联网 发布:js md5加密例子 编辑:程序博客网 时间:2024/04/29 22:58
Nokia3100居然没有提供手电筒的功能,黑暗中实在是不方便。
前一段刚看了J2ME的开发,Nokia UI API v1.0里提供了setLights(int num, int level) 方法,于是就写了个手电筒的程序放在自己手机上用,呵呵。。。代码如下:

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import com.nokia.mid.ui.DeviceControl;

/**
 *
 * 
@author Autumn
 
*/

public class Light extends MIDlet implements CommandListener {
    
private Display display = null;
    
private Command onCommand  = new Command("", Command.OK, 1);
    
private Command offCommand = new Command("", Command.OK, 1);
    
private Command exitCommand = new Command("退出", Command.EXIT, 1);
    
private Form form1;
    
private StringItem si;
    
/** Creates a new instance of Light */
    
public Light() {
        form1 
= new Form("手电筒");
        si 
= new StringItem("手电筒状态:","");
    }

    
    
protected void startApp() throws MIDletStateChangeException {
        
if(display == null)
            display 
= Display.getDisplay(this);
        form1.append(si);
        form1.addCommand(exitCommand);
        lights(
100, offCommand,"");
    }

    
    
protected void pauseApp() {
        
    }

    
    
protected void destroyApp(boolean unconditional) {
        notifyDestroyed();
    }

    
    
public void commandAction(Command cmd, Displayable displayable) {
        
//退出
        if (cmd == exitCommand) {
            
// stop the MIDlet
            destroyApp(true);            
        }

        
//打开
        if(cmd == onCommand){
            form1.removeCommand(onCommand);
            lights(
100, offCommand,"");
        }

        
//关闭
        if(cmd == offCommand){
            form1.removeCommand(offCommand);
            lights(
0, onCommand,"");
        }

    }

    
    
public void lights(int level, Command cmd, String s) {
        si.setText(s);
        DeviceControl.setLights(
0, level);
        form1.addCommand(cmd);
        form1.setCommandListener(
this);
        display.setCurrent(form1);
    }

}
 
另外,com.nokia.mid.ui.DeviceControl还提供了闪光和振动的api:
Method Summary static void flashLights(long duration)
          Temporarily flashes the lights for a specific length of time given as milliseconds in duration parameter. static void setLights(int num, int level)
          Activates and deactivates the lights on the device. static void startVibra(int freq, long duration)
          Activates vibration for a given length of time and frequency. static void stopVibra()
          Stops any vibration.