Download presentation
Presentation is loading. Please wait.
1
Reactive Android Development
CS T & CS T Summer 2016
2
Logging Logging
3
Logging Found in android.util.Log
Provides different logging levels in the form of different methods: Log.v – verbose Log.d – debug Log.i – info Log.w – warning Log.e – error Each takes a tag and a message and can optionally take an exception instance as a third argument. Also provides wtf (what a terrible failure) methods for conditions that should never happen. Depending on configuration, this may kill the app.
4
Persistence Again Last time, we introduced SharedPreferences
Key-Value store An app can have several named stores Acquired by either getPreferences(mode) or getSharedPreferences(filename, mode) Read with preferences.getString(key) preferences.getInt(key), etc. Written with preference.edit().putString(key,value), etc.
5
More Persistence (Files)
Internal or External Storage External used to refer to removable storage Now, might be another internal-storage partition But there are always two External storage may not be available Writing to external storage requires permissions <manifest ...> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> ... </manifest>
6
More Persistence (Files)
Internal Storage getFilesDir() Returns the location for storing permanent files getCacheDir() Returns the location for storing temporary cache files They may be deleted without warning if storage runs low
7
More Persistence (Files)
External Storage Public files Other apps can read/write Will not be deleted automatically when uninstalling the app getExternalStoragePublicDirectory() Private files Technically accessible to other apps Will be automatically deleted when uninstalling the app getExternalFilesDirectory()
8
Structured Persistence (SQL)
Recommended: Contract class public final class FeedReaderContract { // To prevent someone from accidentally instantiating the contract class, // give it an empty constructor. public FeedReaderContract() {} /* Inner class that defines the table contents */ public static abstract class FeedEntry implements BaseColumns { public static final String TABLE_NAME = "entry"; public static final String COLUMN_NAME_ENTRY_ID = "entryid"; public static final String COLUMN_NAME_TITLE = "title"; public static final String COLUMN_NAME_SUBTITLE = "subtitle"; ... }
9
Structured Persistence (Creation)
private static final String TEXT_TYPE = " TEXT"; private static final String COMMA_SEP = ","; private static final String SQL_CREATE_ENTRIES = "CREATE TABLE " + FeedEntry.TABLE_NAME + " (" + FeedEntry._ID + " INTEGER PRIMARY KEY," + FeedEntry.COLUMN_NAME_ENTRY_ID + TEXT_TYPE + COMMA_SEP + FeedEntry.COLUMN_NAME_TITLE + TEXT_TYPE + COMMA_SEP // Any other options for the CREATE command " )"; private static final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS " + FeedEntry.TABLE_NAME;
10
Structured Persistence (Creation)
public class FeedReaderDbHelper extends SQLiteOpenHelper { // If you change the database schema, you must increment the database version. public static final int DATABASE_VERSION = 1; public static final String DATABASE_NAME = "FeedReader.db"; public FeedReaderDbHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } public void onCreate(SQLiteDatabase db) { db.execSQL(SQL_CREATE_ENTRIES); } public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // This database is only a cache for online data, so its upgrade policy is // to simply to discard the data and start over db.execSQL(SQL_DELETE_ENTRIES); onCreate(db); } public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { onUpgrade(db, oldVersion, newVersion);
11
Structured Persistence (Insertion)
SQLiteDatabase db = mDbHelper.getWritableDatabase(); // Create a new map of values, where column names are the keys ContentValues values = new ContentValues(); values.put(FeedEntry.COLUMN_NAME_ENTRY_ID, id); values.put(FeedEntry.COLUMN_NAME_TITLE, title); values.put(FeedEntry.COLUMN_NAME_CONTENT, content); // Insert the new row, returning the primary key value of the new row long newRowId; newRowId = db.insert( FeedEntry.TABLE_NAME, FeedEntry.COLUMN_NAME_NULLABLE, values);
12
Structured Persistence (Query)
SQLiteDatabase db = mDbHelper.getReadableDatabase(); // Define a projection that specifies which columns from the database // you will actually use after this query. String[] projection = { FeedEntry._ID, FeedEntry.COLUMN_NAME_TITLE, FeedEntry.COLUMN_NAME_UPDATED, ... }; // How you want the results sorted in the resulting Cursor String sortOrder = FeedEntry.COLUMN_NAME_UPDATED + " DESC"; Cursor c = db.query( FeedEntry.TABLE_NAME, // The table to query projection, // The columns to return selection, // The columns for the WHERE clause selectionArgs,//The values for the WHERE clause null, // don't group the rows null, // don't filter by row groups sortOrder // The sort order );
13
Fragments
14
Fragments Can be declared in the XML for an activity
Or added dynamically (in the Activity's onCreate method) Can also be replaced at runtime in response to user input The change can be registered so that the back button still works!
15
Fragment communication
Fragments must be associated with an Activity Delivered by the onAttach(Activity activity) method All outgoing messages should be sent to the activity rather than communicating with other fragments directly.
16
Fixing a messy repository
mkdir MyFirstApp For all files <f> that were at the base level git mv <f> MyFirstApp git commit It will ask you for a comment (usually by opening vim, but sometimes nano) You can also use 'git commit –m "message"' git push
17
About git tags Tags are a way of marking a specific point in your development for future reference. You can create one with your current content with git tag –a <tagname> -m "<message>" You can create one for an old version with the checksum for its commit Git tag –a <tagname> <checksum> -m "<message>"
18
Suggestions from Students
The following slides contain suggestions about places to get additional training and tools that might help with development
19
slidenerd Android tutorial reference
20
GenyMotion Compatible? With monkey runner.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.