J2ME实现手机振动器

以前看塞班论坛上有人发了一个,自己正好在学就也学着写了一个,程序实现的不太好,而且里面有一个BUG暂时还不能解决,大家有兴趣可以拿走并加以更加完美的实现~~~同时,如果能解决了里面的一个BUG记得告诉我哟~~~~
/*注:BUG暂时保密,有兴趣者自行挖掘吧~~~~*/
 
package com.dometory0551.carter;
 
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
 
public class SCMIDlet extends MIDlet implements CommandListener,Runnable
{
public Command EXIT_COMMAND;         //退出按钮
public Command REGIST_COMMAND;       //注册按钮
public Command SET_COMMAND;          //设置按钮
public Command REGISTCONFIRM_COMMAND;//确认注册按钮
public Command SAVE_COMMAND;         //设置保存按钮
public Command BACK_COMMAND;         //返回按钮
public Alert REGIST_ALERT;           //提醒注册
public Alert HARM_ALERT;             //提醒伤害
public Alert DONE_ALERT;             //注册成功
public Alert FAIL_ALERT;             //注册失败:暂时没用
public Form REGIST_FORM;             //注册窗口
public Form MAIN_FORM;               //主窗口
public Form SETUP_FORM;              //设置窗口
public TextField REGIST_TEXTFIELD;   //输入注册码
public TextField HOUR_TEXTFIELD;     //输入设置小时
public TextField MINUTE_TEXTFIELD;   //输入设置分钟
public TextField SECOND_TEXTFIELD;   //输入设置秒
public String INFO;              //注册、帮助和主窗口信息串
public String REGISTINFO;        //注册信息
public String HELPINFO;
public int time;                //用户自定义振动时间
public int tempS;                //时间计算临时变量
public int tempY;
public Display display;
//construct function
public SCMIDlet()
{
//初始化命令按钮
EXIT_COMMAND   = new Command("退出",Command.EXIT,1);
REGIST_COMMAND = new Command("注册",Command.SCREEN,1);
SET_COMMAND    = new Command("设置",Command.SCREEN,2);
SAVE_COMMAND   = new Command("保存",Command.OK,1);
BACK_COMMAND   = new Command("返回",Command.BACK,2);
REGIST_COMMAND = new Command("注册",Command.OK,1);
//初始化输入框
REGIST_TEXTFIELD = new TextField("请注册/n",null,10,TextField.PASSWORD);
//初始化窗口
MAIN_FORM      = new Form("手机按摩器");
REGIST_FORM    = new Form("注册");
SETUP_FORM     = new Form("设置");
//初始化警告和提醒
HARM_ALERT     = new Alert("警告!","请注意,经常使用手机对身体有害!",null,AlertType.WARNING);
REGIST_ALERT   = new Alert("注册","请注册后使用!",null,AlertType.CONFIRMATION);
//初始化信息字符串
INFO           = new String("/n/n注意:本程序无专用人群!/n/n用于震动幅度较大的手机实现按摩器。/n该版本为V1.0.0,下一版本将不定期发布。/n作者:金桄宇/n版权归作者本人所有。/n其他任何人不可再未经允许的情况下私自出售以及修改代码!");
REGISTINFO     = new String("/n/n/n请在单击/"注册/"后输入购买获得的注册码进行注册。/n/n如填写错误或选择/"退出/",则程序将自行结束!/n");
REGIST_FORM.addCommand(REGIST_COMMAND);
REGIST_FORM.addCommand(EXIT_COMMAND);
REGIST_FORM.append(REGIST_TEXTFIELD);
REGIST_FORM.append(INFO+REGISTINFO);
REGIST_FORM.setCommandListener(this);
MAIN_FORM.append(INFO+"/n"+"通过选择/"设置/"可以自定义震动时间。/n"+"切忌以秒为单位!");
MAIN_FORM.addCommand(REGIST_COMMAND);
MAIN_FORM.addCommand(SET_COMMAND);
MAIN_FORM.addCommand(EXIT_COMMAND);
MAIN_FORM.setCommandListener(this);
SETUP_FORM.addCommand(SAVE_COMMAND);
SETUP_FORM.addCommand(BACK_COMMAND);
SETUP_FORM.setCommandListener(this);
display        = Display.getDisplay(this);
}
//程序入口方法
public void startApp()
{
display.setCurrent(REGIST_ALERT,REGIST_FORM);
}
//按钮监听
public void commandAction(Command cmd,Displayable dis)
{
if(cmd==EXIT_COMMAND)           //退出按钮
{
notifyDestroyed();
destroyApp(false);
}
if(cmd==REGIST_COMMAND)         //注册按钮
{
REGIST_FORM.append(REGIST_TEXTFIELD.getString());
if((REGIST_TEXTFIELD.getString()).equalsIgnoreCase("kingcarter"))
{
DONE_ALERT = new Alert("恭喜!","注册成功!",null,AlertType.INFO);
display.setCurrent(DONE_ALERT,MAIN_FORM);
try
{
display.vibrate(30000);
}
catch(IllegalArgumentException iae)
{
System.out.println("Error!");
}
}
else
{
FAIL_ALERT = new Alert("警告","注册码错误!注册失败!/n程序将退出!",null,AlertType.ERROR);
notifyDestroyed();
destroyApp(false);
}
}
if(cmd==SET_COMMAND)            //设置按钮
{
display.vibrate(0);
SETUP_FORM.deleteAll();
HOUR_TEXTFIELD = new TextField("/n/n/n请输入小时:/n","0",2,TextField.DECIMAL);
MINUTE_TEXTFIELD = new TextField("/n/n/n请输入分钟:","0",2,TextField.DECIMAL);
SECOND_TEXTFIELD = new TextField("/n/n/n请输入秒:","0",2,TextField.DECIMAL);
SETUP_FORM.append(HOUR_TEXTFIELD);
SETUP_FORM.append(MINUTE_TEXTFIELD);
SETUP_FORM.append(SECOND_TEXTFIELD);
display.setCurrent(SETUP_FORM);
}
if(cmd==SAVE_COMMAND)           //设置保存按钮
{
Thread t = new Thread(this);
display.setCurrent(MAIN_FORM);
t.start();
}
if(cmd==BACK_COMMAND)             //返回按钮
{
try
{
display.vibrate(30000);
}
catch(IllegalArgumentException iae)
{
System.out.println("Error!");
}
display.setCurrent(MAIN_FORM);
}
}
public void run()
{
int a=0;
int b=0;
int c=0;
//对abc三个变量检查赋值
if(HOUR_TEXTFIELD.getString()==null)
{
a=0;
}
else
{
a = Integer.parseInt(HOUR_TEXTFIELD.getString());
}
if(MINUTE_TEXTFIELD.getString()==null)
{
b=0;
}
else
{
b = Integer.parseInt(MINUTE_TEXTFIELD.getString());
}
if(SECOND_TEXTFIELD.getString()==null)
{
c=0;
}
else
{
c = Integer.parseInt(SECOND_TEXTFIELD.getString());
}
time = 3600 * a + 60 * b + c;
if(time<=10)
{
try
{
display.vibrate(time*1000);
}
catch(IllegalArgumentException iae){}
}
if(time>=10)
{
tempS = time / 10;
tempY = time % 10;
for(int i=1;i<=tempS;i++)
{
try
{
display.vibrate(10000);
}
catch(IllegalArgumentException iea){};
}
try
{
display.vibrate(tempY*1000);
}
catch(IllegalArgumentException iae){};
}
}
//以下方法暂时不用
public void pauseApp()
{}
public void destroyApp(boolean f)
{
}
}

你可能感兴趣的:(J2ME实现手机振动器)