This is a discussion on J2ME Tips & Tricks within the J2ME forums, part of the Mobile Software Development category; How to install an application on the Game phone's menu By default, when a user install a Java ME ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| How to install an application on the Game phone's menu By default, when a user install a Java ME application, it is installed in the default folder on in "Applications" folder. If you want to suggest a Nokia device to install the JAR file in the Game menu of the mobile device, you have to insert this property in the JAD file of your game: Quote:
__________________ thanx n regards jeyaprakash.c |
| Sponsored Links |
| |||
| How to invoke web browser from Java ME The default phone web browser can be invoked from within the Java ME application by using javax.microedition.midlet.MIDlet.platformRequest() . On some devices the MIDlet has to be closed (for example on Series 40 devices), before the browser can be launched. The application can take this into account by checking the return value of this method call. The value is true, if the MIDlet suite MUST first exit before the content can be fetched. The following code is used for invoking the webbrowser Quote:
__________________ thanx n regards jeyaprakash.c |
| |||
| How to know the phone supports fileconnection (JSR-75) You can know it by using the following code Quote:
__________________ thanx n regards jeyaprakash.c |
| |||
| How to list the supported fields in a PIMList When using PIM API and its PIMList class, it is important to find out first, what fields are supported by the implementation. This can be done by using the following kind of method: p Code: rivate void readSupportedFields() {
System.out.println("readEvents()");
try {
String[] lists = PIM.getInstance().listPIMLists(PIM.EVENT_LIST);
int length = lists.length;
EventList[] eventList = new EventList[length];
for (int i = 0; i < length; i++) {
eventList[i] = (EventList) PIM.getInstance().openPIMList(PIM.EVENT_LIST, PIM.READ_WRITE, lists[i]);
System.out.println(i+1+": "+lists[i]);
int[] fields = eventList[i].getSupportedFields();
System.out.println("Supported fields:");
for (int j = 0; j < fields.length-1; j++) {
System.out.println(fields[j]+" ");
}
}
} catch (PIMException pe) {
// no such list
} catch (SecurityException se) {
// MIDlet is not allowed access to the specified list
}
}
__________________ thanx n regards jeyaprakash.c |
| |||
| How to listen to radio in Java ME The following is the code that can be used to listen to a radio in Java ME: Quote:
__________________ thanx n regards jeyaprakash.c |
| |||
| How to make a telephone call from Java ME A telephone call can be made from a Java ME application by using javax.microedition.midlet.MIDlet.platformRequest() . On some devices the MIDlet has to be closed (for example on Series 40 devices), before the phone call can be placed. The application can take this into account by checking the return value of this method call. The value is true, if the MIDlet suite MUST first exit before the call can be made. It is also possible to send post-dial DTMF tones along the phone number. However, it is not usually possible to send a DTMF tones sequence to operator service from a MIDlet. On some devices (on Series 40 side) the length of the dial-string is limited. The following code is used for making the telephone call Code: String telNo = "tel:+9682651761543"; platformRequest(telNo );
__________________ thanx n regards jeyaprakash.c |
| |||
| How to make an animation loop If you need to make some animation in Java ME Canvas, you must use a Game Loop using multithreading. This code will be useful as a template for doing such animation. Code: class MyAnimation extends Canvas implements Runnable {
private Thread thread;
private boolean executing;
private final int SLEEP = 200;
public void start() {
executing=true;
thread = new Thread(this);
thread.start();
}
public void stop() {
executing = false;
}
public void run() {
// Do some initial action
while (executing) {
// move objects or sprites to the next animation frame
repaint();
serviceRepaints();//repaint is a just a request whereas
//servicerepaint is a command to repaint all
//ur pending repaint requests
}
try {
// Send the thread to "sleep" for a couple of milliseconds
Thread.sleep(SLEEP);
} catch(Exception e) {}
}
public void paint() {
// Draw objects on screen
}
}
__________________ thanx n regards jeyaprakash.c |
| |||
| How to play a beep in MIDP 1.0 without other API If you are developing a MIDP 1.0 application for older devices, you don't have access to additional APIs (like Nokia UI API and MMAPI) you don't have access to any multimedia feature. But, with this trick, you can try to beep the user. You don't have control of how the beep works, and if the beep works. But, you can try it, and it is 100% MIDP 1.0 compatible. The trick is to use the AlertType class to call the sound that the phone's UI use when shows an alert of some type, for example, Error. So, the code is: Quote:
__________________ thanx n regards jeyaprakash.c |
| |||
| How to play mp3 from server in Java ME The method below shows how an mp3 file can be played from a server in Java ME. Code: public void playAudio()
{
try
{
String url = "http://server/audio.mp3";
HttpConnection conn = (HttpConnection)Connector.open(url,
Connector.READ_WRITE);
InputStream is = conn.openInputStream();
player = Manager.createPlayer(is,"audio/amr");
player.realize();
// get volume control for player and set volume to max
vc = (VolumeControl) player.getControl("VolumeControl");
if(vc != null)
{
vc.setLevel(100);
}
player.prefetch();
player.start();
}
catch(Exception e)
{}
}
__________________ thanx n regards jeyaprakash.c |
| |||
| How to read a binary file from JAR If you want to read a binary file from JAR file (read only), for example a game level, a map, or any data information you have to: * Include the data file in your project, so the IDE can append it to the JAR package. * Use the following code to read a binary file from the JAR Code: private byte[] readBinaryFile(String fileName) throws IOException {
InputStream input = getClass().getResourceAsStream(fileName);
ByteArrayOutputStream output = new ByteArrayOutputStream(1024);
byte[] buffer = new byte[512];
int bytes;
while ((bytes = input.read (buffer)) > 0) {
output.write (buffer, 0, bytes);
}
input.close ();
return output.toByteArray();
}
__________________ thanx n regards jeyaprakash.c |
| |||
| How to read a text file from JAR If you want to read a text file from JAR file (read only), you have to: Quote:
Code: private String readTextFile(String fileName) throws IOException {
InputStream input = getClass().getResourceAsStream(fileName);
ByteArrayOutputStream output = new ByteArrayOutputStream(1024);
byte[] buffer = new byte[512];
int bytes;
while ((bytes = input.read (buffer)) > 0) {
output.write (buffer, 0, bytes);
}
input.close ();
return new String(output.toByteArray());
}
__________________ thanx n regards jeyaprakash.c |
| |||
| How to read an image file from the JAR To read an image file from the JAR to an Image object, you should use this code: Code: try {
Image logo = Image.createImage("/path_to_the_image");
}
catch (java.io.IOException e) {
System.err.println("Image read error");
} * You must use the first slash "/" to refer to the root of the JAR file. * You must include the image in the project to be included in the JAR file by the IDE you are using. * To be 100% standard to all devices, the image should be in PNG format. Many Nokia devices supports GIF, JPG and BMP to.
__________________ thanx n regards jeyaprakash.c |
| |||
| How to receive an image from the server If you want to receive an image from the server (like a PNG file) with Java ME you should use this code: // This snippet only works if the server send the image length Code: HttpConnection c = (HttpConnection) Connector.open("http://www.mydomain.com/myimage.png");
DataInputStream response = new DataInput(c.openInputStream());
byte[] receivedImage = new byte[c.getLength()];
response.readFully(receivedImage);
response.close(); // We have to transform it in a LCUI Image object Code: Image im; im = Image.createImage(receivedImage, 0, receivedImage.length);
__________________ thanx n regards jeyaprakash.c |
| |||
| How to retrieve Midlet attribute value from jad The following code helps to retrieve an attribute value from the jad entry buy using Quote:
In addition to the required attributes, you can also store your own settings in the JAD file (like the server to connect to), and access them with getAppProperty method call. Note that you can change the contents of the jad file even after signing.
__________________ thanx n regards jeyaprakash.c |
| |||
| How to send Binary SMS in Java ME The following Java ME tip explains a method of sending binary messages such as PNG images, sound files etc. In the program BinaryMessage interface represents a binary message. The setPayloadData() method sets the value of the payload in the data container without checking whether the value is valid or not. Code: void sendSMS(byte data[]) {
try {
String destAddress = "sms://9772625262:5000";
MessageConnection smsConnection =
(MessageConnection)Connector.open(destAddress);
//Create binary message
BinaryMessage binaryMSG = (BinaryMessage)smsConnection.newMessage(
MessageConnection.BINARY_MESSAGE);
//Setting destination add
binaryMSG.setAddress(destAddress);
//Add payload data
binaryMSG.setPayloadData(data);
//Now send the message
smsConnection.send(binaryMSG);
smsConnection.close();
} catch(Exception e) {
}
__________________ thanx n regards jeyaprakash.c |
| |||
| How to send MIDlet to background Following code can be used to send MIDlet to background: Code: Display display = Display.getDisplay(myMIDletClass); display.setCurrent(null);
__________________ thanx n regards jeyaprakash.c |
| |||
| How to send POST data to a web server Using Java ME with MIDP 1.0 or MIDP 2.0 you can send and receive information from/to a webserver using HTTP protocol. If you want to send data using POST method, you should use this code: First load the required libraries: Code: import javax.microedition.io.*;
import java.io.*;
Then use this code in a function:
HttpConnection c = (HttpConnection) Connector.open("http://www.domain.com/url");
c.setRequestMethod(HttpConnection.POST);
byte[] data;
// data should be filled with binary data to send
c.setRequestProperty("Content-Length", Integer.toString(data.length));
OutputStream sending = c.openOutputStream();
sending.write(data);
sending.close();
If you want to send POST parameters to be read by PHP, ASP.NET or other server platform, you should make a string parameter like this
// This is a sample
String strData = "name=" + game.getName() + "&score=" + game.getScore();
byte[] data = strData.getBytes();
And also, you have to define an HTTP parameter like this:
c.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
__________________ thanx n regards jeyaprakash.c |
| |||
| How to set image format in JSR 234 From Forum Nokia Wiki Jump to: navigation, search Code: imageFormatControl = (ImageFormatControl)player.getControl("javax.microedition.amms.control. ImageFormatControl");
imageFormatControl.setFormat("image/jpeg");
__________________ thanx n regards jeyaprakash.c |
| |||
| How to set image quality of snapshot taken imageFormatControl = (ImageFormatControl)player.getControl("javax.micro edition.amms.control. ImageFormatControl"); imageFormatControl.setParameter(FormatControl.PARA M_QUALITY, 100);
__________________ thanx n regards jeyaprakash.c |
| |||
| How to set resoultion of snapshot taken Code: //You can list the supported resolutions by: int []supportedRes = cameraControl.getSupportedStillResolutions(); Quote:
Code: ameraControl = (CameraControl)player.getControl("CameraControl");
cameraControl.setStillResolution(1);
__________________ thanx n regards jeyaprakash.c Last edited by jeyaprakash.c : 09-06-2007 at 11:27 PM. |
![]() |
| Thread Tools | |
| Display Modes | |
| |
LinkBacks (?)
LinkBack to this Thread: http://www.discussweb.com/j2me/3374-j2me-tips-tricks.html | |||
| Posted By | For | Type | Date |
| DiscussWeb IT Community - Technical Support and Technology Discussions | This thread | Refback | 08-21-2007 03:24 AM |