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 代表手机的品牌和型号,Nokia手机的返回值格式为“Nokia6310i/4.42”
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
例如,如果用户的手机内置了数码相机,并且支持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;
}
}
}
在J2ME开发中,我们经常需要和手机系统进行交互,获得一些和系统相关的信息,在J2ME API设计中,提供了一系列的系统属性,可以让我们来进行获得,下面就一一进行介绍。
MIDP 1.0和2.0应用程序都可以通过System.getProperty(String key)检测某一个属性的信息。如果该属性有效,将返回对应的字符串,否则,返回null,表示系统不支持此功能。
例如,System.getProperty("microedition.profiles")可能的返回值是"MIDP-1.0"或"MIDP-2.0"。
以下是常见的系统属性和可选API的属性,右侧列出了可能的返回值:
『系统信息』
microedition.platform 代表手机的品牌和型号,Nokia手机的返回值格式为“Nokia6310i/4.42”
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
例如,如果用户的手机内置了数码相机,并且支持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;
}
}
}
在J2ME开发中,我们经常需要和手机系统进行交互,获得一些和系统相关的信息,在J2ME API设计中,提供了一系列的系统属性,可以让我们来进行获得,下面就一一进行介绍。
表1 CLDC、MIDP和JTWI属性
|
属性名称 |
属性作用 |
|
microedition.profiles |
代表手机支持的MIDP版本,返回格式值为“MIDP-1.0”或“MIDP-2.0” |
|
microedition.configuration |
代表手机支持的CLDC版本,返回格式值为“CLDC-1.0”或“CLDC-2.0” |
|
microedition.locale |
代表手机所在的国家或地区,返回值格式为“en-US” |
|
microedition.platform |
代表手机的品牌和型号,Nokia手机的返回值格式为“Nokia6310i/4.42” |
|
microedition.encoding |
代表手机默认的字符集名称,返回值格式为“ISO-8859-1” |
|
microedition.commports |
代表手机可以使用的串口列表,返回值中各个串口之间使用逗号分隔 |
|
microedition.hostname |
MIDP2.0定义,代表本地主机名称,需要手机支持。 |
|
microedition.jtwi.version |
代表手机支持的JTWI版本,值必须是“1.0” |
表2 可选包属性
|
属性名称 |
属性作用 |
|
microedition.media.version |
代表手机支持的MMAPI版本,如果不支持则返回null |
|
microedition.pim.version |
代表手机支持的PIM API版本,如果不支持则返回null |
|
microedition.m3g.version |
代表手机支持的M3G API版本,如果不支持则返回null |
|
microedition.location.version |
代表手机支持的Location API版本,如果不支持则返回null |
|
Bluetooth.api.version |
代表手机支持的BT API版本,如果不支持则返回null |
|
microedition.io.file.
FileConnection.version |
代表手机支持的FC API版本,如果不支持则返回null |
|
microedition.global.version |
代表手机支持的Mobile Internationalization API(JSR-238)版本,如果不支持则返回null |
|
microedition.chapi.version |
代表手机支持的CH(Content Handler) API(JSR211)版本,如果不支持则返回null |
|
microedition.sip.version |
代表手机支持的SIP API版本,如果不支持则返回null |
表3 MMAPI属性
|
属性名称 |
属性作用 |
|
supports.mixing |
代表手机是否支持混音(同时播放多个Player),返回值为“true”或“false” |
|
supports.audio.capture |
代表手机是否支持声音捕获(录音),返回值为“true”或“false” |
|
supports.video.capture |
代表手机是否支持视频捕获(录像),返回值为“true”或“false” |
|
supports.recording |
代表手机是否支持记录(record),返回值为“true”或“false” |
|
audio.encodings |
代表手机支持的声音格式,返回值格式为“encoding=audio/wav”,多个格式之间使用至少一个空格进行间隔 |
|
video.encodings |
代表手机支持的视频格式,返回值格式为“encoding=video/3gpp”,多个格式之间使用至少一个空格进行间隔 |
|
video.snapshot.encodings |
代表手机使用getSnapshot方法获得的视频快照格式,返回值格式为“encoding=png”,多个格式之间使用至少一个空格进行间隔 |
|
streamable.contents |
代表手机支持的流媒体格式,返回null代表不支持 |
表4 Wireless Messaging API属性
|
属性名称 |
属性作用 |
|
wireless.messaging.sms.smsc |
代表手机发送短信时的短信服务中心号码 |
表5 FileConnection API
|
属性名称 |
属性作用 |
|
fileconn.dir.photos |
代表手机中存储照片和其它图片的目录,例如“file:///c:/My files/ Images /” |
|
fileconn.dir.videos |
代表手机中存储视频的目录,例如“file:///c:/My files/Video clips/” |
|
fileconn.dir.tones |
代表手机中存储声音的目录,例如“file:///c:/My files/Tones/” |
|
fileconn.dir.memorycard |
代表手机中存储卡的根目录。例如“file:///d:/” |
|
fileconn.dir.private (Nokia S40不支持) |
代表手机中MIDlet的私有工作目录,例如“file:///c:/System/MIDlets/[1015f294]/scratch” |
|
fileconn.dir.photos.name |
代表手机中图片目录的名称,例如“Images” |
|
fileconn.dir.videos.name |
代表手机中视频目录的名称,例如“Video clips” |
|
fileconn.dir.tones.name |
代表手机中声音目录的名称,例如“Sound clips” |
|
file.separator |
代表手机中的文件分隔符,例如“/” |
|
fileconn.dir.memorycard.name |
代表手机中存储卡的名称,例如“Memory card” |
使用这些属性,可以获得在程序运行过程中需要的很多和系统相关的信息,也可以使用表2中的属性来获得手机是否支持对应的可选包等信息。
实际使用示例:
String name = System.getProperty(“microedition.platform”);
注意:如果需要获得JVM或jad文件中的信息,需要使用MIDlet类中的getAppProperty方法,其属性名则需要查阅jad文件的设定,和本文所述的属性名无关。
本文介绍了如何在J2ME应用程序中检测手机是否支持特定的功能和API,如MMAPI、WMA等,并提供了示例代码说明如何实现。
3198

被折叠的 条评论
为什么被折叠?



