Wednesday, January 20, 2010

Archive for the ‘newbie’ Category:

ads

How to generate Random Numbers in J2ME application

One of the popular question asked in this blog is ways to generate random numbers in J2ME application.

As most people probably realized, the JavaME CLDC does not include the Math.Random class to generate random numbers. However one can generate a series of random numbers in JavaME/J2ME application by using the included java.util.Random class.


Here's a sample usage

JAVA:
  1. import java.util.Random;
  2. ...
  3. Random generator = new Random();
  4. generator.setSeed(System.currentTimeMillis());
  5. float f = generator.nextFloat();
  6. System.out.println(""+(f*100.0f)%100);
  7. ...

The sample code above will produce a random floating-point number between 0..100 (exclusive)

Here's the source code for the full Random Number Demo application : RandomDemo.java,

Presentation Slides for 10 Sept UUM Sintok Class Talk (AP Hatim)

Here is the presentation slides of the 10 Sept 2007 Network Content & Applications in Mobile Devices - Assoc Prof. Hatim Mohamad Tahir class at UUM Sintok.

[tags]sintok,malaysia,uum,presentation,slides,mobile,j2me,javame,webservice,web service,xmlrpc,xml-rpc,soap[/tags]


The best J2ME MIDlet icon size settings

Although not required, It is nice to include icon in your MIDlet distribution as it give a unique feeling to your mobile application. The only problem is different device seems to have different icon sizes requirements for tJ2ME MIDlet.

The Ideal Icon size guidelines for different mobile device
Here is the icon size guideline for different devices

Nokia

  • Series 40 - 128x160=24x24, 208x208=46x46, 240x320=46x48
  • S60 1st and 2nd - 176x208=42x29, 352x416=76x76
  • S60 3rd - 176x208=31x31, 208x176=37x37, 352x416=76x76, 240x320=53x53/55x55, 320x240=52x52/54x54, 208x208=37x37
  • N90/N95 : 84x58

Samsung & Sony Ericsson
Most Samsung phone accept 16x16, 32x32 icon size

Motorola
15x15, 16x16, 32x32

Most phone seems to accept 8bit-depth color for MIDlet icons, except Nokia S60 3rd edition which seems display 24bit icon quite well.

Note that most of this figure are based on experiments and observation on different phone models.


Create Chart Easily on with J2me ChartComponent class

Charts and graphs are very convenient and efficient way to present data to audiences. Charts make it easy for people to compare figures and present an interpretation of the data itself with just a glance.

However writing a class to represent data in charts in J2ME environment is very time consuming if not difficult, especially when the data representation part consists of a very small part of your mobile application.

Fortunately, there's ChartComponent, an easy to use class for generating charts in J2ME JavaME applications. ChartComponent requires MIDP 2.0 / CLDC 1.0 supported phone in order to be use.

Supported charts in the class are :

  1. Horizontal bars
  2. Vertical bars
  3. Line
  4. Pie (experimental)

ChartComponents comes with javadoc API documentation for references and a test suite which serves as a demo and code reference. The only drawback that I found with are : It is not open source, it is quite large.

However the ChartComponent jar class size can be reduced after integrating with your own midlet with the use of obfuscater.

Download ChartComponent from Beanizer.org


Generic AboutForm class for J2ME / JavaME application

I've seen a lot of J2ME / JavaME application authors create their own 'About' or 'Help' form to provide information regarding their MIDlet application. Most of this 'About' page are re-invented each time the author releases a new application generating inconsistencies between application.

Moreover there are also another breed of authors that take a far more lazy approach by abusing the 'Alert' class to include 'About' information. On most phones, This will severely limits the amount of text displayed on the phone screen regarding the application information.

The Solution : A generic AboutForm
In light of this situation, I decided to create a generic AboutForm (derives from the familiar 'Form' class) which provides a consistent look of a proper "About" page on your application. All you need to do is to initialize the class and use it just like ordinary "Form" class.


AboutForm class features

  • Consistent look on your applications
  • Reusability - You don't need to rewrite the about form class
  • Pre-arrange layout, the application/company logo, name, copyright notice and hyperlink are nicely arranged on the form
  • Easy to use and inuitive
  • You can stop wasting your time creating an About form for each of your applications
  • Clean and straight forward - just initiate the class and let it do all the work for you

Here's a snippets of example usage :

JAVA:
  1. AboutForm aboutForm;
  2. ..
  3. ..
  4. //initialize about form, place logo.png in res folder inside the MIDlet apps folder.
  5. aboutForm = new AboutForm("About","AboutFormDemo 1.0","/logo.png");
  6. //set copyright notice
  7. aboutForm.setCopyright("mypapit","2007");
  8. //set hyperlink
  9. aboutForm.setHyperlink("http://mobilepit.com",this);
  10. //set commandlistener
  11. aboutForm.setCommandListener(this);
  12. //add additional notice
  13. aboutForm.append("This is a demonstration of generic AboutForm class.");
  14. ...
  15. ...
  16. public void commandAction(Command c, Displayable d) {
  17. if (c == exitCommand) {
  18. destroyApp(false);
  19. notifyDestroyed();
  20. } else if (c == aboutCommand) {
  21. //display the about form
  22. display.setCurrent(aboutForm);
  23. } else if (c == aboutForm.DISMISS_COMMAND) {
  24. //this will handle "Back"
  25. //notice that aboutForm uses DISMISS_COMMAND as its Command object similar to Alert class in MIDP 2.0
  26. display.setCurrent(mainForm);
  27. }
  28. }

Download AboutForm class (MIDP 2.0)
Here are the link to the downloads :

[tags]j2me,javame,midp,cldc,mobile,java,coding,programming,application,midlet,midlets[/tags]


How to Round Floating point number (Double) to 2 Decimal Points in J2ME JavaME

The minimalistic design of JavaME CLDC means that a lot of packages from the standard Java has to be trimmed out and this includes the flexible NumberFormat packages which is useful for formatting numbers in Java.

The absence of NumberFormat and java.text package means that you can't even round a floating point number!

However there is a workaround to this problem, with this simple method, you can round floating point number (Double) roughly to a specific number of decimal points.

JAVA:
  1. //round to 2 decimal points
  2. double number = (double)(int)((bmi+0.005)*100.0)/100.0;
  3. //round to 4 decimal points
  4. double number = (double)(int)((bmi+0.00005)*10000.0)/10000.0;

The code above shows you how to round a double to 2 and 4 decimal points, you can change the number of zeros above to round it to your desired decimal points.

Hope that will help you in your JavaME / J2ME programming.

[tags]j2me,javame,midp,java,sdk,j2se[/tags]


How to send SMS using WMA JSR-120 in JavaME J2ME

Here a basic way on how to send SMS programatically from within JavaME / J2ME application using Wireless Messaging API (JSR-120).

WMA JSR-20 Code Snippets

JAVA:
  1. import javax.wireless.messaging.*;
  2. import javax.microedition.io.*;
  3. ...
  4. try {
  5. String addr = "sms://" + this.phone;
  6. MessageConnection conn = (MessageConnection) Connector.open(addr);
  7. TextMessage msg =
  8. (TextMessage)conn.newMessage(MessageConnection.TEXT_MESSAGE);
  9. msg.setPayloadText("SMS Demo to " + text );
  10. conn.send(msg);
  11. } catch (IllegalArgumentException iae) {
  12. //do something
  13. } catch (Exception e) {
  14. //do something
  15. }

As with other networking code, you need to execute this in a separate thread from the main MIDlet application (i.e commandAction), to avoid potential deadlocks.

Download complete working MIDlet application that demonstrates Wireless Messaging API code : SMSDemo.zip




Post a Comment

Template by : kendhin x-template.blogspot.com by :