Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to optimize J2ME applications

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to optimize J2ME applications". In daily operation, I believe many people have doubts about how to optimize J2ME applications. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to optimize J2ME applications"! Next, please follow the editor to study!

If there is any difference between j2me applications and j2se applications, it is the environment in which they are restricted to run.

The main bottleneck of many J2ME systems is the amount of memory available to store and run applications. For example, many current MIDP devices, they

The amount of memory limited to the application is 50K or less, a long way from a server-based J2SE environment that may require megabytes.

Since you can easily encounter these limitations in your development, in this J2ME technical tip, you will learn how to make your application take up the most

Less memory. You will use these techniques to reduce the space taken up by MIDlet, where MIDlet simply displays a text box in which the content is changed.

Make a sound at the time:

Package com.j2medeveloper.techtips

Import Javax.microedition.lcdui.*

Public class BeforeSizeOptimization extends

BasicMIDlet {

Public static final Command exitCommand =

New Command ("Exit"

Command.EXIT, 1)

Public BeforeSizeOptimization () {

}

Protected void initMIDlet () {

GetDisplay () .setCurrent (new MainForm ()

}

Public class MainFoRM extends Form {

Public MainForm () {

Super ("MainForm")

AddCommand (exitCommand)

Append (textf)

SetCommandListener (new CommandListener () {

Public void commandAction (Command c

Displayable d) {

If (c = = exitCommand) {

ExitMIDlet ()

}

}

}

);

SetItemStateListener (

New ItemStateListener () {

Public void itemStateChanged (

Item item) {

If (item = = textf) {

AlertType.INFO.playSound (

GetDisplay ()

}

}

}

);

}

Private TextField textf =

New TextField ("Type anything", null

20, 0)

}

}

Although this example only demonstrates MIDlet, the techniques used can be used to optimize any J2ME program.

Note that the MIDlet demonstrated above depends on the following class:

Package com.j2medeveloper.techtips

Import javax.microedition.lcdui.*

Import javax.microedition.midlet.*

Public abstract class BasicMIDlet extends MIDlet {

Private Display display

Public BasicMIDlet () {

}

Protected void destroyApp (boolean unconditional)

Throws MIDletStateChangeException {

ExitMIDlet ()

}

Public void exitMIDlet () {

NotifyDestroyed ()

}

Public Display getDisplay () {return display;}

Protected abstract void initMIDlet ()

Protected void pauseApp () {

}

Protected void startApp ()

Throws MIDletStateChangeException {

If (display = = null) {

Display = Display.getDisplay (this)

InitMIDlet ()

}

}

}

After you package it with J2ME Wireless Toolkit, this MIDlet example is only a little over 4K.

The first step in reducing size is to remove unnecessary classes by reducing the functionality of the application. Is every feature of your application really necessary?

Can your users tolerate the absence of ringtones and whistles? Let's build a minimal version of the application. Notice that the MIDlet example is already quite small.

The second step focuses on the internal classes defined by the application, especially anonymous classes. Keep in mind that each class file has a corresponding space expense. Even the most insignificant

Classes also have a cost:

Public class foo {

/ / nothing here

}

By compiling this class, you can find that you have a class file that takes up almost 200byte space. The general purpose of an anonymous class is to implement event listening.

The MIDlet example defines two listener. A simple optimization is to have the main class of MIDlet implement CommandListener and ItemStateListener

Two interfaces, delete the listener code inside. Remember that multiple objects can use the same listener. If there is a need to distinguish

You can pass parameters to the commanAction and itemStateChanged methods.

Inner classes also inflate code in other ways, because the compiler must generate different variables and methods to allow an inner class to access private information in the wrapper class.

The third step is to maximize the use of built-in classes. For example, don't create your own collection classes in CLDC-based programs. Try to use the built-in Hashtable

And Vector. It's the same with designing MIDP applications. The MIDlet example defines a form subclass to create its main form, but it can also be easily created directly:

MainForm = new Form ("MainForm")

MainForm.addCommand (okCommand)

MainForm.setCommandListener (listener)

There is no question of right or wrong, it's just some simple things to consider.

The fourth step is to cancel the inheritance of your application. You may have broken down the code into one or more abstract classes, which is recommended to improve code reuse

The OOD design method of. It may contradict what you have learned, but it makes more sense to simplify the hierarchy of inheritance. If your abstract class-- probably from another project--

If you are inherited only once, it is particularly reasonable to simplify the inheritance relationship. The MIDlet example extends the BaicMIDlet class, but the two classes are easily combined into one.

The fifth step is to shorten your package name, class name, method name and data member name. This step may seem silly, but an class file holds a lot of character information.

Shortening their names will reduce the size of clas files. This approach won't save much, but they will increase when it extends to multiple classes. The package name is special.

It should be shortened. Because the MIDP application is completely independent, you can completely avoid the package name-- no chance to conflict with the names of other classes of the device. MIDlet example

It can be removed from the com.j2medeveloper.techtips package.

Note that shortening names are not what you want to do by hand, we can use a tool called "scrambler" to do it. The main purpose of the scrambler is

To "hide" the code for the application, it decompiles the program into something that is almost impossible to read. Another effect of this process is to reduce the size of the program.

That's because the hidden code is mainly by renaming method and data members. Here is a developer source code scrambler called RetroGuard, a free tool from

Http://www.retrologic.com, and there are a lot of commercial packages on it.

Finally, focus on the initialization of some arrays. (the MIDlet example does not do any array initialization, but this is a very important step in the program.) when compiled, an array is initialized

The code looks like this:

Int arr [] = {0,1,2,3}

The actual generated code is as follows:

Arr [0] = 0

Arr [1] = 1

Arr [2] = 2

Arr [3] = 3

If you want to study, you can use the javap tool released with java2sdk to break the byte code into a class (using the-c argument). You may be surprised and dissatisfied.

What you see. The two options are: (1) encode the data into a String, decode it to an array at run time, or (2) save the data as a binary file

Go to the package and access it at run time using the getReourceAsStream method of class loader.

What is said here is just some guidance, and not every step mentioned here is suitable for every J2ME application. However, most of them can apply this MIDlet example.

The optimized version of MIDlet is as follows:

Import javax.microedition.lcdui.*

Import javax.microedition.midlet.*

Public class ASO extends MIDlet

Implements CommandListener

ItemStateListener {

Private Display display

Private Form mainForm

Private TextField mainFormTF =

New TextField ("Type anything", null

20, 0)

Public static final Command exitCommand =

New Command ("Exit"

Command.EXIT, 1)

Public ASO () {

}

Public void commandAction (Command c

Displayable d) {

If (c = = exitCommand) {

ExitMIDlet ()

}

}

Protected void destroyApp (boolean unconditional)

Throws MIDletStateChangeException {

ExitMIDlet ()

}

Public void exitMIDlet () {

NotifyDestroyed ()

}

Public Display getDisplay () {return display;}

Protected void initMIDlet () {

MainForm = new Form ("MainForm")

MainForm.addCommand (exitCommand)

MainForm.setCommandListener (this)

MainForm.setItemStateListener (this)

MainForm.append (mainFormTF)

GetDisplay () setCurrent (mainForm)

}

Public void itemStateChanged (Item item) {

If (item = = mainFormTF) {

AlertType.INFO.playSound (getDisplay ())

}

}

Protected void pauseApp () {

}

Protected void startApp ()

Throws MIDletStateChangeException {

If (display = = null) {

Display = Display.getDisplay (this)

InitMIDlet ()

}

}

}

At this point, the study on "how to optimize J2ME applications" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report