您的位置:首页 > 编程语言

一起来学J2ME_1:撰写一个手机系统属性显示程序 (附jad文件的错误代码)

2008-11-13 22:20 627 查看
目标:撰写一个手机系统属性显示程序SysInfoMIDlet



开发环境:NetBeans4.1

测试机型:Sun DefaultColorPhone

真机型号:Nokia 6030





功能实现:

1.实现手机系统属性的显示

2.能够响应上,下方向键的消息,使属性信息队列能够在屏幕上滚动显示。

3.能够响应左,右方向键的消息,使过长的属性信息能够在屏幕在左右移动,让用户看得见属性信息的全部内容。

4.能够设置显示字体的大小,并根据字体的大小调节行距。



代码如下,注释以后会慢慢加上的:



文件1:SysInfoMIDlet.java

import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.*;
public class SysInfoCanvas extends GameCanvas implements Runnable,
		CommandListener {
	Display display;
	Font f = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
	boolean isPlay = true;
	int arrayLength = 33;
	int screenWidth = 0;
	int screenHeight = 0;
	int fontDis = 0;
	byte move = 4;
	String[] propertyStr = new String[arrayLength];
	int[] strLength = new int[arrayLength];
	int[][] Loca = new int[arrayLength][3];
	MIDlet midlet;
	public SysInfoCanvas(MIDlet mid, Display dis) {
		super(true);
		midlet = mid;
		display = dis;
		setFullScreenMode(true);
		screenWidth = getWidth();
		screenHeight = getHeight();
		GetProperty();
		fontDis = f.getHeight() + 2;
		for (int j = 0; j < arrayLength; j++) {
			Loca[j][2] = f.stringWidth(propertyStr[j]);
			Loca[j][1] = j * fontDis;
			Loca[j][0] = 0;
		}
		addCommand(new Command("大字体", Command.SCREEN, 1));
		addCommand(new Command("中字体", Command.SCREEN, 1));
		addCommand(new Command("小字体", Command.SCREEN, 1));
		addCommand(new Command("退出", Command.EXIT, 1));
		setCommandListener(this);
	}
	public void start() {
		isPlay = true;
		new Thread(this).start();
	}
	public void stop() {
		isPlay = false;
	}
	public void run() {
		Graphics g = getGraphics();
		while (isPlay) {
			input();
			drawScreen(g);
			try {
				Thread.sleep(80);
			} catch (InterruptedException ie) {
				ie.printStackTrace();
			}
		}
	}
	private void drawScreen(Graphics g) {
		g.setColor(0xffff00);
		g.fillRect(0, 0, screenWidth, screenHeight);
		drawSodino(g);
		g.setColor(0x000000);
		Property(g);
		flushGraphics();
	}
	public void drawSodino(Graphics g) {
		g.setColor(0xf813);
		Font f = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD
				| Font.STYLE_ITALIC, Font.SIZE_LARGE);
		g.setFont(f);
		for (int i = 0; i < 6; i++) {
			g.drawString("Sodino", 30, i * 20, Graphics.LEFT | Graphics.TOP);
		}
	}
	public void input() {
		int key = getKeyStates();
		int temp = 0;
		if (UP_PRESSED == key) {
			temp = Loca[0][1];
			for (int j = 0; j < arrayLength - 1; j++) {
				Loca[j][1] = Loca[j + 1][1];
			}
			Loca[arrayLength - 1][1] = temp;
		} else if (DOWN_PRESSED == key) {
			temp = Loca[arrayLength - 1][1];
			for (int j = arrayLength - 1; j > 0; j--) {
				Loca[j][1] = Loca[j - 1][1];
			}
			Loca[0][1] = temp;
		} else if (LEFT_PRESSED == key) {
			for (int i = 0; i < arrayLength; i++) {
				if (Loca[i][1] >= 0
						&& Loca[i][1] + f.getHeight() <= screenHeight
						&& 0 > Loca[i][0]) {
					Loca[i][0] += move;
				}
			}
		} else if (RIGHT_PRESSED == key) {
			for (int i = 0; i < arrayLength; i++) {
				if (Loca[i][1] >= 0
						&& Loca[i][1] + f.getHeight() <= screenHeight
						&& screenWidth < Loca[i][0] + Loca[i][2]) {
					Loca[i][0] -= move;
				}
			}
		}
	}
	public void Property(Graphics g) {
		g.setFont(f);
		propertyStr[arrayLength - 2] = "当前空余内存: "
				+ Runtime.getRuntime().freeMemory();
		System.gc();
		propertyStr[arrayLength - 1] = "最大空闲内存:"
				+ Runtime.getRuntime().freeMemory();
		for (int i = 0; i < arrayLength; i++) {
			g.drawString(propertyStr[i], Loca[i][0], Loca[i][1], Graphics.LEFT
					| Graphics.TOP);
		}
	}
	public void commandAction(Command c, Displayable dis) {
		if (c.getLabel().equals("退出")) {
			midlet.notifyDestroyed();
		} else if (c.getLabel().equals("大字体")) {
			f = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
					Font.SIZE_LARGE);
			ResetStringWidth();
			modifyLoca1();
		} else if (c.getLabel().equals("中字体")) {
			f = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
					Font.SIZE_MEDIUM);
			ResetStringWidth();
			modifyLoca1();
		} else if (c.getLabel().equals("小字体")) {
			f = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
					Font.SIZE_SMALL);
			ResetStringWidth();
			modifyLoca1();
		}
	}
	public void modifyLoca1() {
		int theTopLoca = 0;
		for (int i = 0; i < arrayLength; i++) {
			if (0 == Loca[i][1]) {
				theTopLoca = i;
				break;
			}
		}
		for (int i = theTopLoca + 1; i < theTopLoca + arrayLength; i++) {
			int temp = i % arrayLength;
			if (temp == 0) {
				Loca[0][1] = Loca[arrayLength - 1][1] + fontDis;
			} else {
				Loca[temp][1] = Loca[temp - 1][1] + fontDis;
			}
		}
	}
	public void ResetStringWidth() {
		propertyStr[arrayLength - 4] = "当前字体高度: " + f.getHeight();
		fontDis = f.getHeight() + 2;
		for (int i = 0; i < arrayLength; i++) {
			Loca[i][2] = f.stringWidth(propertyStr[i]);
		}
	}
	public void GetProperty() {
		propertyStr[0] = "MIDP: " + System.getProperty("microedition.profiles");
		propertyStr[1] = "CLDC: "
				+ System.getProperty("microedition.configuration");
		propertyStr[2] = "字符包: " + System.getProperty("microedition.encoding");
		propertyStr[3] = "手机产国: " + System.getProperty("microedition.locale");
		propertyStr[4] = "手机型号: " + System.getProperty("microedition.platform");
		propertyStr[5] = "串口列表: "
				+ System.getProperty("microedition.commports");
		propertyStr[6] = "手机名: " + System.getProperty("microedition.hostname");
		propertyStr[7] = "支持混音: " + System.getProperty("supports.mixing");
		propertyStr[8] = "视频捕获: "
				+ System.getProperty("supports.audio.capture");
		propertyStr[9] = "声音捕获: "
				+ System.getProperty("supports.vedio.capture");
		propertyStr[10] = "视频格式: " + System.getProperty("audio.encoding");
		propertyStr[11] = "声音格式: " + System.getProperty("vedio.encodings");
		propertyStr[12] = "支持记录: " + System.getProperty("supports.recording");
		propertyStr[13] = "支持3D: "
				+ System.getProperty("microedition.m3g.version");
		if (true == display.flashBacklight(2500)) {
			propertyStr[14] = "背景灯闪烁: true";
		} else {
			propertyStr[14] = "背景灯闪烁: false";
		}
		if (true == display.vibrate(500)) {
			propertyStr[15] = "支持震动: true";
		} else {
			propertyStr[15] = "支持震动: false";
		}
		if (true == display.isColor()) {
			propertyStr[16] = "是否彩色: true";
			propertyStr[17] = "颜色数: " + display.numColors();
			;
		} else {
			propertyStr[16] = "是否彩色: false";
			propertyStr[17] = "颜色数: White and Black Only";
		}
		if (true == this.hasRepeatEvents()) {
			propertyStr[18] = "连续按键: true";
		} else {
			propertyStr[18] = "连续按键: false";
		}
		if (true == this.hasPointerEvents()) {
			propertyStr[19] = "指针事件: true";
		} else {
			propertyStr[19] = "指针事件: false";
		}
		if (true == this.hasPointerMotionEvents()) {
			propertyStr[20] = "指针拖拽: true";
		} else {
			propertyStr[20] = "指针拖拽: false";
		}
		if (true == this.isDoubleBuffered()) {
			propertyStr[21] = "支持双缓存: true";
		} else {
			propertyStr[21] = "支持双缓存: false";
		}
		propertyStr[22] = "蓝牙版本: "
				+ System.getProperty("Bluetooth.api.version");
		propertyStr[23] = "短信服务中心号码: "
				+ System.getProperty("wireless.messaging.sms.smsc");
		propertyStr[24] = "手机存储卡名称: "
				+ System.getProperty("fileconn.dir.memorycard.name");
		propertyStr[25] = "流媒体格式: " + System.getProperty("streamable.contents");
		propertyStr[arrayLength - 7] = "IMEI号: " + getIMEI();
		propertyStr[arrayLength - 6] = "Lac号: " + getLac();
		propertyStr[arrayLength - 5] = "CellID号: " + getCellID();
		propertyStr[arrayLength - 4] = "字体高度: " + f.getHeight();
		propertyStr[arrayLength - 3] = "总内存:"
				+ Runtime.getRuntime().totalMemory();
		propertyStr[arrayLength - 2] = "当前空闲内存:"
				+ Runtime.getRuntime().freeMemory();
		propertyStr[arrayLength - 1] = "最大空闲内存:"
				+ Runtime.getRuntime().totalMemory();
	}
	private String getIMEI() {
		return (System.getProperty("com.sonyericsson.imei") == null ? System
				.getProperty("com.nokia.IMEI") == null ? System
				.getProperty("com.nokia.mid.imei") == null ? System
				.getProperty("phone.imei") == null ? System.getProperty("IMEI") == null ? System
				.getProperty("com.samsung.imei") == null ? System
				.getProperty("com.siemens.imei") == null ? "null" : System
				.getProperty("com.siemens.imei") : System
				.getProperty("com.samsung.imei")
				: System.getProperty("IMEI")
				: System.getProperty("phone.imei")
				: System.getProperty("com.nokia.mid.imei")
				: System.getProperty("com.nokia.IMEI")
				: System.getProperty("com.sonyericsson.imei"));
	}
	private String getLac() {
		return (System.getProperty("com.sonyericsson.net.lac") == null ? System
				.getProperty("com.nokia.mid.lac") == null ? System
				.getProperty("LocAreaCode") == null ? "null" : System
				.getProperty("LocAreaCode") : System
				.getProperty("com.nokia.mid.lac") : System
				.getProperty("com.sonyericsson.net.lac"));
	}
	private String getCellID() {
		return (System.getProperty("com.sonyericsson.net.cellid") == null ? System
				.getProperty("com.nokia.mid.cellid") == null ? System
				.getProperty("com.samsung.cellid") == null ? System
				.getProperty("com.siemens.cellid") == null ? "null" : System
				.getProperty("com.siemens.cellid") : System
				.getProperty("com.samsung.cellid") : System
				.getProperty("com.nokia.mid.cellid")
				: System.getProperty("com.sonyericsson.net.cellid"));
	}
}








本人正在学习J2ME,将自己的所获发于网上与大家分享,不足之处待路过的神仙哥哥神仙姐姐们拍砖提醒之。感激流涕!!!!!!!!





附jad文件的错误代码:



jad ( Java Application Discriptor )
文件是J2ME的一个重要的组成部分,在我们发布J2ME程序的时候,jad文件经常报出各种错误,如:

  com.sun.kvem.midletsuite.InvalidJadException: Reason = 13

  其中的错误原因是
1-52的代码,很让人费解。我在网上找到了这些代码的解释,和大家分享如下:

  public static final int
JAD_SERVER_NOT_FOUND = 1;

  public static final int JAD_NOT_FOUND =
2;

  public static final int MISSING_PROVIDER_CERT = 4;

  public
static final int CORRUPT_PROVIDER_CERT = 5;

  public static final int
UNKNOWN_CA = 6;

  public static final int INVALID_PROVIDER_CERT = 7;

  public static final int CORRUPT_SIGNATURE = 8;

  public static final
int INVALID_SIGNATURE = 9;

  public static final int
UNSUPPORTED_CERT = 10;

  public static final int
EXPIRED_PROVIDER_CERT = 11;

  public static final int EXPIRED_CA_KEY =
12;

  public static final int MISSING_SUITE_NAME = 13;

  public
static final int MISSING_VENDOR = 14;

  public static final int
MISSING_VERSION = 15;

  public static final int INVALID_VERSION = 16;

  public static final int OLD_VERSION = 17;

  public static final int
MISSING_JAR_URL = 18;

  public static final int JAR_SERVER_NOT_FOUND =
19;

  public static final int JAR_NOT_FOUND = 20;

  public static
final int MISSING_JAR_SIZE = 21;

  public static final int
SUITE_NAME_MISMATCH = 25;

  public static final int VERSION_MISMATCH =
26;

  public static final int VENDOR_MISMATCH = 27;

  public
static final int INVALID_KEY = 28;

  public static final int
INVALID_VALUE = 29;

  public static final int INSUFFICIENT_STORAGE =
30;

  public static final int JAR_SIZE_MISMATCH = 31;

  public
static final int NEW_VERSION = 32;

  public static final int
UNAUTHORIZED = 33;

  public static final int JAD_MOVED = 34;

  public static final int CANNOT_AUTH = 35;

  public static final int
CORRUPT_JAR = 36;

  public static final int INVALID_JAD_TYPE = 37;

  public static final int INVALID_JAR_TYPE = 38;

  public static final
int ALREADY_INSTALLED = 39;

  public static final int
DEVICE_INCOMPATIBLE = 40;

  public static final int
MISSING_CONFIGURATION = 41;

  public static final int MISSING_PROFILE
= 42;

  public static final int INVALID_JAD_URL = 43;

  public
static final int INVALID_JAR_URL = 44;

  public static final int
PUSH_DUP_FAILURE = 45;

  public static final int PUSH_FORMAT_FAILURE =
46;

  public static final int PUSH_PROTO_FAILURE = 47;

  public
static final int PUSH_CLASS_FAILURE = 48;

  public static final int
AUTHORIZATION_FAILURE = 49;

  public static final int
ATTRIBUTE_MISMATCH = 50;

  public static final int PROXY_AUTH = 51;

  public static final int TRUSTED_OVERWRITE_FAILURE = 52;

  有了这些代码的解释,
我们就很容易知道错误的原因了。

  另外,大家知道,有的手机在安装jar文件的时候,不需要jad文件。这是因为手机厂商在操作系统中内置了对jar文件的解析功能。就如同手机自动生成
jad文件,然后使用这个jad文件安装jar文件一样。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐