详细说明MIDP中的属性问题

来源:互联网 发布:涉外域名纠纷案件板块 编辑:程序博客网 时间:2024/06/07 14:35
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

      本文目的是说明MIDP中的属性问题,主要涉及到jad和MANIFEST.MF文件的一些概念。事实上很多人并不清楚这方面的细节。所以你应该仔细读读这篇文章.

      通常MIDP的应用程序是以.jar文件和.jad文件发布的。jar文件就是我们所说的jar包,jad文件的全名是java application descriptor。在jad文件中包含着Application Management Software用来管理MIDlets的生命周期的信息,当然也包括很多重要的属性。他们是成对出现的例如:MIDlet-Name:myjava。你可以通过调用getAppProperty("MIDlet-Name")来得到String类型的myjava,这是非常方便的,如果你想让你的MIDlet去联网那么你可以定义一个name为SERVER_URL,把它的值定义为http://yourip:port/myservlet,这样如果你想去连接其他地址的话只需要修改jad文件了,不需要重新编译。因此我们要清楚一点在jad文件里面放置一些MIDlet初始化需要的值是比较可取的。

      在jar包里面包括一个目录META-INFO里面有个文件是MANIFEST.MF。它是用来描述JAR文件的,也包含一些其它的信息,你可能发现了,在MANIFEST.MF和JAD文件中的一些值是相同的。那么这就存在着一个规则,AMS如何决定去读取哪个值呢?答案是这样的。

  1. 如果MIDlets的jar包是signed的,那么AMS要去察看它的数字签名,如果是有效的那么它会首先去读取MANIFEST.MF的内容,然后读取jad文件中相同的内容,如果匹配的话那么它会安装这个jar包,如果不匹配的话就会拒绝安装。如果他发现数字签名无效的话直接就会拒绝安装。
  2. 如果MDIlets的jar包是unsigned的话,那么AMS就把它认为是非信任的,他去读取jad文件的属性,如果发现没有的话它去读取MANIFEST的内容,也就是说如果jad和MANIFEST里面都有的属性AMS只会去读取jad中的。

      下面这个逻辑看上去是比较清楚的: given : String key // attribute name return : String value throws : NullPointerException if key is null

 if trusted  String v0 = lookup key in manifest  String v1 = lookup key in descriptor

  if ( v0 != null && v1 != null )   // found in both; must be the same value   assert( v0.compareTo( v1 ) == 0 );

  value = ( v1 != null )? v1 : v0; else // untrusted  value = lookup key in descriptor

  if value == null   value = lookup key in manifest

 return value

下面简单介绍一下MIDP2.0中定义的一些重要的参数,在MIDP2.0中定义了18个值。其中在MANIFEST.MF和jad中都必须定义的有六个,他们是:MIDlet-Name MIDlet-Version MIDlet-Vendor MicroEdition-Profile MicroEdition-Configuration  MIDlet-n 由于一个MIDlet suite里面可以有多个MIDlet的,因此你要说明他们比如MIDlet-1,MIDlet-2.....MIDlet-n,这一点非常的重要,在部署jar和jad文件到手机的时候一定要确保MIDlet-Name的正确,它的值是你的jar包的名字!否则不会安装成功的。还有两个值是jad文件中不许有的他们是MIDlet-Jar-URL ,MIDlet-Jar-Size 其中应该注意的是MIDlet-Jar-URL,如果你要是通过OTA下载的话,那么你不许让他指向你jar包的绝对地址,例如http://myip:port/app/myapp.jar。

      关于一个MIDlet suite里面有多个MIDlet的情况我写了个简单的代码测试了一下,如果还有疑问的话你可以运行一下下面的程序:import javax.microedition.lcdui.Display;import javax.microedition.lcdui.Form;import javax.microedition.midlet.MIDlet;import javax.microedition.midlet.MIDletStateChangeException;/* * Created on 2004-7-14 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */

/** * @author E2412C * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */public class MIDlet1 extends MIDlet{

    private Display display;        /* (non-Javadoc)     * @see javax.microedition.midlet.MIDlet#startApp()     */    protected void startApp() throws MIDletStateChangeException    {        // TODO Auto-generated method stub        display = Display.getDisplay(this);        display.setCurrent(new Form("1"));    }

    /* (non-Javadoc)     * @see javax.microedition.midlet.MIDlet#pauseApp()     */    protected void pauseApp()    {        // TODO Auto-generated method stub

    }

    /* (non-Javadoc)     * @see javax.microedition.midlet.MIDlet#destroyApp(boolean)     */    protected void destroyApp(boolean arg0) throws MIDletStateChangeException    {        // TODO Auto-generated method stub

    }

}import javax.microedition.lcdui.Display;import javax.microedition.lcdui.Form;import javax.microedition.midlet.MIDlet;import javax.microedition.midlet.MIDletStateChangeException;/* * Created on 2004-7-14 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */

/** * @author E2412C * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */public class MIDlet2 extends MIDlet{

    private Display display;        /* (non-Javadoc)     * @see javax.microedition.midlet.MIDlet#startApp()     */    protected void startApp() throws MIDletStateChangeException    {        // TODO Auto-generated method stub        display = Display.getDisplay(this);        display.setCurrent(new Form("2"));    }

    /* (non-Javadoc)     * @see javax.microedition.midlet.MIDlet#pauseApp()     */    protected void pauseApp()    {        // TODO Auto-generated method stub

    }

    /* (non-Javadoc)     * @see javax.microedition.midlet.MIDlet#destroyApp(boolean)     */    protected void destroyApp(boolean arg0) throws MIDletStateChangeException    {        // TODO Auto-generated method stub

    }

}import javax.microedition.lcdui.Display;import javax.microedition.lcdui.Form;import javax.microedition.midlet.MIDlet;import javax.microedition.midlet.MIDletStateChangeException;/* * Created on 2004-7-14 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */

/** * @author E2412C * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */public class MIDlet3 extends MIDlet{

    private Display display;        /* (non-Javadoc)     * @see javax.microedition.midlet.MIDlet#startApp()     */    protected void startApp() throws MIDletStateChangeException    {        // TODO Auto-generated method stub        display = Display.getDisplay(this);        display.setCurrent(new Form("3"));    }

    /* (non-Javadoc)     * @see javax.microedition.midlet.MIDlet#pauseApp()     */    protected void pauseApp()    {        // TODO Auto-generated method stub

    }

    /* (non-Javadoc)     * @see javax.microedition.midlet.MIDlet#destroyApp(boolean)     */    protected void destroyApp(boolean arg0) throws MIDletStateChangeException    {        // TODO Auto-generated method stub

    }

}JAD文件的内容如下:MIDlet-3: MIDlet3,,MIDlet3MIDlet-2: MIDlet1,,MIDlet1MIDlet-1: MIDlet2,,MIDlet2MIDlet-Jar-URL: MIDlets.jarMicroEdition-Configuration: CLDC-1.0MIDlet-Version: 1.0.0MIDlet-Name: MIDletsMIDlet-Vendor: Midlet Suite VendorMicroEdition-Profile: MIDP-1.0把上面的三个.java文件打包成MIDlets.jar文件,然后双击.jad文件。程序就可以运行起来了,我在wtk2.1下运行没有任何问题!  

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击