IT Community - Software Programming, Web Development and Technical Support

J2ME Tips & Tricks

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 ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > Mobile Software Development > J2ME

Register FAQ Members List Calendar Mark Forums Read
  #41 (permalink)  
Old 09-06-2007, 07:51 AM
jeyaprakash.c jeyaprakash.c is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 228
jeyaprakash.c is on a distinguished road
Arrow Re: J2ME Tips & Tricks

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:
Nokia-MIDlet-Category: Game
__________________
thanx n regards
jeyaprakash.c
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #42 (permalink)  
Old 09-06-2007, 07:52 AM
jeyaprakash.c jeyaprakash.c is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 228
jeyaprakash.c is on a distinguished road
Arrow Re: J2ME Tips & Tricks

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:
String url = "http://forum.nokia.com";
platformRequest(url );
__________________
thanx n regards
jeyaprakash.c
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #43 (permalink)  
Old 09-06-2007, 07:53 AM
jeyaprakash.c jeyaprakash.c is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 228
jeyaprakash.c is on a distinguished road
Arrow Re: J2ME Tips & Tricks

How to know the phone supports fileconnection (JSR-75)



You can know it by using the following code

Quote:
System.getProperty("microedition.io.file.FileConne ction.version");
The method above returns a String different than null if the API exists.
__________________
thanx n regards
jeyaprakash.c
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #44 (permalink)  
Old 09-06-2007, 07:54 AM
jeyaprakash.c jeyaprakash.c is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 228
jeyaprakash.c is on a distinguished road
Arrow Re: J2ME Tips & Tricks

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
	}
}
If you need to know the supported fields in contact list or to-do list, just replace PIM.EVENT_LIST with PIM.CONTACT_LIST or PIM.TODO_LIST. Note, that you will get only the constant values of the fields, so output might look like this: 1: Meeting Supported fields: 108 107 100 106 102. The actual field names can be found from PIM API specification.
__________________
thanx n regards
jeyaprakash.c
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #45 (permalink)  
Old 09-06-2007, 07:55 AM
jeyaprakash.c jeyaprakash.c is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 228
jeyaprakash.c is on a distinguished road
Arrow Re: J2ME Tips & Tricks

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:
Player player = Manager.createPlayer("capture://radio?f=98.3&st=stereo");
// This will tune to 98.3 FM frequency in stereo mode
For the above code to work, it is required that the MMAPI implementation on the device supports "radio" capture locator. This locator is available on a number of Series 40 phones, but not on S60 devices.
__________________
thanx n regards
jeyaprakash.c
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #46 (permalink)  
Old 09-06-2007, 07:56 AM
jeyaprakash.c jeyaprakash.c is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 228
jeyaprakash.c is on a distinguished road
Arrow Re: J2ME Tips & Tricks

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 );
It should also be noted that user is also provided means to initiate a phone call from MIDP LCDUI PHONENUMBER type TextField or TextBox. User is also able to fetch a number from native phone book (or contacts) application to this type of TextField/TextBox. The Call and Fetch operations are available in Options menu or directly from softkeys depending whether there's also application provided Commands in the same Displayable, or Form Item in the case of TextField.
__________________
thanx n regards
jeyaprakash.c
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #47 (permalink)  
Old 09-06-2007, 07:57 AM
jeyaprakash.c jeyaprakash.c is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 228
jeyaprakash.c is on a distinguished road
Arrow Re: J2ME Tips & Tricks

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #48 (permalink)  
Old 09-06-2007, 07:58 AM
jeyaprakash.c jeyaprakash.c is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 228
jeyaprakash.c is on a distinguished road
Arrow Re: J2ME Tips & Tricks

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:
AlertType.ERROR.playSound(Display.getDisplay(this) );
__________________
thanx n regards
jeyaprakash.c
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #49 (permalink)  
Old 09-06-2007, 07:59 AM
jeyaprakash.c jeyaprakash.c is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 228
jeyaprakash.c is on a distinguished road
Arrow Re: J2ME Tips & Tricks

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #50 (permalink)  
Old 09-06-2007, 08:01 AM
jeyaprakash.c jeyaprakash.c is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 228
jeyaprakash.c is on a distinguished road
Arrow Re: J2ME Tips & Tricks

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #51 (permalink)  
Old 09-06-2007, 11:15 PM
jeyaprakash.c jeyaprakash.c is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 228
jeyaprakash.c is on a distinguished road
Arrow Re: J2ME Tips & Tricks

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:
* Include the txt file in your project, so the IDE can append it to the JAR package.
* Use the following code to read a text file from the JAR
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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #52 (permalink)  
Old 09-06-2007, 11:16 PM
jeyaprakash.c jeyaprakash.c is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 228
jeyaprakash.c is on a distinguished road
Arrow Re: J2ME Tips & Tricks

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");
}
Remember:

* 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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #53 (permalink)  
Old 09-06-2007, 11:17 PM
jeyaprakash.c jeyaprakash.c is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 228
jeyaprakash.c is on a distinguished road
Arrow Re: J2ME Tips & Tricks

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 now the image in a byte array.
// We have to transform it in a LCUI Image object
Code:
Image im;
im = Image.createImage(receivedImage, 0, receivedImage.length);
// Now you can use this image to draw it on Canvas or store in RMS
__________________
thanx n regards
jeyaprakash.c
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #54 (permalink)  
Old 09-06-2007, 11:18 PM
jeyaprakash.c jeyaprakash.c is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 228
jeyaprakash.c is on a distinguished road
Arrow Re: J2ME Tips & Tricks

How to retrieve Midlet attribute value from jad



The following code helps to retrieve an attribute value from the jad entry buy using

Quote:
MIDlet.getAppProperty("MIDlet-Vendor")
In this case it will return the value of MIDlet-Vendor from the jad file.

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #55 (permalink)  
Old 09-06-2007, 11:19 PM
jeyaprakash.c jeyaprakash.c is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 228
jeyaprakash.c is on a distinguished road
Arrow Re: J2ME Tips & Tricks

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #56 (permalink)  
Old 09-06-2007, 11:20 PM
jeyaprakash.c jeyaprakash.c is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 228
jeyaprakash.c is on a distinguished road
Arrow Re: J2ME Tips & Tricks

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);
The setCurrent(null) doesn't set the displayable to null but is used in Java ME implementations that support concurrent applications like S60 to send the foreground application to background. The application that is shown is the last application that was shown before the MIDlet got activated. For example, if user launched the MIDlet from application shell just before then application shell is shown.
__________________
thanx n regards
jeyaprakash.c
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #57 (permalink)  
Old 09-06-2007, 11:21 PM
jeyaprakash.c jeyaprakash.c is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 228
jeyaprakash.c is on a distinguished road
Arrow Re: J2ME Tips & Tricks

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #58 (permalink)  
Old 09-06-2007, 11:22 PM
jeyaprakash.c jeyaprakash.c is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 228
jeyaprakash.c is on a distinguished road
Arrow Re: J2ME Tips & Tricks

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #59 (permalink)  
Old 09-06-2007, 11:23 PM
jeyaprakash.c jeyaprakash.c is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 228
jeyaprakash.c is on a distinguished road
Arrow Re: J2ME Tips & Tricks

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #60 (permalink)  
Old 09-06-2007, 11:25 PM
jeyaprakash.c jeyaprakash.c is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 228
jeyaprakash.c is on a distinguished road
Arrow Re: J2ME Tips & Tricks

How to set resoultion of snapshot taken



Code:
//You can list the supported resolutions by:

int []supportedRes = cameraControl.getSupportedStillResolutions();
Quote:
/* the list will be like this and you have to take the index of this as shown

[480, 640, 960, 1280, 1224, 1632, 1500, 2000, 1536, 2048]

index:

0 - 640x480

1 - 1280x960

2 - 1632x1224

3 - 2000x1500

4 - 2048x1536

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.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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