×
Namespaces

Variants
Actions
Revision as of 14:17, 4 June 2009 by tanjaluodes (Talk | contribs)

How to detect if a Class/Package is available on the phone

From Nokia Developer Wiki
Jump to: navigation, search

Java ME has many optional packages that phones can have pre-installed on top of MIDP and CLDC. So, how can one write code that detects on-the-fly if some class or API is available?

We can use the dynamic instantiation method, like this:

boolean MMAPIAvailable;
try {
// Try to instantiate a class using a string as the Class name
// so, the SDK without the API can compile the application
Class.forName("javax.microedition.media.Player").newInstance();
// If the next code is executed, then the API is available
MMAPIAvailable = true;
} catch ( ClassNotFoundException e) {
MMAPIAvailable = false;
}
41 page views in the last 30 days.