Home >Java >javaTutorial >Summary of comprehensive basic explanations of J2ME program development (2)
17. Using CMWAP proxy in J2ME network programming
The network connections provided by China Mobile are divided into two types: CMNET and CMWAP. Among them, CMNET can have unlimited access to the Internet, and the tariff is relatively expensive. CMWAP is similar to an HTTP code and can only access applications that support HTTP, but the rates are cheap and the stability is relatively poor.
In actual J2ME network programming, it is generally necessary to provide a CMWAP proxy to connect to the network. In J2ME, the connection code is different from the direct connection. The code is as follows:
HttpConnection http = (HttpConnection)Connector.open((( "http://10.0.0.172/"+url);
http.setRequestPRoperty("X-Online-Host",ServerName);
For example, the address you need to access is: http://www.test.com/ login/loginServlet
The above code is:
HttpConnection http = (HttpConnection)Connector.open(("http://10.0.0.172/"+
"login/loginServlet");
http.setRequestProperty("X -Online-Host","www.test.com");
In actual use, you only need to use the domain name or IP of the address you actually need to access instead of ServerName, such as "www.test.com" in the example , use the subsequent address class to replace the url in the code, such as "login/loginServlet" in the example, you can actually use the CMWAP proxy to connect.
18. Time processing in J2ME
Time processing. It is quite common in program development. Here is a brief explanation of time processing
1. Expression of time
There are two expressions of time in J2ME:
1. GMT January 1, 1970, midnight 12. The number of milliseconds between the point and now is represented
This method is suitable for comparing the difference between two times
2. Expressed in the form of objects
2. Time processing related classes
Time processing is involved in J2ME. Three classes:
1. System class
long time = System. currentTimeMillis();
Use this method to get the current time. The expression of time is the first one mentioned above. 2. Date class
Date date = new Date();
Get the current time and express it in the form of an object.
3. Calendar class Calendar calendar = Calendar. getInstance();
3. Specific operations of time processing
1 , Conversion of the above three expressions:
a) Convert the time obtained by the System class into a Date object
Date date = new Date (System. currentTimeMillis());
b) Convert the Date type object to the Calendar type Object
Calendar calendar = Calendar. getInstance();
Date date = new Date();
calendar.setTime(date);
2. Use Calendar to complete some date operations:
Calendar is the most commonly used and has the most functions in time processing A powerful class that can be used to obtain the date, day of the week, and other information at a certain time.
Get the date:
Calendar calendar = Calendar. getInstance();
……
int day = calendar.get(Calendar. DATE);
The operation of getting the date, year, and week is similar to this.
It should be noted that the number representing the month in Calendar differs by 1 from the actual number, that is, January is represented by the number 0, February is represented by the number 1,...December is represented by the number 11.
19. Complete Guide to Random Number Processing in J2ME
Generate random numbers in the program and compare their uses, such as in the field of artificial intelligence, etc. Here is a simple summary of the operations of generating random numbers in J2ME. I hope it will be helpful to everyone Can help.
Different from J2SE, J2ME cannot use the random number of the Math class to generate random numbers. You can only use the Random class of the java.util package to generate random numbers.
1. Create an object of Random type:
Random random = new Random(); It is completely equivalent to the following code:
Random random = new Random(System. currentTimeMillis());
It is equivalent to using the current time as the seed number to create.
The second way is to create by specifying the seed number yourself.
You can use either of the above two methods according to your needs.
2. Generate random numbers:
After creating the random object, we can generate random numbers:
Generate random integers:
int k = random.nextInt();
Generate random long integers:
long l = random.nextLong();
3. Generate numbers in the specified range:
For example, generate random numbers between 0-10:
int k = random.nextInt();
int j = Math.abs(k % 10);
First generate a random integer k, then use k and 10 to take the remainder, and finally use the abs method of the Math class to get the absolute value to obtain a random number between 0-10.
Get a random number between 0-15, similar to:
int k = random.nextInt();
int j = Math.abs(k % 15);
Get a random number between 10-20:
int k = random.nextInt();
int j = Math.abs(k % 10) + 10; —Font can achieve better performance in low-level user interfaces, so how to use the Font class?
First of all, due to the limitations of mobile phone devices, the font types supported by mobile phones are very limited, so in J2ME only the default fonts supported by mobile phones can be used to construct Font class objects. The following are the methods used when creating objects of the Font class:
getFont(int face,int style,int size);
For example:
Font font = Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font. SIZE_MEDIUM);
No matter which parameter, you can only use the values set by the system. The specific sizes of these values may be different on different mobile phones. The following is a detailed introduction to the values of the three parameters:
The face parameter refers to the appearance of the font, and its value is:
FACE_MONOSPACE——Monospaced font
FACE_PROPORTIONAL——Balanced font
FACE_SYSTEM——System font
The style parameter refers to the style of the font, its value:
STYLE_BOLD - bold
STYLE_ITALIC - italic
STYLE_PLAIN - normal
STYLE_UNDERLINED - underline
STYLE_BOLD STYLE_ITALIC - bold italic
STYLE_UNDERLINED STYLE_BOLD - underlined Bold
STYLE_UNDERLINED STYLE_ITALIC - underlined italic
STYLE_UNDERLINED STYLE_ITALIC STYLE_BOLD - underlined bold italic
The size parameter refers to the size of the font, its value:
SIZE_SMALL - small
SIZE_MEDIUM - medium
SIZE_LAR GE——Big
Passed The values of the above parameters can be used to combine the font objects you need.
The following are some commonly used font operations:
1. Get the system's default font:
Font font = Font.getDefaultFont();
2. Inside the panit method, assuming the name of the Graphics parameter is g, get the current font The method is:
Font font = g.getFont();
3. Inside the panit method, assuming the name of the Graphics parameter is g, the method to set the current font is:
g.setFont(font);
where font constructs a font object for you.
4. In MIDP2.0, List can set the font format of each line. The method is:
list.setFont(0,font);
The above code sets the first line in the list to the font type font.
21. Using colors in J2ME mobile program development
In the process of J2ME mobile phone development, colors need to be often used for drawing to enhance the performance of the program. Here is an introduction to how to use colors.
Since J2ME technology is relatively simple, it does not implement a special color class, but only uses the concept of RGB to represent colors. Here is a brief introduction to the concept of RGB. Color is composed of three primary colors: red, green, and blue. Therefore, a combination of these three colors can be used to represent a specific color. Among them, R, G , each value of B is between 0-255. When expressing color, you can use three numbers to express it, or you can use a hexadecimal format such as 0X00RRGGBB to express it. The following is the expression form of common colors:
Red: (255,0,0 ) or 0x00FF0000
Green: (0,255,0) or 0x0000FF00
Blue: (255,255,255) or 0x00FFFFFF
Other colors can also be combined in the above way.
After knowing the expression of color, let’s introduce how to use color in J2ME program. The methods involved are all in the Graphics class, including the following:
1.getColor():
Get the currently used color , the return value is a number in the format of 0x00RRGGBB. For example:
int color = g.getColor();
where g is an object of Graphics type.
2.setColor(int RGB):
Set the color used. For example:
g.setColor(0x00ff0000);
3.setColor(int red, int green, int blue)
It has the same effect as the above method, for example:
g.setColor(255,0,0);
After setting the color used by Graphics, you can draw the specified color when drawing.
22. Obtain the client’s mobile phone number in the J2ME networking application
In the J2ME program development process, for certain needs, it is often necessary to obtain the user's mobile phone number, but this function is not provided in the standard J2ME class library.
When using China Mobile’s CMWAP method to connect to the network, China Mobile will put the user’s mobile phone number in a header information named x-up-calling-line-id. You can obtain the user’s ID by reading the header information. The specific code is as follows:
String usermphone = http.getHeader("x-up-calling-line-id");
Where http is an object of HttpConnction type.
23. Use J2ME to send mobile phone short messages
In the program, there are generally three ways to send short messages:
1. Use the program to send short messages on the network, such as major
The above is Comprehensive basic explanation summary of J2ME program development (Part 2). For more related content, please pay attention to the PHP Chinese website (www.php.cn)!