开发者

Start a specific part of an application only one time after installation

On my app开发者_C百科 I've got a Database which i have to fill one time with some entries. I have packed this step in my splashscreen. Now I got the problem every time i open up my application it puts again all entries to my database. I'm looking for a way to let this happen only on the really first time the app gets opend. But i don't find out, how i can realize this. I'm happy over every input and Idea!

Have a nice day

safari

Notes

I had an idea to realize that. I make a boolean in my SplashScreen, which takes a look at the database table if there is nothin, it inputs all the entries and makes a mark in a table that its filled like yes. Afterwards if i open the app again it takes a look at the database if theres a yes or what ever i definied, it doesn't put again all entries in my database. Sorry for that crappy definition of what my idea is, my english isnt the best.

I'm searching for an easier way than this.


why not have a startup activity that checks if the app is launched for the first time ( and stores the value in the app preferences ) and either forwards to the database setup activity or to the main activity....

Here's a sample on how to read and write to the preferences:

Define preference key:

public static final String KEY_APP_IS_INITIALIZED = "APP_INITIALIZED";

read boolean value from preferences:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context)
boolean appIsInitialized = prefs.getBoolean(KEY_APP_IS_INITIALIZED, false);

store boolean value to preferences:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context)
Editor editor = prefs.edit();
editor.putBoolean(KEY_APP_IS_INITIALIZED, true);
editor.commit();

Launching the activities:

Intent intent = new Intent(startup_activity, 
    db_initialized ? 
        MainActivity.class :
        DbInitActivity.class);
startup_activity.startActivity(intent);


You could use SharedPreferences to store a boolean value which indicates if the database is already filled.

You can store the data using this code:

SharedPreferences pref = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean(key, value);
editor.commit();

and then when you start the application you just read your value using:

SharedPreferences pref = getPreferences(MODE_PRIVATE);
pref.getBoolean(value, false);


// Check if DB is set up on start
SharedPreferences settings = getSharedPreferences("yourappidentifiername",MODE_PRIVATE);
String DbSetUp = settings.getString("dbsetup", "no");
// DbSetUp is "no" or "yes"


// Set that DB is set up
SharedPreferences settings = getSharedPreferences("yourappidentifiername",MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString("dbsetup", "yes");
editor.commit();


Not an optimal solution, but you could check for one of your entries, if they don't exist, add them to your DB, otherwise ignore them.


Try this....

Have one bool value in shared preferences.

Check that variable is set or not on each start of the application.

if not set then set it and perform db operation.

if set don't do db operation just go to next steps.


I've got an idea, reading other answers. Use a version for the database to write in the sharedpreference, so next time you'll need to update the database, just delete the old one and refill the new one with you data.

public void populateResDB(int version){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
            int resDbVersion = prefs.getInt("ResDBVersion", 1);
            if(resDbVersion<version){
                //Populate database here
                //Delete table/db
                //Populate the table/database with new or updated data

                //Update ResDBVersion for the next time
                        Editor editor = prefs.edit();
                        editor.putInt("ResDBVersion", version);
                        editor.commit();
            }else{
                //installed db has the same version as the one we want to install
                //so, do nothing.
            }
}

I'm trying it now, not yet tested. In the onCreate of the splash activity use:

populateResDB(1); //1 is for the first time, increase it manually


You can use method onCreate in your database helper - it's used only, when new database is created, so I think that is what you're looking for.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜