Unit 4
Unit 4
Preference, Storage
Purpose:
Allows applications to share data with other applications. They provide a
standard interface for other applications to access and modify the
data.
<provider
android:name=".MyContentProvider"
android:authorities="com.example.provider"
android:exported="true"/>
Define the URIs that the content provider will handle using a UriMatcher.
Define the structure of the data, constants for URIs, and column names.
content://com.example.provider/table_name/123
URIs for Data Location: Content providers are accessed using URIs that
follow a specific format. These URIs typically include three parts:
Authority: A unique identifier for the specific content provider you want
to interact with.
Path: Specifies the data path within the provider (e.g., denoting a table
name or a specific record).
Create (Insert):
o Adds new data to the content provider.
o Method: insert(Uri uri, ContentValues values)
o ContentValues values = new ContentValues();
Example:
Read (Query):
Example
Update:
Example
values.put("column_name", "new_value");
Delete:
o Removes data from the content provider.
o Method: delete(Uri uri, String selection, String[] selectionArgs)
Example
Example Implementation:
1. Contract Class:
Internal Storage:
Example
fos.write(fileContents.getBytes());
fos.close();
External Storage:
Use Cases:
Key Points:
Example Usage:
fos.write(fileContents.getBytes());
fos.close();
Then, you can use File class methods to create, read, write, and
manage files within those directories.
Key Points:
Example Usage:
SharedPreferences sharedPreferences =
getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
editor.putString("key", "value");
editor.apply();
Use Cases:
Use the Editor object obtained from the shared preferences to put
(store) and get (retrieve) data using key-value pairs.
Key Points:
Relational Database: Organizes data into tables with rows and columns,
allowing for efficient data retrieval and manipulation.
SQLiteOpenHelper Class:
Key Points:
OnCreate(): Called when the database is created for the first time.
Example Usage:
@Override
@Override
if (oldVersion < 2) {
Row by Row Exploration: The Cursor allows you to iterate through each
row returned by the query. Imagine a table of contacts; a Cursor lets you
visit each contact record (row) one by one.
Extracting Column Values: Within each row, you can access data stored in
specific columns. The Cursor provides methods like getString(), getInt(),
etc., corresponding to different data types (text, integer, etc.) stored in
the columns. By calling these methods, you extract the desired data
values from each row.
Example Usage:
SQLiteDatabase db = dbHelper.getReadableDatabase();
while (cursor.moveToNext()) {
// Process data...