public class Calc extends Activity {
public static final String PREFS_NAME = "MyPrefsFile";
@Override
protected void onCreate(Bundle state){
super.onCreate(state);
// . . .
// Restore preferences
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean silent = settings.getBoolean("silentMode", false);
setSilent(silent);
}
@Override
protected void onStop(){
super.onStop();
// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("silentMode", mSilentMode);
// Commit the edits!
editor.commit();
}
}String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();<manifest ...>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
...
</manifest>// Checks if external storage is available for read and write
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(state) ? true : false;
}
// Checks if external storage is available to at least read
public boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state) ? true : false;
}public File getAlbumStorageDir(String albumName) {
// Get the directory for the user's public pictures directory.
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), albumName);
if (!file.mkdirs()) {
Log.e(LOG_TAG, "Directory not created");
}
return file;
}public static final String ALBUMS_DIRNAME = "albums";
public File getAlbumStorageDir(String albumName) {
File file = new File(getExternalFilesDir(ALBUMS_DIRNAME));
if (!file.mkdirs()) {
Log.e(LOG_TAG, "Directory not created");
}
return file;
}public class MyDatabaseHelper extends SQLiteOpenHelper {
public static final String DB_NAME = "database.db";
public static final int DB_VERSION = 1;
public static final String TBL_ROOMS_NAME = "rooms";
public static final String TBL_ROOMS_COLUMN_ID = "id";
public static final String TBL_ROOMS_COLUMN_LABEL = "label";
public static final String TABLES_SCHEMA = "create table "
+ TBL_ROOMS_NAME + "(" + TBL_ROOMS_COLUMN_ID
+ " integer primary key autoincrement, " + TBL_ROOMS_COLUMN_LABEL
+ " text not null);";
protected final Context context;
protected SQLiteDatabase db;
public MyDatabaseHelper(Context pContext) {
super(pContext, DB_NAME, null, DB_VERSION);
context = pContext;
}
@Override
public void onCreate(SQLiteDatabase pDb) {
db = pDb;
db.execSQL(TABLES_SCHEMA);
initDb();
}
private void initDb() {
new Thread(new Runnable() { // Starts a thread to load the database tables
public void run() {
for (String s : new String[] { "A01", "A02" }) {
ContentValues v = new ContentValues();
v.put(TBL_ROOMS_COLUMN_LABEL, s);
db.insert(TBL_ROOMS_NAME, null, v);
}
}
}).start();
}
public final SQLiteDatabase open() {
db = getWritableDatabase();
return db;
}
public final void close() {
db.close();
}
}// edu.android.db.BaseDAO.java
public abstract class BaseDAO<T>
extends MyDatabaseHelper {
public BaseDAO(Context pContext) {
super(pContext);
}
public SQLiteDatabase getDb() {
return db;
}
abstract public void create(T item);
abstract public T read(long id);
abstract public void update(T item);
abstract public void delete(long id);
abstract public List<T> list();
}
// edu.android.model.Room.java
public class Room {
private long id;
private String label;
public long getId() { return id; }
public void setId(long pId) { id = pId; }
public String getLabel() { return label; }
public void setLabel(String pLabel) { label = pLabel; }
@Override
public String toString() {
return String.format("(%d) %s", id, label);
}
}
// edu.android.db.RoomDAO.java
public class RoomDAO extends BaseDAO<Room> {
public void create(Room item) {
ContentValues value = new ContentValues();
value.put(MyDatabaseHelper.TBL_ROOMS_COLUMN_LABEL,
item.getLabel());
db.insert(MyDatabaseHelper.TBL_ROOMS_NAME, null, value);
}
public Room read(long id) {
Cursor c = db.rawQuery(
"select * from " + MyDatabaseHelper.TBL_ROOMS_NAME
+ " where " + MyDatabaseHelper.TBL_ROOMS_COLUMN_ID
+ " = ?", new String[]{String.valueOf(id)});
Room r = new Room();
// ... // ...
r.setId(c.getLong(0));
r.setLabel(c.getString(1));
c.close();
return r;
}
public void update(Room item) {
ContentValues value = new ContentValues();
value.put(MyDatabaseHelper.TBL_ROOMS_COLUMN_LABEL,
item.getLabel());
db.update(MyDatabaseHelper.TBL_ROOMS_NAME, value,
MyDatabaseHelper.TBL_ROOMS_COLUMN_ID
+ " = ?",
new String[] {String.valueOf(item.getId())}
);
}
public void remove(long id) {
db.delete(MyDatabaseHelper.TBL_ROOMS_NAME,
MyDatabaseHelper.TBL_ROOMS_COLUMN_ID
+ " = ?", new String[] {String.valueOf(id)}
);
}
@Override
public List<Room> list() {
List<Room> list = new ArrayList<Room>();
Cursor cursor = db.rawQuery("select * from "
+ TBL_ROOMS_NAME, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
list.add(cursorToRoom(cursor));
cursor.moveToNext();
}
cursor.close();
return list;
}
private Room cursorToRoom(Cursor cursor) {
Room room = new Room();
room.setId(cursor.getLong(0));
room.setLabel(cursor.getString(1));
return room;
}
}