十一月's profileNOVEMBREBlogListsGuestbookMore Tools Help

Blog


    January 30

    使用platformRequest()自动更新MIDlet套件

     

    MIDP 2.0提供了一系列的新特性,其中一个就是调用平台的应用。例如可以调用WAP浏览器访问特定的网址,也可以调用电话应用程序呼叫某个号码。上述两个功能都可以使用MIDlet类的platformRequest()方法实现,platformRequest()接受一个String类型的参数url,如果是呼叫电话号码则url的形式为tel:13810011001。如果是调用WAP浏览器则url的形式类似于http://www.j2medev.com/wap.wml

    本文介绍如何使用platformRequest()方法自动更新MIDlet套件,在MIDP的文档中说明。如果url的形式为一个指定的MIDlet套件,可以是JAD文件,也可以是jar文件。例如http://www.j2medev.com/helloworld.jad。这个时候,此请求会被认为是安装MIDlet套件来对待,这样用户可以控制安装的过程,就像我们直接从WAP浏览器输入地址安装MIDlet套件一样。如果请求的MIDlet套件是当前正在运行的应用程序的升级版,则当前的程序需要先退出,然后执行更新操作。

    如果想让发布的MIDlet套件具备自动升级的功能,那么需要提供一个服务器端程序,服务器端程序能够检测是不是有更新版本的应用程序下载,并且可以将这个结果告诉给客户端。通常这样的服务器端可以实现为Web应用程序,用servlet和MIDlet通信。当用户运行MIDlet的时候,MIDlet首先连接指定的服务器获取信息,如果没有新版软件则正常运行,如果有新版本的软件则是用platformRequest()方法请求安装应用程序。

                         if (update == NEED_UPDATE) {

                                try {

                                       platformRequest("http://www.j2medev.com/wap/autoupdate.jar");

                                       destroyApp(true);

                                       notifyDestroyed();

                                } catch (ConnectionNotFoundException ex) {

                                       ex.printStackTrace();

                                }

                               

                         }else{

                                //do something else

                         }

    这里我们在代码中执行destroyApp(true),让MIDlet套件主动退出。如果想做的更为专业,可以在检测到有新版本的软件的时候,显示一个对话框让用户选择是否自动更新,根据用户的选择执行相关的操作。下面的代码UpdateMIDlet可以用于演示此项功能。

    /**

     * author mingJava

     * Created on 2006-3-8

     */

    package com.j2medev.autoupdate;

     

    import java.util.Random;

    import javax.microedition.io.ConnectionNotFoundException;

    import javax.microedition.lcdui.Display;

    import javax.microedition.lcdui.Form;

    import javax.microedition.midlet.MIDlet;

    import javax.microedition.midlet.MIDletStateChangeException;

     

    public class UpdateMIDlet extends MIDlet {

     

           public static int NEED_UPDATE = 0;

     

           public static int NO_UPDATE = 1;

     

           private Display display = null;

     

           protected void startApp() throws MIDletStateChangeException {

                  if (display == null) {

                         display = Display.getDisplay(this);

                         int update = (new Random().nextInt() >>> 1) % 2;

                         if (update == NEED_UPDATE) {

                                try {

                                       platformRequest("http://www.j2medev.com/wap/autoupdate.jar");

                                       destroyApp(true);

                                       notifyDestroyed();

                                } catch (ConnectionNotFoundException ex) {

                                       ex.printStackTrace();

                                }

                               

                         }else if(update == NO_UPDATE){

                                Form form = new Form("Test");

                                form.append("No update is needed");

                                display.setCurrent(form);

                         }

                  }

           }

     

           protected void pauseApp() {

     

           }

     

           protected void destroyApp(boolean arg0) throws MIDletStateChangeException {

     

           }

     

    }

           为了模拟是否有升级软件的可能,我们在startApp()中随机生成一个随机数update。如果update等于0则代表有升级版本,如果update等于1则代表不需要升级。为了简单起见,这里我们没有编写联网检测升级版本的程序。此程序在Nokia 7610上测试通过,可以自动升级MIDlet套件。

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://novembre-winter.spaces.live.com/blog/cns!B4EC2BD80D2AD637!138.trak
    Weblogs that reference this entry
    • None