简体   繁体   中英

android content provider - first run Activity Splash screen while filling the database in the content provider from raw data

In my application I have a content provider which uses a database for the content provided. When the database is created the first time it needs to be filled from the raw content of a Json file. My idea was that i trigger this filling of the database at onCreate of my SQLiteOpenHelper subclass. This works fine yet I am not sure how to handle the the communication between application and content provider when the app is running the first time. Basically i would like to show some sort of a splash screen while the database is filled. Yet how does the application get informed that

  1. the content provider is busy filling the database when running the first time
  2. the content provider is ready to go

Surely I could fill the database from the application by calling the content provider with each dataset yet I would prefer doing it within the sphere of the content provider so that the application does not have to handle the reading of the json file etc. Besides design preferences it would also enable the content provider to fill the database more efficiently because it would have the whole dataset at once. I have a feeling this is not possible yet I hope I miss some simple point.

Any suggestions how to achieve this would be highly appreciated.

Thanks

martin

When using a content Provider i would presume that your using a DBHelper class to manage the creation of the database. Below is the code from the android notes example project.

This shows how the DBHelper constructor is intelligent enough to determine if the database has been created before. In the createDatabase method i would subsequently call a method to pre-populate the database, from as you say a json file.

The problem is that this doesn't really allow you to communicate to the Activity that your database hasn't been initialised.

One thought could be that you use SharedPreferences to store the fact you've populated the database. You could then check the sharedPreference in the activity on startup, Call the content provider to populate the database and then store in the shared preference that you've done this task already.

Just be aware that i'm not sure if the sharedPreferences maintain the same state as the database if you for example erase the data from the android settings menu. You'd need to check that.

http://code.google.com/p/android-notes/source/browse/trunk/src/com/bitsetters/android/notes/DBHelper.java?r=10

public class DBHelper {

private static final String DATABASE_NAME = "notes";
private static final String TABLE_DBVERSION = "dbversion";
private static final String TABLE_NOTES = "notes";
private static final int DATABASE_VERSION = 1;
private static String TAG = "DBHelper";
Context myCtx;

private static final String DBVERSION_CREATE = 
    "create table " + TABLE_DBVERSION + " ("
            + "version integer not null);";

private static final String NOTES_CREATE =
    "create table " + TABLE_NOTES + " ("
        + "id integer primary key autoincrement, "
        + "note text, "
        + "lastedit text);";

private static final String NOTES_DROP =
    "drop table " + TABLE_NOTES + ";";

private SQLiteDatabase db;
/**
 * 
 * @param ctx
 */
public DBHelper(Context ctx) {
    myCtx = ctx;
            try {
                    db = myCtx.openOrCreateDatabase(DATABASE_NAME, 0,null);

                    // Check for the existence of the DBVERSION table
                    // If it doesn't exist than create the overall data,
                    // otherwise double check the version
                    Cursor c =
                            db.query("sqlite_master", new String[] { "name" },
                                            "type='table' and name='"+TABLE_DBVERSION+"'", null, null, null, null);
                    int numRows = c.getCount();
                    if (numRows < 1) {
                            CreateDatabase(db);
                    } else {
                            int version=0;
                            Cursor vc = db.query(true, TABLE_DBVERSION, new String[] {"version"},
                                            null, null, null, null, null,null);
                            if(vc.getCount() > 0) {
                                vc.moveToFirst();
                                version=vc.getInt(0);
                            }
                            vc.close();
                            if (version!=DATABASE_VERSION) {
                                    Log.e(TAG,"database version mismatch");
                            }
                    }
                    c.close();


            } catch (SQLException e) {
                    Log.d(TAG,"SQLite exception: " + e.getLocalizedMessage());
            } finally {
                    db.close();
            }
}

private void CreateDatabase(SQLiteDatabase db)
{
            try {
                    db.execSQL(DBVERSION_CREATE);
                    ContentValues args = new ContentValues();
                    args.put("version", DATABASE_VERSION);
                    db.insert(TABLE_DBVERSION, null, args);

                    db.execSQL(NOTES_CREATE);
                    // Populate with data
                    populateDataBaseFromFile();// There are probably better ways to do this.
                    setSharedPreferenceYouPopulatedDB();
            } catch (SQLException e) {
                    Log.d(TAG,"SQLite exception: " + e.getLocalizedMessage());
            } 
}

Personally I wouldn't bother with the splash screen, unless you really needed to.

Another thought might be to:

  1. Write in the db helper a method to determin if your tables exist. Return false if not.
  2. In startup activity call ContentProvider with a request that calls the DBHelper test method.
  3. If false then display splash screen and then call Content Provider to populate DB.
  4. If true, then carry on as normal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM