Search code examples
androidandroid-contentprovider

in-Memory Content Provider


I want to use content provider as an inter-process communication mechanism between different apps in Android.

Assume that Main app acts as a content provider as well. I would like the app to download huge amounts of data and store it in-memory (without writing on disk) and pass on this data to other app using content provider. Other app will get the content URI in the intent and read the data from this content URI.

My question in this regard is that is it possible to store data in memory by main app and let the content provider send that data. Will the content provider life cycle allow this to be done?


Solution

  • This was one of the best examples I've found of an in memory content provider. https://gist.github.com/m039/6550133


    Another easy method is pass null value to SQLiteOpenHelper constructor parameter:name.

    all the other operations(insert/update/delete/query) are the same as write on disk.

        /**
     * Create a helper object to create, open, and/or manage a database.
     * This method always returns very quickly.  The database is not actually
     * created or opened until one of {@link #getWritableDatabase} or
     * {@link #getReadableDatabase} is called.
     *
     * @param context to use to open or create the database
     * @param name of the database file, or null for an in-memory database
     * @param factory to use for creating cursor objects, or null for the default
     * @param version number of the database (starting at 1); if the database is older,
     *     {@link #onUpgrade} will be used to upgrade the database; if the database is
     *     newer, {@link #onDowngrade} will be used to downgrade the database
     */
    public SQLiteOpenHelper(Context context, String name, CursorFactory factory, int version) {
        this(context, name, factory, version, null);
    }