1/30

Android Development

Android Framework Basics

2/30

Agenda

3/30

Shared Preferences

4/30

Shared Preferences

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);
    }
5/30

Shared Preferences

...
@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();
    }
}
6/30

Internal Storage

7/30

Internal Storage

String FILENAME = "hello_file";
String string = "hello world!";

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
8/30

Internal Storage

9/30

External Storage

10/30

External Storage

<manifest ...>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    ...
</manifest>
11/30

External Storage

/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
    }
    return false;
}
12/30

External Storage

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;
}
13/30

External Storage

14/30

SQLite Databases

15/30

SQLite Databases

public class DictionaryOpenHelper extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 2;
    private static final String DICTIONARY_TABLE_NAME = "dictionary";
    private static final String DICTIONARY_TABLE_CREATE =
                "CREATE TABLE " + DICTIONARY_TABLE_NAME + " (" +
                KEY_WORD + " TEXT, " +
                KEY_DEFINITION + " TEXT);";

    DictionaryOpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DICTIONARY_TABLE_CREATE);
    }
}
16/30

SQLite Databases

17/30

Room

18/30

Room gradle dependencies

Ad this dependeny to your app build.gradle

implementation "androidx.room:room-runtime:2.2.5"
annotationProcessor "androidx.room:room-compiler:2.2.5"
19/30

Entity

20/30

Entity Example

Use @PrimaryKey(autoGenerate = true) to let Room create and auto increment unique ids for your entries.

import androidx.room.Entity;
import androidx.room.PrimaryKey;

@Entity
public class MovieDetail {

	@PrimaryKey(autoGenerate = true)
	public int uid;

	public String Title;

	public String Year;

	public String Poster;

}
21/30

Data access object

22/30

DAO Example

import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;

import java.util.List;

@Dao
public interface MovieDetailDao {

	@Query("SELECT * FROM moviedetail")
	List<MovieDetail> getAll();

	@Insert
	void insert(MovieDetail movieDetail);

	@Delete
	void delete(MovieDetail movieDetail);
}
23/30

Alternative databases

24/30

Database

25/30

Database Example

import androidx.room.Database;
import androidx.room.RoomDatabase;

@Database(entities = {MovieDetail.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
	public abstract MovieDetailDao movieDetailDao();
}
26/30

Room Example

Insert an entitiy into database

new Thread(new Runnable() {
	@Override
	public void run() {
		AppDatabase db = Room.databaseBuilder(getApplicationContext(),
				AppDatabase.class, "movieDb").build();

		db.movieDetailDao().insert(movieDetail);
	}
}).start();
27/30

Room Example

Get a list of entities from database

new Thread(new Runnable() {
	@Override
	public void run() {
		AppDatabase db = Room.databaseBuilder(getApplicationContext(),
				AppDatabase.class, "movieDb").build();

		List<MovieDetail> movieDetailList = db.movieDetailDao().getAll();
		Log.d("TAG", movieDetailList.get(0).Title);
	}
}).start();
28/30

Network Connections

29/30

Summary

30/30

Recab Questions