J2me检测首次启动应用程序

时间:2012-05-07 10:22:27

标签: java java-me midp

我的问题很简单,如何识别首次启动申请?我认为可以通过在RMS中保存一些值并在应用程序启动时读取它并决定要做什么来实现。

但是有没有更简单的解决方案?

1 个答案:

答案 0 :(得分:3)

据我所知,没有一种更简单的方法,但无论如何这都很简单;

    public static boolean isFirstRun() {
        RecordStore rs = null;
        try {
            rs = RecordStore.openRecordStore("myAwesomeRecordStore", false); //check
            return false; //record store exists, so it's not our first run
        } catch (Exception e) {
            //RecordStoreNotFoundException, but we need to catch others anway
            try {
                rs = RecordStore.openRecordStore("myAwesomeRecordStore", true); //create
            } catch (Exception e1) {}
        }
        finally {
            if (rs != null) try {rs.closeRecordStore();} catch (Exception e) {}
        }
        return true; //so, record store did not exist and it was created (if no error occured (there shouldn't be any errors anyway))
    }