2 Resources under Android Flashcards
Why should you externalise resources in Android?
So you can maintain them independently.
Externalising resources allows you can create alternative screen sizes or localised resources without affecting the Java source code.
How are resources stored in Android SDK?
They are externalised in various /res folders. Mostly in XML (Manifest, layout, values (strings).
How are resources accessed in Android?
via the R class ie R.drawable.file.xml or R.layout. Except files in the res/values folder where for eg creates a R.string resource and creates a R.color resource.
What is localisation?
Providing resources for different regions, ie res/values-en, res/values-fr.
What type of resource must you always provide?
Default
How do you access resources in Java code
By using the R class
R.
ie R.drawable.image
How do you access a resource in XML
@string/hello
Name 3 methods of local data storage…
Key-value pairs (Shared preference)
Internal file storage
SQLite Database
Describe key-value pairs
For local data storage, key value pairs can be used to store user preferences.
The SharedPreferences class can be used to store data that can accessed throughout the application. The SharedPreferences class is exposed via the object. Also, by instantiating MODE_PRIVATE the preferences will be private to the application. Example:
private SharedPreferences settings;
(OnCreate)
settings = getSharedPreferences(“my_pref”, MODE_PRIVATE);
Alternatively, if we don’t want the preferences to be shared in the app we can call the getPreferences(MODE_PRIVATE) method instead which will result in the preferences only being saved within the activity.
Describe internal file storage
Data can also be stored locally in files. Files may be in a number of formats such as images, XML or text. The files are by default private to the application and are removed when the app is.
To write a file call openFileOutput(); and then use .write() and .close() methods to write and close the file output.
Similarly to read we can use openFileInput();
Useful methods: getFileDir(), getDir(), deleteFile() and fileList();
With java.io.* package we can use Java techniques such as BufferedWriter, ObjectOutputStream & Serialisable.
The File class can be used with internal storage.
File myFile = new File(context.getFilesDir(), filename);
Can query free space with getFreeSpace(); and delete with file.delete();
Describe SQLite Database Storage
Structured data saved in a DB
Android has built in support
All classes/ interfaces in android.database.sqlite package
DB are stored locally to app
Can share with other apps via content provider
Classes
SQLiteOpenHelper
Create a helper object to create, open and/or manage a DB
Methods:
getDatabaseName()
getReadableDatabase() - create or open a DB
getWriteableDatabase()
…. extends SQLiteOpenHelper
public static final String COLUMN_ID = “student_no”;
private static final String TABLE_CREATE = “CREATE TABLE” + COLUNM_ID
Then create constructor
SQLLiteQueryBuilder
Helps build and manage queries - sends to SQLite DB objects.
Methods:
appendColumns(StringBuilder s, String[ columns) - adds the names in column s)
getTables() - returns list of tables being queried
buildQueryString() - build SQL query
Describe Content Providers
Content providers are a method for managing access to structured data.
CP’s encapsulate data and has mechanisms for defining data security
CP’s connect data in one process with code running in another process
Android SKD has CP’s for audio, video, images, contact info etc
CP’s provide to external applications one or more tables providing data (usually SQLite). One use is a keyboard storing non standard spellings.
Give some examples of Content Providers
Other examples:
browser history, bookmakrs
maps - previous searches
Stored messages sent/ recieved
iOS allows users to simultaneously search their contacts, docs, web, app store all at once. Done through tables, usually under the hood with SQLite.
How do you access data on a Content Provider?
Use the ContentResolver object in your app’s context.
The ContentResolver object communicates with the provider object as a client, an instance of a class that implements ContentProvider.
The provider object receives the request from the client (resolver), performs the requested action and returns the result.
For example, to get a list of the words and their locales from the User Dictionary Provider, you call ContentResolver.query(). The query() method calls the ContentProvider.query() method defined by the User Dictionary Provider.
The ContentProvider uses the path part of the content URI to choose the table to access. A provider usually has a path for each table it exposes.
Methods of Content Provider
onCreate() - when provider started
query() - method receives request from client, result returned as a Cursor object
insert() - inserts new record into CP
delete()
update()
getType() - returns MIME type of data at URI