Search code examples
javajava-memidp

J2me detecting first start of application


My question is simple how can I recognize first start of application? I think it can be accomplished by saving some value in RMS and reading it when app starts and decide what to do.

But isn't there simpler solution?


Solution

  • There is'nt a simpler method as far as I know, but that's very easy anyway;

        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))
        }