获取设备支持的可选API

J2ME规范包括了许多可选包,如支持多媒体功能的MMAPI,支持消息接收和发送的WMA,支持3D游戏的M3G API。如果某一款手机支持某个可选API,MIDP应用程序就可以使用它。但是,让用户回答“本机是否支持MMAPI”是不友好的,发布几个不同版本不但增加了开发的工作量,也让用户难以选择。因此,应用程序应该自己检测手机是否支持某一API,从而在运行期决定是否可以使用此API。

MIDP 1.0和2.0应用程序都可以通过System.getProperty(String key)检测某一个属性的信息。如果该属性有效,将返回对应的字符串,否则,返回null,表示系统不支持此功能。

例如,System.getProperty("microedition.profiles")可能的返回值是"MIDP-1.0"或"MIDP-2.0"。

以下是常见的系统属性和可选API的属性,右侧列出了可能的返回值:

系统信息


microedition.platform       平台名称,如j2me

microedition.configuration   CLDC或CDC版本,如CLDC-1.0

microedition.profiles       MIDP版本,如MIDP-1.0

microedition.encoding       默认的系统编码,如GBK

microedition.locale         默认的区域设置,如zh-CN

MMAPI相关

microedition.media.version     MMAPI的版本,如1.1

supports.mixing           是否支持混音,如true

supports.audio.capture       是否支持音频捕获,如true

supports.video.capture       是否支持视频捕获,如true

supports.recording         是否支持录音,如true

audio.encodings           音频编码格式,如encoding=pcm encoding=pcm&rate=8000&bits=8&channels=1

video.snapshot.encodings       拍摄图片的编码格式,如encoding=jpeg encoding=png

streamable.contents         支持的流媒体格式,如audio/x-wav


WMA相关

wireless.messaging.sms.smsc     返回SMS的服务中心,如+8613800010000

wireless.messaging.mms.mmsc     返回MMS的服务中心,如http://mmsc.monternet.com


其他

microedition.m3g.version       返回Mobile 3D的版本,如1.0

bluetooth.api.version         返回蓝牙API的版本,如1.0

microedition.io.file.FileConnection.version 返回FileConnection的版本,如1.0

microedition.pim.version       返回PIM的版本,如1.0

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;

import javax.microedition.midlet.MIDlet;

/**
   Creates the "Hello world" program in J2ME MIDP.

   Note that the class must be public so that the device
   application management software can instantiate it.
*/
public class HelloWorld extends MIDlet
{
  ...

  public void startApp()
  {
    // Create a Displayable widget.
    form = new Form("Hello World");

    // Add a string to the form.
    String msg = "My first MIDlet!";
    form.append(msg);

    // This app simply displays the single form created
    // above.
    display = Display.getDisplay(this);
    display.setCurrent(form);

    printSystemProperties();
    printAppProperties();
  }

  /**
     Prints the values of the standard system properties
     using the System.getProperty() call.
  */
  protected void printSystemProperties()
  {
    String conf;
    String profiles;
    String platform;
    String encoding;
    String locale;

    conf = System.getProperty("microedition.configuration");
    System.out.println(conf);

    profiles = System.getProperty("microedition.profiles");
    System.out.println(profiles);

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

    encoding = System.getProperty("microedition.encoding");
    System.out.println(encoding);

    locale = System.getProperty("microedition.locale");
    System.out.println(locale);
    System.out.println();
  }
  /**
    Prints application properties using the
    MIDlet.getAppProperty() call.
*/
protected void printAppProperties()
{
   System.out.println(getAppProperty("MIDlet-Name"));
   System.out.println(getAppProperty("MIDlet-Jar-Size"));
   System.out.println(getAppProperty("MIDlet-Jar-URL"));
   System.out.println(getAppProperty("MIDlet-Vendor"));
}

}

例如,如果用户的手机内置了数码相机,并且支持MMAPI,我们就可以在MIDP程序中拍摄照片。因此,在应用程序启动时就应该判断是否启用拍照功能以及用户手机支持的图片编码格式:

boolean supports_take_photo = false;
boolean supports_jpeg_encoding = false;
boolean supports_png_encoding = false;
boolean supports_gif_encoding = false;
if(System.getProperty("microedition.media.version")!=null) {
  if("true".equals(System.getProperty("supports.video.capture")))
    supports_take_photo = true;
    String all_encoding = System.getProperty("video.snapshot.encodings");
    if(all_encoding!=null) {
        if(all_encoding.indexOf("jpeg")!=(-1))
          supports_jpeg_encoding = true;
        if(all_encoding.indexOf("png")!=(-1))
          supports_png_encoding = true;
        if(all_encoding.indexOf("gif")!=(-1))
          supports_gif_encoding = true;
    }
  }
}

 The current set of system properties for use by the getProperty(String) method is returned as a Properties object. If there is no current set of system properties, a set of system properties is first created and initialized. This set of system properties always includes values for the following keys:

Key      Description of Associated Value

java.version

Java Runtime Environment version
java.vendorJava Runtime Environment vendor
java.vendor.urlJava vendor URL
java.homeJava installation directory
java.vm.specification.versionJava Virtual Machine specification version
java.vm.specification.vendorJava Virtual Machine specification vendor
java.vm.specification.nameJava Virtual Machine specification name
java.vm.versionJava Virtual Machine implementation version
java.vm.vendorJava Virtual Machine implementation vendor
java.vm.nameJava Virtual Machine implementation name
java.specification.versionJava Runtime Environment specification version
java.specification.vendorJava Runtime Environment specification vendor
java.specification.nameJava Runtime Environment specification name
java.class.versionJava class format version number
java.class.pathJava class path
java.library.pathList of paths to search when loading libraries
java.io.tmpdirDefault temp file path
java.compilerName of JIT compiler to use
java.ext.dirsPath of extension directory or directories
os.nameOperating system name
os.archOperating system architecture
os.versionOperating system version
file.separatorFile separator ("/" on UNIX)
path.separatorPath separator (":" on UNIX)
line.separatorLine separator ("/n" on UNIX)
user.nameUser's account name
user.homeUser's home directory
user.dirUser's current working directory

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值