日期:2014-05-20  浏览次数:20559 次

手把手教您使用三星SDK完成一个新的MIDlet应用
本文将教你如果使用三星SDK创建应用程序,同时将以代码呈现的形式编译、打包、运行等,在最后将会简单介绍一下混淆器(obfuscator)和调试器(Debugger),这两个技术的详细描述将在后续转载中向开发者呈现。同时后面的连载文章也会简单教会您如果将自己的开发结果部署到web网站上面,供其下载。
下面,让我们开始用三星SDK创建我们的第一个项目吧。放弃经典的Hello world吧!

创建简单项目
  依据之前的连载,打开KToolbar工具,然后点击文件菜单,如下图所示。我们可以新建一个项目,可以从Jar/JAD导入一个文件,也可以打开一个已经存在的项目。
1.打开主菜单



2.创建一个新项目。点击新建项目



在项目名字字段输入项目名称。如:TestCreateProject,然后包名字字段为空。MIDlet类名字段输入类名TestCreateProject,然后产生项目。如下图所示,这一部大家可以不选择,以后可以随时更改。下图的一些参数,是对于这个程序的一些JSR及其工程的一些属性设置。



控制台上也会出现一些创建项目的信息,这个看起来有点类似Eclipse的控制台打印的感觉。



到此为止,项目已经创建完成,下面就对项目进行编写代码,及相应的属性设置了。

项目开发
  在上面创建项目完成的基础上,如果完成整个项目的开发,在需要三个步骤,分别如下:
1.编写代码
  可以根据您的应用,编写代码文件、代码需要的资源文件等。
2.编译
  使用三星SDK编译和验证您的代码。
3.运行
  通过模拟器进行运行代码,运行方法,参考前面的已经发表的文章。
如果在编译和运行过程中,出现一些错误,那么在KToolbar的控制台会出现一些打印信息,根据打印的错误信息,你可以纠正你的代码和一些错误的配置等。下面对整个过程进行详细介绍

一 创建代码
  你可以使用文本编辑器或者其他IDE工具来创建您的源代码文件并且进行编写。依据上面步骤创建的MIDlet类,已经有了代码框架,您可以在这个基础上进行编写您的代码,省去了很多框架代码编写的工作量。TestCreateProject.java代码如下所示:
/*
* TestCreateProject.java
*/


import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 * The application must extend this class to allow the application
 * management software to control the MIDlet and to be able to retrieve
 * properties from the application descriptor
 */

public class TestCreateProject extends MIDlet implements CommandListener {

  private Command exitCmd = new Command("Exit", Command.EXIT, 1);
  private Display display;
  private Form mainForm;

  /**
  * Constructor for the MIDlet,
  * initializations of the form
  */

  public TestCreateProject(){
  }

  /**
  * Signals the MIDlet to terminate and enter the Destroyed state.
  * In the destroyed state the MIDlet must release all resources and
  * save any persistent state
  * @param unconditional If true when this method is called, the
  * MIDlet must cleanup and release all resources. If false the
  * MIDlet may throw MIDletStateChangeException to indicate
  * it does not want to be destroyed at this time.
  *
  */

  protected void destroyApp(boolean unconditional){

  }

  /**
  * Signals the MIDlet to enter the Paused state.
  * In the Paused state the MIDlet must release shared resources.
  */

  protected void pauseApp(){

  }

  /**
  * Signals the MIDlet that it has entered the Active state
  * The method will only be called when the MIDlet is in the Paused state
  * Performs some initialization, places app in active state
  *@exception MIDletStateChangeException is thrown if the MIDlet cannot
  * start now but might be able to start at a later time.
  */

  protected void startApp()throws MIDletStateChangeException{

  display = Display.getDisplay(this);
  mainForm = new Form("TestCreateProject");
  mainForm.addCommand(exitCmd);
  mainForm.setCommandListener(this);
  display.setCurrent(mainForm);

  }

  public void commandAction(Command c, Displayable d) {
  if (c == exitCmd) {
  destroyApp(false);
  notifyDestroyed();  
  }  

  }  

}
  如果想打开编辑这个源代码文件,可以右键单击要修改的文件,如下图所示: