Store data

Reference

http://developer.android.com/guide/topics/data/data-storage.html

Shared preferences

Write

SharedPreferences settings = getPreferences(0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("isInitialized", true);
editor.apply(); // or editor.commit();

Read

SharedPreferences settings = getPreferences(0);
boolean isInitialized = settings.getBoolean("isInitialized", false);

Database

Contract

public static abstract class ActionLog implements BaseColumns {
    public static final String TABLE_NAME = "ActionLog";
    public static final String COL_DATE = "ActionDate";
    public static final String COL_ACTION = "Action";
}

Create

public class DbManager extends SQLiteOpenHelper {
    private static final String DB_NAME = "CheckSKYP.db";
    public static final int DATABASE_VERSION = 1;

    public DbManager(Context context) {
        super(context, DB_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        sql = "create table " + DbContract.ActionLog.TABLE_NAME +
                " (" + DbContract.ActionLog._ID +
                " integer primary key autoincrement, " +
                DbContract.ActionLog.COL_DATE + " text, " +
                DbContract.ActionLog.COL_ACTION + " text )";
        db.execSQL( sql );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //db.execSQL("drop table " + DbContract.CallLog.TABLE_NAME);
        onCreate(db);
    }
}

Write

public static void WriteAction(Context context, String connection, String action) {
    DbManager dbManager = new DbManager(context);
    SQLiteDatabase db = dbManager.getWritableDatabase();
    ContentValues values = new ContentValues();
    String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    values.put(DbContract.ActionLog.COL_DATE, date);
    values.put(DbContract.ActionLog.COL_ACTION, action);
    long newRowId = db.insert(DbContract.ActionLog.TABLE_NAME, null, values);
    db.close();
}

Read

public static void SendDatabaseActionsToService(Context context) {
    DbManager dbManager = new DbManager(context);
    SQLiteDatabase db = dbManager.getReadableDatabase();
    Cursor c = db.rawQuery("select * from " + DbContract.ActionLog.TABLE_NAME + " order by " + DbContract.ActionLog.COL_DATE, null);
    c.moveToFirst();
    while(!c.isAfterLast()) {
        String actionDate = c.getString(c.getColumnIndex(DbContract.ActionLog.COL_DATE));
        String description = c.getString(c.getColumnIndex(DbContract.ActionLog.COL_ACTION));
        // Do something
        c.moveToNext();
    }
    c.close();
    db.close();
}

Delete

public static  void DeleteAction(Context context, int actionId) {
    DbManager dbManager = new DbManager(context);
    SQLiteDatabase db = dbManager.getWritableDatabase();
    db.delete(DbContract.ActionLog.TABLE_NAME, DbContract.ActionLog._ID + "=" + actionId, null);
    db.close();
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License