2 Resources under Android Flashcards

1
Q

Why should you externalise resources in Android?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How are resources stored in Android SDK?

A

They are externalised in various /res folders. Mostly in XML (Manifest, layout, values (strings).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How are resources accessed in Android?

A
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is localisation?

A

Providing resources for different regions, ie res/values-en, res/values-fr.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What type of resource must you always provide?

A

Default

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you access resources in Java code

A

By using the R class
R.
ie R.drawable.image

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you access a resource in XML

A

@string/hello

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Name 3 methods of local data storage…

A

Key-value pairs (Shared preference)
Internal file storage
SQLite Database

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Describe key-value pairs

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Describe internal file storage

A

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();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Describe SQLite Database Storage

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Describe Content Providers

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Give some examples of Content Providers

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you access data on a Content Provider?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Methods of Content Provider

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

You don’t need to develop your own provider if you don’t intend to share your data with other applications.

A

true

17
Q

You need your own provider to provide custom search suggestions in your own application.

A

True

You also need your own provider if you want to copy and paste complex data or files from your application to other applications.

18
Q

Useful methods of Internal Storage…

A

getFilesDir()
Gets the absolute path to the filesystem directory where your internal files are saved.

getDir()
Creates (or opens an existing) directory within your internal storage space.

deleteFile()
Deletes a file saved on the internal storage.

fileList()
Returns an array of files currently saved by your application.

19
Q

What is the Content Provider Package?

A

android.provider

20
Q

What does the SQLiteDatebase query() method return?

A

A cursor.

A cursor returns all the rows from a searched query and allows navigation.

21
Q

Name 2 classes associated with the android.database.sqlite package

A

SQLiteDatabase (represents a database)
SQLiteOpenHelper (Create, open, manage DB)
SQLiteQueryBuilder (help build more complex queries)

22
Q

What are methods of the SQLiteOpenHelper class

A

getReadableDatabase() - create/open a readable database
getWriteableDatabase() - create/open a writeable database.
getDatabaseName() - returns name

23
Q

What are methods of SQLiteQueryBuilder

A

appendColumns(StringBuilder s, String[] columns) - adds non-null names in column s, separated with commas

getTables() - returns list of tables
buildQueryString() - build SQL query string

24
Q

What does a content provider do?

A

Provides a central repository of data for external applications

25
Q

3 methods of a content provider?

A

Methods:
query() – method receives a request from from client, result is returned as a cursor object.
insert() – Inserts new record into the content provider
delete() – Deletes existing record from content provider

26
Q

How is a Content Providers URI made up?

A

:////

ie content://contacts/people/5

27
Q

5 steps to creating a content provider?

A
  1. Create a ContentProvider class which extends ContentProviderbaseclass
  2. Define your URI ie content://contacts/people/id (prefix-authority_type-data_type-id)
  3. Create an SQLite DB. Use SQLiteOpenHelper method to open/ create DB’s
  4. Implement Content Provider and perform queries
  5. Register CP in activity file using tag
28
Q

How does the content provider return .query() results

A

Since a Cursor is a “list” of rows, a good way to display the contents of a Cursor is to link it to a ListView via a SimpleCursorAdapter.

29
Q

Give an example of the SQLiteOpenHelper class

A

Private SQLiteDatabase myDatabase
Private SQLiteOpenHelper myHelper;

myDatabase = myHelper.getWriteableDatabase();

30
Q

Describe query() method of Content Provider

A

query()
Retrieve data from your provider. Use the arguments to select the table to query, the rows and columns to return, and the sort order of the result. Return the data as a Cursor object.

public abstract Cursor query (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)

The ContentProvider.query() method must return a Cursor object, or if it fails, throw an Exception. If you are using an SQLite database as your data storage, you can simply return the Cursor returned by one of the query() methods of the SQLiteDatabase class. If the query does not match any rows, you should return a Cursor instance whose getCount() method returns 0.

31
Q

Describe insert() method of ContentProvider

A

insert()
Insert a new row into your provider. Use the arguments to select the destination table and to get the column values to use. Return a content URI for the newly-inserted row.

public abstract Uri insert (Uri uri, ContentValues values)

The insert() method adds a new row to the appropriate table, using the values in the ContentValues argument. If a column name is not in the ContentValues argument, you may want to provide a default value for it either in your provider code or in your database schema.

This method should return the content URI for the new row. To construct this, append the new row’s _ID (or other primary key) value to the table’s content URI, using withAppendedId().

32
Q

Describe delete() method of Content Provider

A

delete()
Delete rows from your provider. Use the arguments to select the table and the rows to delete. Return the number of rows deleted.

public abstract int delete (Uri uri, String selection, String[] selectionArgs)

The delete() method does not have to physically delete rows from your data storage. If you are using a sync adapter with your provider, you should consider marking a deleted row with a “delete” flag rather than removing the row entirely. The sync adapter can check for deleted rows and remove them from the server before deleting them from the provider.