转:J2ME获取手机参数

来源:互联网 发布:php 魔术引用 编辑:程序博客网 时间:2024/05/16 18:17

目标:撰写一个手机系统属性显示程序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 = 30;
    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 -4] = "字体高度: " + f.getHeight();
        propertyStr[arrayLength -3] = "总内存:" + Runtime.getRuntime().totalMemory();       
        propertyStr[arrayLength -2] = "当前空闲内存:" + Runtime.getRuntime().freeMemory();
        propertyStr[arrayLength -1] = "最大空闲内存:" + Runtime.getRuntime().freeMemory();
    }

}

 

 

原创粉丝点击