12 Replies - 20491 Views - Last Post: 31 March 2010 - 03:07 PM

#1 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1876
  • View blog
  • Posts: 20,284
  • Joined: 17-March 01

Week #12 Challenge: Java ME

Post icon  Posted 22 March 2010 - 01:52 PM

Week #12: JavaME


Posted Image

Challenge submitted by NickDMax

CHALLENGE:
We'll keep up the mobile theme this month with JavaME. Write a basic App for a Cell Phone or Mobile device using JavaME

You don't need an JavaME device to try this challenge. The SDK comes with an emulator!

JavaME is one of the most widely used Mobile development platforms. Developing JavaME Applications is not very complicated and its one app that you can take with you and show all your friends (like pictures of the kids). Since JavaME development kits come with phone emulators you don't even have to own a mobile device to participate.

IDEAS:
  • Note Taker
  • Basic Game
  • Private Photo Album
  • Bluetooth File transfer
  • Bluetooth P2P chat
  • Bluetooth Head2Head game
  • Text message client
  • Twitter client
  • DIC status updater?
  • Mass Text app



HOW TO GET STARTED:
  • : Download and install the Java 6 JDK (note: if you already develop in java you should already have this installed and just need to locate the JDK install directory).
  • : Download and install the JavaME SDK 3.0 (note: if you do not wish to sign up for a Sun One account you can skip that part but the link is very very tiny :) )
  • : (optional) download the SDK for your particular phone -- if you would like an emulator for your particular phone you can see if the manufacture has an JavaME SDK available -- I know Sony Ericsson does. Having the platform specific SDK may enable features specific to that manufacturer or phone. A list of links to various manufactures can be found here under Java ME Development Kits.
  • : (optional) Get a better IDE: If you would prefer you can also download a more robust IDE such as NetBeans Mobility or Eclipse Pulsar.


The SDK comes with a basic IDE that you can use, it has a number of sample applications and emulators available. If you wish you can start right here in the SDK editor. Simply start the Java ME Platform SDK and choose an example project to open and explore.

If you would like to try using another IDE I can recommend Eclipse Pulsar (link above) which I have been using. I will update this post with a short description of how to get a basic HelloWorld application up and running. Meanwhile I highly suggest playing with the SDK demo applications.

EXAMPLE CODE:
package com.nickdmax.firstme;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.*;

/**
 * A basic Java ME MIDP MIDlet. Displays a form with the message "Hello Phone".
 * MIDlets must implement three methods: startApp(), pauseApp(), destroyApp().<br>
 * startApp() -- called to initialize or resume the application. <br>
 * pauseApp() -- called to pause the application. <br>
 * destroyApp() -- called when exiting - clean up application resources. <br>
 * <br>To make the MIDlet have functionality it helps to respond to commands. To respond
 * to commands you can implement the CommandListener interface.
 * @author NickDMax @ DreamInCode
 */
public class HelloME extends MIDlet implements CommandListener {
	private Command exitCmd; //The exit command...
	private Display display; //The phone's display
	private Form mainForm; //the main form for the application
	
	/**
	 * Object Constructor -- initializes the Form and the Commands. You could place this
	 * stuff in the startApp() method but it only need to happen once to setup the form, so
	 * there really is no need to do it each time the startApp() method gets called.
	 */
	public HelloME() {
		display = Display.getDisplay(this);
		mainForm = new Form("Hello Phone!");
		exitCmd = new Command("Exit", Command.EXIT, 1);
		mainForm.addCommand(exitCmd);
		mainForm.setCommandListener(this);
	}
	
	/**
	 * This is called when the App starts or is resumed after a pause (say a phone call),
	 * so we need to act according to state -- this simple app only has one state (display form).
	 */
	protected void startApp() throws MIDletStateChangeException {
		display.setCurrent(mainForm);
	}
	
	/**
	 * Called to cleanup the app. Not currently used.
	 */
	protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
		//No cleanup needed
	}

	/**
	 * Called when the phone needs to interrupt the app. You should save the state of the application
	 * and restore it in the startApp() method. 
	 */
	protected void pauseApp() {
		//Nothing to pause		
	}

	/**
	 * Responding to commands. In this case there is only 1 command - exit.
	 */
	public void commandAction(Command c, Displayable d) {
		if (c == exitCmd) {
			try {
		      destroyApp(true);
			} catch (Exception ignore) { }
		      notifyDestroyed();
		}
	}	
}


WHERE TO GO FOR HELP:
If you're struggling, you can get help from Dream.In.Code's Mobile Development forum.

I find the Sony Ericsson Developer World to be an excellent resource (but I have a SE phone).

Is This A Good Question/Topic? 0
  • +

Replies To: Week #12 Challenge: Java ME

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Week #12 Challenge: Java ME

Posted 22 March 2010 - 02:26 PM

Getting started with Eclipse Pulsar.

I was going to write a bit of a tutorial (and I guess I will) but I found this tutorial here which seems to do a better job than I would be able to (nice pretty pictures). Part 2 goes over how to import the emulators and start a project.
Was This Post Helpful? 0
  • +
  • -

#3 Jono20201  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 179
  • Joined: 07-July 09

Re: Week #12 Challenge: Java ME

Posted 22 March 2010 - 03:05 PM

Ironicly I have just got a new Nokia 5230. I was thinking of trying this. Thanks for giving me the links.
Was This Post Helpful? 0
  • +
  • -

#4 aroymart  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 22-February 09

Re: Week #12 Challenge: Java ME

Posted 22 March 2010 - 03:37 PM

ah thank you! I've been trying to learn Java but couldn't even find the right development kit.
I'll be sure to post my program when I'm done :D
Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Week #12 Challenge: Java ME

Posted 22 March 2010 - 03:48 PM

Getting Started With Eclipse Pulsar

Step 1: Download and install a Java JDK (keep track of install location)
Step 2: Download and install Java platform SDK's for whatever platforms you need (Keep track of install locations)(I did JavaME SDK, Sony Ericcson SDK, LG SDK)
Step 3: Download Eclipse Pulsar
Step 4: Create a directory where you would like to install Eclipse Pulsar (I did C:\Program Files(x86)\Eclipse Pulsar\ )
Step 4a: Extract the contents of the "exlipse" folder from the download archive into the created directory.
Step 4b: (windows optional) Create a start menu icon: copy the exlipse.exe and "Paste as shortcut" into the start menu (I am really glossing over how to do this if you need help try asking the the OS/Computer Support forum).
Step 5: Start the Eclipse App (may need to tell it where you have Java)
Step 5a: Choose a location for your workbench (I choose C:\Dev\Pulsar\ )
Step 5b: On the start page choose "go to workbench" (when Eclipse first starts it has a cover page, choose to go strait to the workbench, if you would like to return to the cover page just ask me and I will tell you how).
Step 6: Import SDKs:
...a: Go to the "Window" menu: Window > Prferences
...b: JavaME > Device Management
...c: Click the "import" button on the right
...d: Browse to the location of The JavaME SDK (for me: C:\Java_ME_platform_SDK_3.0\ )
...e: Click Ok. Then Select All then finish
...f: Repeat c-e for all SDKs you have downloaded.
Step 7: File > New > MIDLet Project
Step 7a: Give the project a name (HelloJavaME)
Step 7b: in the "Configuration" block, Click on the "Add" button
Step 7b.i: Give the configuartion a name (I choose: "SEW300")
Step 7b.ii: Choose an SDK (For me Sony Ericcson)
Step 7b.iii: Choose a Device (for me the SonyEriccson_W300)
Step 7b.iiii: Finish
(note if you would like to add a few other configurations it will make it easier for testing how your app will look-feel on other platforms)
Step 8: Fill Out the MIDLet Project Properties: Note you must have something in these field for the app to deploy to a phone (at least I needed to for my phone).
Step 8a: choose default for all other options
Step 9: Choose "Finish"
Eclipse may ask you to switch perspectives and just choose OK

Writing the code:

Right click the "src" directory in the left hand navigation panel. Choose "New > Package" and pick a package name (for me: "dic.nickdmax" )

Right click on the new package and choose "New > Java ME MIDlet" and give it a name "HelloJavaMe" then "Finish"

Basic Code example:
package dic.nickdmax;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.*;



/**
 * A basic Java ME MIDlet. Displays a form with the message "Hello Phone".
 * MIDlets must implement three methods: startApp(), pauseApp(), destroyApp().<br>
 * startApp() -- called to initialize or resume the application. <br>
 * pauseApp() -- called to pause the application. <br>
 * destroyApp() -- called when exiting - clean up application resources. <br>
 * <br>To make the MIDlet have functionality it helps to respond to commands. To respond
 * to commands you can implement the CommandListener interface.
 * @author NickDMax @ DreamInCode
 */
public class HelloJavaME extends MIDlet implements CommandListener {
	private Command exitCmd; //The exit command...
	private Display display; //The phone's display
	private Form mainForm; //the main form for the application
	
	/**
	 * Object Constructor -- initializes the Form and the Commands. You could place this
	 * stuff in the startApp() method but it only need to happen once to setup the form, so
	 * there really is no need to do it each time the startApp() method gets called.
	 */
	public HelloJavaME() {
		display = Display.getDisplay(this);
		mainForm = new Form("Hello Phone!");
		exitCmd = new Command("Exit", Command.EXIT, 1);
		mainForm.addCommand(exitCmd);
		mainForm.setCommandListener(this);
	}

	
	/**
	 * This is called when the App starts or is resumed after a pause (say a phone call),
	 * so we need to act according to state -- this simple app only has one state (display form).
	 */
	protected void startApp() throws MIDletStateChangeException {
		display.setCurrent(mainForm);
	}
	
	/**
	 * Called to cleanup the app. Not currently used.
	 */
	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		// TODO clean up any resources allocated
	}

	protected void pauseApp() {
		// TODO save state of application if paused
	}

	/**
	 * Responding to commands. In this case there is only 1 command - exit.
	 */
	public void commandAction(Command c, Displayable d) {
		if (c == exitCmd) {
			try {
		      destroyApp(true);
			} catch (Exception ignore) { }
		      notifyDestroyed();
		}
	}
	
}


The above is a basic code example (taken from my snippet here

Once you save this you should be able to choose to run the program. Note I had some problems getting the Sony Ericcson Service started under windows 7 so I had some additional steps:

I had to select "Application Descriptor" from the left hand navigation, then under "Runtime" choose "Add" and then add "ClamshellCldcPhone1" which is the basic model for my phone.
Attached Image
Was This Post Helpful? 2
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Week #12 Challenge: Java ME

Posted 22 March 2010 - 04:38 PM

humph! Under windows XP I was able to run the W300 emulator but for some reason I can't run it under windows 7. I am able to run the LG phones and the Default phones.

That is irritating, it will run when connected to the actual phone, but not as an emulation. I must have missed something somewhere...
Was This Post Helpful? 0
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Week #12 Challenge: Java ME

Posted 22 March 2010 - 05:42 PM

I was interested in doing a bluetooth P2P chat... so I am beginning with this description from the API: Part II: The Java APIs for Bluetooth Wireless Technology. I know what your are thinking... if you are close enough to use bluetooth, why not just talk? Well... I don't have a good answer to that. Another point of view is: Why not just text... well I don't know that either... but I wanted to test out bluetooth and I figured this was an easy example.

There are also some WEB APIs.Web is really expensive on my phone so I didn't really think about it... but maybe someone could do a twitter updater.

What would be neat is a DIC status updater! eh? there's an idea.
Was This Post Helpful? 0
  • +
  • -

#8 Munawwar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 162
  • View blog
  • Posts: 457
  • Joined: 20-January 10

Re: Week #12 Challenge: Java ME

Posted 23 March 2010 - 07:59 AM

I used the JavaME wireless toolkit 2.5.2 (that now comes with the SDK) for sending text messages.

(Took this code from my old app.It should work for older MIDP 1.0 phones too.It's slightly different from what I found here)
import javax.wireless.messaging.*;
...
...
public void SendMessage(String mobileNumber)
{
   try {
     String MsgToSend="Hello world";  
     MessageConnection clientConn = (MessageConnection) Connector.open("sms://" + mobileNumber + ":5000"); //5000 is a port number.You can put anything (I think a max of 65535)

     TextMessage tmsg = (TextMessage) clientConn.newMessage(MessageConnection.TEXT_MESSAGE);
     tmsg.setAddress("sms://" + mobileNumber + ":5000");
     tmsg.setPayloadText(MsgToSend);
     clientConn.send(tmsg);
   } catch (IOException ioExc) {
     System.out.println("Client connection could not be obtained");
     ioExc.printStackTrace();
   } catch (Exception exc) {
     exc.printStackTrace();
   }
}
...



To receive an SMS, you will have to send the SMS through a port number and the "client" should be listening for text messages on that port. (I have the code for it but I cannot understand my own code.It's been 2 years since I looked at this).

This post has been edited by Munawwar: 24 March 2010 - 04:01 AM

Was This Post Helpful? 1
  • +
  • -

#9 finaiized  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 29-November 09

Re: Week #12 Challenge: Java ME

Posted 23 March 2010 - 04:24 PM

Weird, I was itching to try Java as an alternative to C# and I guess this is my chance :D
Was This Post Helpful? 0
  • +
  • -

#10 ludjer  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 186
  • Joined: 31-October 08

Re: Week #12 Challenge: Java ME

Posted 25 March 2010 - 04:15 PM

hey guys
anyone has some good pointers on howto connect to the internet with Java ME
Was This Post Helpful? 0
  • +
  • -

#11 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Week #12 Challenge: Java ME

Posted 26 March 2010 - 11:37 AM

All I can offer the is Demo code in the SDK. The NetworkDemo project shows Socket and Datagram messages and you can read more at the javax.microedition.oi documentation
Was This Post Helpful? 0
  • +
  • -

#12 robhilly  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 46
  • Joined: 26-May 08

Re: Week #12 Challenge: Java ME

Posted 31 March 2010 - 12:42 PM

I am very interested in participating in this challenge. Just one question. Do we submit the final result in this forum topic? And if so, do you want just the code output, the resulting midlet, or both?
Was This Post Helpful? 0
  • +
  • -

#13 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Week #12 Challenge: Java ME

Posted 31 March 2010 - 03:07 PM

Its up to you... the code and a screen-capture of the app running in the emulator would be good.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1