K_java平台上的开发时候,由于不同的支持J2ME的手持设备的性能是不同的,我们希望能够获得设备的具体信息,比如设备是否支持彩色以及支持的颜色数量、屏幕的宽度和高度、是否支持触摸屏等等。前者在Display类中通过isColor()方法和numColors()方法来获取,后者则可以通过Canvas类中getWidth()等方法实现。以下给出示例代码供参考(代码在MIDP1.0、CLDC1.0下运行通过):

DevParaCap.java

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

 
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;

 
/*
 * Created on 2005-5-16
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

 
/**
 * @author k_eckel
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

 
public class DevParaCap extends MIDlet implements CommandListener{
       private Display dis = null;
       private Command cmdExit;
       private Canvas canvas;

 
       /**
        * 
        */
       public DevParaCap() {
              super();
              // 
              cmdExit = new Command("Exit",Command.EXIT,1);
              canvas = new Canvas()
              {
                     public void paint(Graphics g){
                            }
              };
              
              canvas.addCommand(cmdExit);
              canvas.setCommandListener(this);
       }

 
       /* (non-Javadoc)
        * @see javax.microedition.midlet.MIDlet#startApp()
        */
       protected void startApp() throws MIDletStateChangeException {
              // TODO Auto-generated method stub
              dis = Display.getDisplay(this);
              getDevCapability(dis,canvas);
              
              dis.setCurrent(canvas);

 
       }

 
       /* (non-Javadoc)
        * @see javax.microedition.midlet.MIDlet#pauseApp()
        */
       protected void pauseApp() {
              // TODO Auto-generated method stub

 
       }

 
       /* (non-Javadoc)
        * @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
        */
       protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
              // TODO Auto-generated method stub
              this.notifyDestroyed();

 
       }
       
       public static void getDevCapability(Display d,Canvas c)
       {
              System.out.println("支持彩色: " + d.isColor());
              System.out.println("颜色数量: " + d.numColors());
              System.out.println("正常屏幕宽度: " + c.getWidth());
              System.out.println("正常屏幕高度: " + c.getHeight());
              System.out.println("支持触摸屏设备: " + c.hasPointerEvents());
              System.out.println("支持触摸屏拖放: " + c.hasPointerMotionEvents());
              System.out.println("支持重复按键事件: " + c.hasRepeatEvents());
              
       }
       
       public void commandAction(Command c,Displayable d)
       {
              if (c == cmdExit)
              {
                     try
                     {
                            destroyApp(false);
                            notifyDestroyed();
                     }
                     catch (MIDletStateChangeException e)
                     {
                     e.printStackTrace();
                     }
              }
              
       }
}


       在模拟器上运行的结果为:

支持彩色: true

颜色数量: 4096

正常屏幕宽度: 240

正常屏幕高度: 289

支持触摸屏设备: false

支持触摸屏拖放: false

支持重复按键事件: false


   结果的含义我就不罗嗦了:)。