我的问题很简单,如何识别应用程序的首次启动?我认为可以通过在 RMS 中保存一些值并在应用程序启动时读取它并决定做什么来完成。
但是没有更简单的解决方案吗?
据我所知,没有更简单的方法,但无论如何这很容易;
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))
}