日期:2014-05-20 浏览次数:21104 次
此实例为本人原创的电话呼叫程序,用户输入号码进行拨号,由于本人一直是搞低级UI开发的,所以并没有用高级控件做输出体。
?
框架代码1 - Constant
import javax.microedition.lcdui.Graphics;
/**
?* @author 空白节奏
?* */
public class Constant
{
public static final int SCR_WIDTH = 240;
public static final int SCR_HEIGHT = 320;
public static final int SCR_CENTERW = 120;
public static final int SCR_CENTERH = 160;
public static final byte FPS = 20;
public static final String rms_name = "RMS_name";
//走程状态
public static int MIDLetState = 0;
public static final int MIDLetState_LOGO = 1;
public static final int MIDLetState_MENU = 2;
public static final int KEY_UP = -1;
public static final int KEY_DOWN = -2;
public static final int KEY_LEFT = -3;
public static final int KEY_RIGHT = -4;
public static final int KEY_FIRE = -5;
public static final int KEY_LSOFT = -6;
public static final int KEY_RSOFT = -7;
public static final int KEY_CLEAR = -8;
public static final int KEY_SEND = -10;
public static final int KEY_END = -11;
public static final int KEY_ASTERISK = 42;// *
public static final int KEY_POUND = 35;// #
public static final int KEY_NUM0 = 48;
public static final int KEY_NUM1 = 49;
public static final int KEY_NUM2 = 50;
public static final int KEY_NUM3 = 51;
public static final int KEY_NUM4 = 52;
public static final int KEY_NUM5 = 53;
public static final int KEY_NUM6 = 54;
public static final int KEY_NUM7 = 55;
public static final int KEY_NUM8 = 56;
public static final int KEY_NUM9 = 57;
public static final int GLT = Graphics.LEFT | Graphics.TOP;
public static final int GRT = Graphics.RIGHT | Graphics.TOP;
public static final int GLB = Graphics.LEFT | Graphics.BOTTOM;
public static final int GRB = Graphics.RIGHT | Graphics.BOTTOM;
public static final int GTH = Graphics.TOP | Graphics.HCENTER;
public static final int GBH = Graphics.BOTTOM | Graphics.HCENTER;
public static final int GLV = Graphics.LEFT | Graphics.VCENTER;
public static final int GRV = Graphics.RIGHT | Graphics.VCENTER;
public static final int GHV = Graphics.HCENTER | Graphics.VCENTER;
}
?
框架代码2 - GameMIDlet
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;
/**
?* @author 空白节奏
?* */
public class GameMIDlet extends MIDlet
{
static GameMIDlet gameMIDlet = null;
Displayable current = null;
GameMaster gm = null;
public GameMIDlet()
{
gameMIDlet = this;
}
protected void startApp()
{
current = Display.getDisplay(this).getCurrent();
if(current == null)
{
gm = new GameMaster(this);
Display.getDisplay(this).setCurrent(gm);
}
else
{
Display.getDisplay(this).setCurrent(current);
}
}
protected void pauseApp()
{
}
protected void destroyApp(boolean arg0)
{
}
public static void quitApp()
{
gameMIDlet.destroyApp(true);
gameMIDlet.notifyDestroyed();
gameMIDlet = null;
}
}
?
?
框架代码3 - GameMaster
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
/**
?* @author 空白节奏
?* */
public class GameMaster extends Canvas implements Runnable
{
private boolean isStart = false;
private Image img_buffer = null;
private Graphics gg;
private boolean isFullScreen = true;
call c = null;
GameMIDlet gml = null;
public GameMaster(GameMIDlet gml)
{
this.gml = gml;
init();
setFullScreenMode(isFullScreen);
isStart = true;
new Thread(this).start();
}
public void paint(Graphics g)
{
gg.setColor(0);
gg.setClip(0, 0, Constant.SCR_WIDTH, Constant.SCR_HEIGHT);
gg.fillRect(0, 0, Constant.SCR_WIDTH, Constant.SCR_HEIGHT);
c.paint(gg);
g.drawImage(img_buffer, 0, 0, Constant.GLT);
}
public void update()
{
c.updata();
}
public void keyPressed(int keyCode)
{
}
public void keyReleased(int keyCode)
{
}
public void pointerPressed(int x, int y)
{
c.pointerPressed(x, y, gml);
}
public void init()
{
c = new call();
img_buffer = Image.createImage(Constant.SCR_WIDTH, Constant.SCR_HEIGHT);
gg = img_buffer.getGraphics();
}
long startTime = 0;
long timeTaken = 0;
public void run()
{
while (isStart)
{
startTime = System.currentTimeMillis();
update();
repaint();
timeTaken = System.currentTimeMillis() - startTime;
if(timeTaken < 1000 / Constant.FPS)
{
try
{
Thread.sleep(1000 / Constant.FPS - timeTaken);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
}
?
逻辑代码 - call
import java.util.Vector; import javax.microedition.io.ConnectionNotFoundException; import javax.microedition.lcdui.Graphics; /** ?* @author 空白节奏 ?* */ public class call { callButten[] cb = null; Vector v = new Vector(0, 1); String stError = ""; public call() { init(); } public void init() { cb = new callButten[13]; for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { cb[(i*3)+j] = new callButten(1+(i*3)+j+"", 20+(j*40), 30+(i*50), 30, 30, -1, 0); } } cb[9] = new callButten("0", 150, 130, 30, 30, -1, 0); cb[10] = new callButten("拨打", 150, 30, 60, 30, -1, 0); cb[11] = new callButten("清除", 150, 80, 60, 30, -1, 0); cb[12] = new callButten("退出", 20, 220, 180, 30, -1, 0); } public void paint(Graphics g) { for(int i = 0; i < cb.length; i++) { cb[i].paint(g); } g.setColor(-1); g.fillRect(20, 180, 180, 30); if(v.size() > 0) { g.setColor(0); for(int i = 0; i < v.size(); i++) { g.drawString(v.elementAt(i).toString(), 22+(i*8), 182, 20); } } g.setColor(255,0,0); g.drawString(stError, 10, 20, 20); } public void updata() { } public void pointerPressed(int x, int y, GameMIDlet gm) { for(int i = 0; i < cb.length; i++) { if(x > cb[i].x && x < (cb[i].x + cb[i].width) && y > cb[i].y && y < (cb[i].y + cb[i].hight)) { if(i < 10) { v.addElement(cb[i].num); } else if(i == 10) { callplat(gm); } else if(i == 11) { if(v.size() > 0) v.removeElementAt(v.size()-1); } else if(i == 12) { gm.quitApp(); } } } } public void callplat(GameMIDlet gm) { String stURL = ""; for(int i=0; i<v.size(); i++) { stURL += v.elementAt(i).toString(); } try { if(!stURL.equals("")) { gm.platformRequest("tel:"+stURL); } } catch (ConnectionNotFoundException e) { stError = e.getMessage(); } } } class callButten { String num; int x, y, width, hight; int bgColor, strColor; callButten(){} callButten(String num, int x, int y, int width, int hight, int bgColor, int strColor) { this.num = num; this.x = x; this.y = y; this.width = width; this.hight = hight; this.bgColor = bgColor; this.strColor = strColor; } void paint(Graphics g) { g.setColor(bgColor); g.fillRect(x, y, width, hight); g.setColor(strColor); g.drawString(num, x+(width>>1)-5, y+(hight>>1)-5, 20); } }
?
?
实现了J2ME的打电话功能,需要MIDP2.0支持,需注意的是,有些机器不支持platformRequest方法,比如我的E680G就不支持,并且API对此方法的描述也是雷人,- - 大概意思是“此方法应该能够拨打指定电话号码”,应该二字很雷人。
?
转载请声明出处。?
?
附件1就是此程序实例。
附件2是jar。