Handling Optional APIs in J2ME

断了今生、忘了曾经 提交于 2019-12-22 18:33:35

问题


What is the right way of working with optional APIs in Java Mobile?

  • Does one need to make different versions of their app?
  • Or is it enough to check APIs availability at runtime using System.getProperty()?

Let's say I'd like my app to support JSR-256 (the Sensor API). Would importing classes from javax.microedition.sensor and registering variables of these types break my app if the device doesn't support it?

I am sure there must be a pretty standard way of handling that situation.

Thanks!


回答1:


You can use system properties, or Class.forName() to see if the API is available. Just make sure classes using those APIs don't get loaded on handsets that don't support them. You can make one version of your midlet that supports many different handsets.




回答2:


There are two ways of determining what APIs are supported. If you are going to take the system property route, look at the list here. Classes can also be used, although you have to be careful. The harder question is ensuring that classes don't get loaded:

The article linked to by Albus has the following quote:

Class loaders (see below) can opt to load a type early in anticipation of eventual use. If this strategy is chosen, the class loader must not report any problem (by throwing a subclass of java.lang.LinkageError) encountered during loading until the type's first active use.

So, if the class isn't actually used at runtime, then you shouldn't run into any errors. Of course, Java ME has many implementations and I'm not sure whether it is safe to trust them all to implement it correctly.



来源:https://stackoverflow.com/questions/3591705/handling-optional-apis-in-j2me

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!