Saturday 9 March 2013

creating j2me apps in mobile / learning java in mobile


creating j2me apps in mobile / learning java in mobile


signed jad file as provided by maulikkakadiya bro is uploaded in post #2

This thread contains 4 tutorials.

1) An introduction of j2me that will be required to make our first application in mobile.

2) Configuring mobile sdk and creating a new blank project.

3) Coding in mobile using mobile sdk.

4) An example to show how to  convert a hello world program made in j2se ( java for desktop machines) to j2me ( java for mobile phones).

The first three are longer than the maximum number of characters supported , So i have to make pdf for them.
The last tutorial is there in this thread.

PART IV :
Many people cant start learning java because they dont have computer , and the examples that are given in java tutorials are made for computers. But you can try most of these examples in mobile also, just a few conversions needed. In desktop java , there is already an interface made to take inputs and display output ( called console), But in j2me , there is no such interface. So first of all , we have to make an interface like application that can display our output of code. In the sample j2me application in third tutorial , I have made such an application. It contains of an editor like screen to display the output , and a button exit , to exit the application. The code to create this interface is :

public class MyMIDlet extends MIDlet implements CommandListener

{
  TextBox mybox=new TextBox("sample box"," ",100,TextField.ANY);
  Command mycommand=new Command("exit",Command.EXIT,0);
  public void startApp()
{
   mybox.addCommand(exit);
   Display.getDisplay(this).setCurrent(mybox);
   mybox.addCommandListener(this);
}
  public void commandAction(Command c, Displayable next)
  {
    if(c==exit)
     {
       notifyDestriyed();
      }

   }
public void pauseApp()
{
}
public void destroyApp()
{
}
}

Dont pay attention to the code here. I had explained about it in detail. Just note that this code will make an interface like this :

The textBox will be used as an output displaying screen.

Now if you have seen a simple hello word program in java , its code will look like this :

public class MyClass

{
  public static void main(String[] args)
  {
      System.out.println("Hello world");
   }

}

Now check what these things are , and compare it with the code we have written above :

1) MyClass in the name of class in desktop java -> MyMIDlet is the name of our class in j2me

2) The execution of program starts from the method main(String[] args) in desktop java ->
    The execution of program starts from startApp() in j2me.

3) To display "Hello world" in console (which work as output screen) in desktop java we write :

System.out.println("Hello world");

    To display "Hello world" in textbox (which we are using as output screen) in j2me , we write :

    mybox.setString("Hello world");

So simply saying ,

  startApp() works as main(String[] args) in j2me

mybox.setString( ) works as System.out.println( ) in j2me
5.88 KB, Downloads: 130
Have 3 people ratedMoneyPackReason
 gazcony85+ 5Usefull
 azirenniwre+ 5Usefull
 Mamun7+ 20Usefull
Total score: Money + 30   View Rating Log
*****************
JAI HIND

My Blog
Phones : Samsung galaxy Y pro duos (with root access) ; Nokia C5-00 s60v3 (H@cked)
My Facebook id
Even A Devil May Cry Sometimes
*****************
java powered !
Rank: 7Rank: 7Rank: 7
UID
21821
Posts
1973
Karma
36
Credits
1481
Money
4611
Online time
1483 Hours
Reg time
28-6-10
2#
 Post time: 31-7-12 20:41:25
|Show the author posts only
Edited by badp90 at 31-7-2012 20:53

So , to make a hello world program in j2me , you have to first write code for the interface everytime , so I suggest just save it and reuse it each time. After that , if you find some code inside main() in j2se , then code it inside startApp() in j2me . To display output , use mybox.setString(output here) .
note that mybox is the name of box that we are using, thats why we are writing mybox.setString(). In case u have used another name for the box , then u have to use that name with setString().
Now the complete code of hello world j2me application will look like this :

public class MyMIDlet extends MIDlet implements CommandListener

{
  TextBox mybox=new TextBox("sample box"," ",100,TextField.ANY);
  Command mycommand=new Command("exit",Command.EXIT,0);
  public void startApp()
{
   mybox.setString("Hello World");
   mybox.addCommand(exit);
   Display.getDisplay(this).setCurrent(mybox);
   mybox.addCommandListener(this);
}
  public void commandAction(Command c, Displayable next)
  {
    if(c==exit)
     {
       notifyDestriyed();
      }

   }
public void pauseApp()
{
}
public void destroyApp()
{
}
}
Most of the code is just to create the Display ,we have just added mybox.setString("Hello World");

Using this conversion technique , you can easily learn and try basic concepts of java in mobile phone itself. You just need mobile sdk , which is also uploaded below. Just search for online tutorials of java from google
618.6 KB, Downloads: 509
368.29 KB, Downloads: 199
185.73 KB, Downloads: 161
368 KB, Downloads: 165

No comments:

Post a Comment