全面进行真机JSR以及按键测试

http://www.3geye.net/?uid-3-action-viewspace-itemid-265

前阵子看到,有J2me的朋友对真机 测试做了一个比较详细的代码,我重新整理了下,补全了一些参数。不敢独享,现在与大家分享下,体验下开发的快乐

package com.ucfly.jsrdemo;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.media.Manager;
import javax.microedition.midlet.MIDlet;

/**
 *
 * <P>标题:JSRDemoForm类,程序的显示窗口  </P>
 * <P>描述:手机 JSR支持测试.这个类处理的逻辑太多了,不适合</P>
 * <p>在适当的适合,应该采用mvc的结构,Form只管自己绘制UI,逻辑应该放到另外一个类去实现</P>
 * <P>作者:fox</P>

 *
 */

public class JSRDemoForm extends Canvas {


    private static final int MENU = 0;
    private static final int MAIN = 1;
    private static final int KEY = 2;
    private static final int ABOUT = 3;
    private int state;

    private int key;

    private String cldc;
    private String midp;
    private boolean too = true;
    private int width;
    private int height;

    private JSRDemoMIDlet midlet;
    private int sumHeight;

    private static String[] menu_text = {
                                        "主要信息与JSR支持",
                                        " 手机键值测试",
                                        "退出程序"
    };
    private int point = 0;

    private int count = 0;

    private int x = 0; //绘画坐标
    private int y = 0;


    private long total = 0;
    private long free = Runtime.getRuntime().freeMemory();

    private Font big = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
                                    Font.SIZE_LARGE);

    private Font normal = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
                                       Font.SIZE_MEDIUM);

    private Font small = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
                                      Font.SIZE_SMALL);

    private String platform;

    private String encoding;
    private String locale;
    private String m3g;


    private String sms;
    private String mms;

    private String mmapi;

    private String pim;

    private String bluetooth;

    private String fileConnection;
    private String sip;
    private String location;
    private String jtwi;
    private String jsr177;


    /**
     * 构造函数
     *
     */
    public JSRDemoForm(JSRDemoMIDlet midlet) {
        this.midlet = midlet;
        this.init();
        this.getSystemInfo();
    }

    //重构初始化
    private void init() {
        cldc = System.getProperty("microedition.configuration");
        //userAgent = System.getProperty("microedition.platform");
        midp = System.getProperty("microedition.profiles");
        setFullScreenMode(true);
        too = true;
        state = MENU;
        this.width = this.getWidth();
        this.height = this.getHeight();
    }

    protected void paint(Graphics g) {
        clean(g, width, height);
        switch (state) {
        case MENU:
            paintMenu(g);
            break;
        case KEY:
            piantKeyInfo(g);
            break;
        case MAIN:
            paintSystemInfo(g);
            break;

        }

    }

    //绘制 菜单
    private void paintMenu(Graphics g) {
        if (!too) {
            g.setColor(0x00FF00);
            g.drawString("MIDP1.0的手机,", 0, 0, Graphics.LEFT | Graphics.TOP);
            g.drawString("不支持后面的测", 0, 20, Graphics.LEFT | Graphics.TOP);
            g.drawString("试,按任意键退出", 0, 40, Graphics.LEFT | Graphics.TOP);
        } else {
            width = getWidth();
            height = getHeight();
            g.setColor(0xFFFFFF);
            if (count % 2 == 0) {
                g.fillTriangle(width / 2, height / 2 - 30, width / 2 - 5,
                               height / 2 - 20, width / 2 + 5, height / 2 - 20);
                g.fillTriangle(width / 2, height / 2 + 30, width / 2 - 5,
                               height / 2 + 20, width / 2 + 5, height / 2 + 20);
            } else {
                g.fillTriangle(width / 2, height / 2 - 30, width / 2 - 3,
                               height / 2 - 20, width / 2 + 3, height / 2 - 20);
                g.fillTriangle(width / 2, height / 2 + 30, width / 2 - 3,
                               height / 2 + 20, width / 2 + 3, height / 2 + 20);
            }
            g.setColor(0x00FF00);
            g.drawString(menu_text[point], width / 2, height / 2 - 7,
                         Graphics.HCENTER | Graphics.TOP);
            g.drawString("按2,5,8键", width / 2, height,
                         Graphics.HCENTER | Graphics.BOTTOM);
        }
    }

    //绘制key信息
    private void piantKeyInfo(Graphics g) {
        g.setColor(0xFFFFFF);
        g.drawString("键值是:", width / 2, height / 2 - 20,
                     Graphics.HCENTER | Graphics.TOP);
        g.setColor(0x00FF00);
        g.drawString("" + key, width / 2, height / 2,
                     Graphics.HCENTER | Graphics.TOP);
        g.drawString("按5键退出", width / 2, height,
                     Graphics.HCENTER | Graphics.BOTTOM);
    }


    /**获取系统值,因为系统性能问题,所以一开始就载入
     *
     */

    private void getSystemInfo() {
        total = Runtime.getRuntime().totalMemory();
        free = Runtime.getRuntime().freeMemory();

        big = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
                           Font.SIZE_LARGE);

        normal = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
                              Font.SIZE_MEDIUM);

        small = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
                             Font.SIZE_SMALL);

        platform = System.getProperty("microedition.platform");

        encoding = System.getProperty("microedition.encoding");
        locale = System.getProperty("microedition.locale");
        m3g = System.getProperty("microedition.m3g.version");
        m3g = m3g == null ? "false" : m3g;

        sms = System.getProperty(" wireless.messaging.sms.smsc");
        sms = sms == null ? "false" : sms;
        mms = System.getProperty("wireless.messaging.mms.mmsc");
        mms = mms == null ? "false" : mms;

        mmapi = System.getProperty("microedition.media.version");
        mmapi = mmapi == null ? "false" : mmapi;

        pim = System.getProperty("microedition.pim.version");
        pim = pim == null ? "false" : pim;

        bluetooth = System.getProperty("bluetooth.api.version");
        bluetooth = bluetooth == null ? "false" : bluetooth;

        fileConnection = System.getProperty(
                "microedition.io.file.FileConnection.version");
        fileConnection = fileConnection == null ? "false" : fileConnection;
        sip = System.getProperty("microedition.sip.version");
        sip = sip == null ? "false" : sip;
        location = System.getProperty("microedition.location.version");
        location = location == null ? "false" : location;
        jtwi = System.getProperty("microedition.jtwi.version");
        jtwi = jtwi == null ? "false" : jtwi;
        jsr177 = System.getProperty("microedition.smartcardslots");
        jsr177 = jsr177 == null ? "false" : jsr177;

    }

    //绘制并获取JSR支持以及其他相关信息
    private void paintSystemInfo(Graphics g) {
        g.setClip(0, 0, width, height - 20);
        //内存
        g.setColor(0x00FF00);
        g.drawString(">>>", x, y, Graphics.LEFT | Graphics.TOP);
        g.setColor(0xFFFFFF);

        g.drawString("总共内存:" + total, x, y + 20, Graphics.LEFT | Graphics.TOP);

        g.drawString("可用内存:" + free, x, y + 40, Graphics.LEFT | Graphics.TOP);
        //屏幕
        g.setColor(0x00FF00);
        g.drawString(">>>", x, y + 60, Graphics.LEFT | Graphics.TOP);
        g.setColor(0xFFFFFF);
        g.drawString("屏幕宽:" + width, x, y + 80, Graphics.LEFT | Graphics.TOP);
        g.drawString("屏幕高:" + height, x, y + 100, Graphics.LEFT | Graphics.TOP);
        //字体:
        g.setColor(0x00FF00);
        g.drawString(">>>", x, y + 120, Graphics.LEFT | Graphics.TOP);
        g.setColor(0xFFFFFF);

        int fontw = big.stringWidth("国");
        int fonth = big.getHeight();
        g.drawString("大字体(宽&高):" + fontw + "&" + fonth, x, y + 140,
                     Graphics.LEFT | Graphics.TOP);

        fontw = normal.stringWidth("国");
        fonth = normal.getHeight();
        g.drawString("中字体(宽&高):" + fontw + "&" + fonth, x, y + 160,
                     Graphics.LEFT | Graphics.TOP);

        fontw = small.stringWidth("国");
        fonth = small.getHeight();
        g.drawString("小字体(宽&高):" + fontw + "&" + fonth, x, y + 180,
                     Graphics.LEFT | Graphics.TOP);
        //系统属性
        g.setColor(0x00FF00);
        g.drawString(">>>", x, y + 200, Graphics.LEFT | Graphics.TOP);
        g.setColor(0xFFFFFF);
        g.drawString("CLDC:" + cldc, x, y + 220, Graphics.LEFT | Graphics.TOP);
        g.drawString("MIDP:" + midp, x, y + 240, Graphics.LEFT | Graphics.TOP);

        g.drawString("平台:" + platform, x, y + 260, Graphics.LEFT | Graphics.TOP);

        g.drawString("编码:" + encoding, x, y + 280, Graphics.LEFT | Graphics.TOP);

        g.drawString("地区:" + locale, x, y + 300, Graphics.LEFT | Graphics.TOP);

        g.drawString("3D:" + m3g, x, y + 320, Graphics.LEFT | Graphics.TOP);

        g.drawString("WMA SMS:" + sms, x, y + 340,
                     Graphics.LEFT | Graphics.TOP);

        g.drawString("WMA MMS:" + mms, x, y + 360, Graphics.LEFT | Graphics.TOP);

        g.drawString("MMAPI:" + mmapi, x, y + 380, Graphics.LEFT | Graphics.TOP);

        g.drawString("PIM:" + pim, x, y + 400, Graphics.LEFT | Graphics.TOP);

        g.drawString("BlueTooth:" + bluetooth, x, y + 420,
                     Graphics.LEFT | Graphics.TOP);

        g.drawString("FileConnection:" + fileConnection, x, y + 440,
                     Graphics.LEFT | Graphics.TOP);

        g.drawString("SIP:" + sip, x, y + 460, Graphics.LEFT | Graphics.TOP);

        g.drawString("LOCATION:" + location, x, y + 480,
                     Graphics.LEFT | Graphics.TOP);

        g.drawString("JTWI:" + jtwi, x, y + 500, Graphics.LEFT | Graphics.TOP);

        String ch = System.getProperty("CHAPI-Version");
        ch = ch == null ? "false" : ch;
        g.drawString("CHAPI-JSR-211:" + ch, x, y + 520,
                     Graphics.LEFT | Graphics.TOP);

        g.drawString("JSR-177:" + jsr177, x, y + 540,
                     Graphics.LEFT | Graphics.TOP);

        //媒体格式
        g.setColor(0x00FF00);
        g.drawString(">>>", x, y + 560, Graphics.LEFT | Graphics.TOP);
        g.setColor(0xFFFFFF);
        String[] mime = Manager.getSupportedContentTypes(null);
        g.drawString("支持的媒体格式:", x, y + 580, Graphics.LEFT | Graphics.TOP);
        this.sumHeight = 600;
        for (int i = 0; i < mime.length; i++) {
            this.sumHeight = this.sumHeight + 20;
            g.drawString(mime[i], x, y + 600 + i * 20,
                         Graphics.LEFT | Graphics.TOP);
        }

        g.setClip(0, 0, width, height);
        g.setColor(0x00FF00);
        g.drawString("按2,4,5,6,8键", width / 2, height,
                     Graphics.HCENTER | Graphics.BOTTOM);
    }

    private void clean(Graphics g, int width, int height) {
        g.setColor(0x000000);
        g.fillRect(0, 0, width, height);
    }


    public void keyPressed(int keyCode) {
        switch (state) {
        case MENU:
            if (!too) {
                midlet.Exit();
            } else {
                actionMenu(keyCode);
            }
            break;
        case KEY:
            key = keyCode;
            if (key == KEY_NUM5) {
                state = MENU;
            }
            break;
        case MAIN:
            actionMain(keyCode);
            break;
        case ABOUT:
            if (keyCode == KEY_NUM5) {
                state = MENU;
            }
            break;
        }
        repaint();
    }

    private void actionMenu(int keyCode) {
        switch (keyCode) {
        case KEY_NUM8:
            if (point > 0) {
                point--;
            } else {
                point = menu_text.length - 1;
            }
            break;
        case KEY_NUM2:
            if (point < menu_text.length - 1) {
                point++;
            } else {
                point = 0;
            }
            break;
        case KEY_NUM5:
            if (point == menu_text.length - 1) {
                midlet.Exit();
            } else {
                state = point + 1;
            }
            break;
        }
    }

    private void actionMain(int keyCode) {
        switch (keyCode) {
        case KEY_NUM2:

            if(y<=-20)
                y += 20;
            break;
        case KEY_NUM8:


           if(-y<=(this.sumHeight - this.height+20))
               y -= 20;
            break;
        case KEY_NUM4:
            x -= 20;
            break;
        case KEY_NUM6:
            x += 20;
            break;
        case KEY_NUM5:
            state = MENU;
            break;
        }
    }

    protected void sizeChanged(int w, int h) {
        if (h > this.height) {
            this.height = h;
        }
    }


}

你可能感兴趣的:(UI,mvc,360)