【问题标题】:How to setup android App for the first time?第一次如何设置安卓应用程序?
【发布时间】:2012-05-30 05:03:50
【问题描述】:

如何在我的程序中首次设置我的 SQLite DB?执行此操作的程序结构是什么?

我能想到的唯一方法是保持第一次布尔值:

if (isFirstInstall) setup();

这似乎很不专业。是否有进行此类设置的 onFirstInstall() 调用?

【问题讨论】:

    标签: android sqlite installation


    【解决方案1】:

    试试这个

    private SharedPreferences mPreferences;
    
        boolean firstTime = mPreferences.getBoolean("firstTime", true);
        if (firstTime) { 
            SharedPreferences.Editor editor = mPreferences.edit();
            editor.putBoolean("firstTime", false);
            editor.commit();
            showMiddleActivity();
        }
    

    【讨论】:

      【解决方案2】:

      您可以将布尔值存储到 SharedPreferences 中,并检查值(第一次与否) 当应用程序启动时。

      查看sharedPreferences的文档

      希望thread 对您有所帮助

      【讨论】:

        【解决方案3】:

        您可以尝试使用此代码。

        从 onCreate() 调用 checkFirstLaunch();

        private boolean checkFirstLaunch() {
                try {
                    PackageInfo info = getPackageManager().getPackageInfo(getPackageName(), 0);
                    //int currentVersion = info.versionCode;
                    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
                    int lastVersion = prefs.getInt(PreferencesActivity.KEY_HELP_VERSION_SHOWN, 0);
                    if (lastVersion == 0) {
                        isFirstLaunch = true;
                    } else {
                        isFirstLaunch = false;
                    }
        
                } catch (PackageManager.NameNotFoundException e) {
                    Log.w(TAG, e);
                }
                return false;
            }
        

        你得到布尔结果。

        if (isFirstLaunch) {
            //code    
        }
        

        【讨论】:

          【解决方案4】:

          您可以检查数据库是否存在,如果它丢失,则创建它,而不是弄清楚它是否是第一次启动。

          public void createDataBase() throws IOException {
              if ( checkIfDataBaseExist() ) {
                  // db exists, do nothing
              } else {
                  // By calling this method and empty database will be created into
                  // the default system path of your application
                  this.getReadableDatabase();
                  try {
                      // Do you db copying here
                  } catch (IOException e) {
                      throw new Error("Error copying database");
                  }
              }
          }
          
          private boolean checkIfDataBaseExist() {
              File dbFile = new File(DB_PATH + DB_NAME);
              return dbFile.exists();
          }
          

          【讨论】:

            猜你喜欢
            • 2015-08-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-11-26
            • 1970-01-01
            • 1970-01-01
            • 2011-11-25
            • 2021-07-16
            相关资源
            最近更新 更多