Let the JavaME program implement real RunAnywhere-general Linux technology-Linux programming and kernel information. The following is a detailed description. Please advise, tengshiquan@yahoo.com.cn
"Write once, run anywhere" is the slogan of JAVA, but it is very difficult to run anywhere without changing the code for applications made on the j2's platform. If you want to apply an application to most models, you must consider all aspects. The difficulty is quite high.
For example, if you are playing games for the treasure chest, most of the online models are MIDP1. 0 machines, I feel that the most troublesome API to be transplanted is to count the voice part, which must be changed according to each model. Although the image is easy to make adaptive, the sound part can only be changed according to each model.
The following provides a solution that enables the agent to automatically load the sound resources supported by the model and use
Model sound API for playing.
Key issues:
1. The APIS for playing music vary with models, especially older models.
It must be automatically loaded according to the model at runtime.
2. Different models support different audio resource files. It must be automatically loaded according to the model at runtime.
3. Different models of JVM have more or less special bugs.
Solution:
1. Principle: Standard APIs can be used with standard APIs, and APIs of various models can be used if standard APIs are not used.
// Player types
Static final int STANDARD = 0; // For MIDI
Static final int NOKIA = 1; // For ott
Static final int SAMSUNG = 2; // For mmf
Static final int NEC = 3; // For MIDI
Static final String [] supportedPlayerTypes = {
"Javax. microedition. media. Player", // STANDARD API
"Com. nokia. mid. sound. Sound", // Nokia
"Com. samsung. util. AudioClip", // samsung
"Com. nec. media. AudioClip", // nec
};
The reflection mechanism is used for dynamic loading:
Public void determinePlayerType (){
// Use most-> less use
Issuppsound sound = true;
For (int I = 0; I <supportedPlayerTypes. length; I ++ ){
// Try to load a proper sound Player
Try {
Class. forName (supportedPlayerTypes ); // Load the current Player type
PlayerType = I; // Save the type of the loaded class
Return;
} Catch (Exception e) {// loading failed, indicating that it is not supported. Continue to load the following
E. printStackTrace ();
}
}
Issuppsound sound = false;
}
2. You can load the audible resources that can be played based on the type of Successful loading.
Public void createPlayer (String name ){
If (! IsSupportSound)
Return;
Switch (playerType ){
Case STANDARD: // for MIDI
Case NEC:
CreatePlayerFactory ("/" + name + ". mid ");
Break;
Case NOKIA: // for ott
CreatePlayerFactory ("/" + name + ". ott ");
Break;
Case SAMSUNG: // for mmf
CreatePlayerFactory ("/" + name + ". mmf ");
Break;
}
3. There is no special way to debug bugs specific to each model. This can only blame the vendor...
Advantages of this solution: you do not need to change the code During porting. You only need to keep the relevant resources in the JAR package of the corresponding model. In this way, you don't have to go over all models.
Note: using System. getProperty ("microedition. platform") to determine the model is not safe, because some models simply return the j2platform.
Legacy problems:
[1] when running Class. forName ("javax. microedition. media. Player"), NecN820 immediately reports an "application error" instead of throwing a "ClassNotFoundException" exception. This is the JVM feature of this model (BUG), so all information about javax. microedition. media. Player must be commented out in the code of NecN820. This requires code modification, which is against our original intention and is indeed a pity. (It is estimated that all models of NEC are similar)
[2] This class is still to be extended to support more models. And add the vibration part of the API. Theoretically, all models can be included. However, in actual applications, you only need to include the model-related APIs you need.
Testing model: Samsung E708, MOTOV600, NOKIA 7650, and NecN820 (comment out javax. microedition. media. Player) are all tested.
Below is the source program
Http://www.pconline.com.cn/pcedu... c/05-09-05-j2me.txt
In addition, for program compilation, you can add the APIs of various models as LIB to the eclipse project.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service