Chapter 4
Chapter 4
This makes internal storage a good place for internal app data that the user doesn't
need to directly access.
The system provides a private directory on the file system for each app where you can
organize any files your app needs.
When the user uninstalls your app, the files saved on the internal storage are removed.
Because of this behavior, you should not use internal storage to save anything the user
expects to persist independently of your app.
COMPILED BY CHALEW Z.(MSC) 4
Internal cache files
If you had like to keep some data temporarily, rather than store it persistently, you should
use the special cache directory to save the data.
Each app has a private cache directory specifically for these kinds of files.
When the device is low on internal storage space, Android may delete these cache files to
recover space.
You should always maintain the cache files yourself and stay within a reasonable limit of
space consumed, such as 1MB.
When the user uninstalls your app, these files are removed.
fOut.write(str.getBytes());
fOut.close();
COMPILED BY CHALEW Z.(MSC) 6
Reading file
In order to read from the file you just created , call the openFileInput() method with the name
of the file. It returns an instance of FileInputStream.
After that, you can call read method to read one character at a time from the file and then you
can print it. Its syntax is given below
int c;
String temp="";
while( (c = fin.read()) != -1){
temp = temp + Character.toString((char)c);}// temp contains all the data of the file.
fin.close();
COMPILED BY CHALEW Z.(MSC) 7
External storage
Every Android device supports a shared "external storage" space that you can use to save files.
This space is called external.
It is a storage space that users can mount to a computer as an external storage device, and it might
even be physically removable (such as an SD card).
Files saved to the external storage are world-readable and can be modified by the user when they
enable USB mass storage to transfer files on a computer.
So before you attempt to access a file in external storage in your app, you should check for the
availability of the external storage directories as well as the files you are trying to access.
Most often, you should use external storage for user data that should be accessible to other apps
and saved even if the user uninstalls your app, such as captured photos or downloaded files.
The system provides standard public directories for these kinds of files, so the user has one
location for all their photos, ringtones, music, and such.
The SharedPreferences APIs allow you to read and write persistent key-value pairs of primitive data
types: booleans, floats, ints, longs, and strings.
The key-value pairs are written to XML files that persist across user sessions, even if your app is killed.
You can manually specify a name for the file or use per-activity files to save your data.
The API name "shared preferences" is a bit misleading because the API is not strictly for saving "user
preferences," such as what ringtone a user has chosen.
You can use SharedPreferences to save any kind of simple data, such as the user's high score.
COMPILED BY CHALEW Z.(MSC) 9
Shared preferences
In order to use shared preferences, you have to call a method getSharedPreferences() that
returns a SharedPreference instance pointing to the file that contains the values of
preferences.
General Syntax:
The first parameter is the key and the second parameter is the MODE.
A database is an organized collection of data that can be easily accessed, managed, and updated.
It used to store information in a structured format, allowing for efficient retrieval and
manipulation of data.
They are fundamental in various applications, from small websites to large enterprise systems.
Here are key features and concepts associated with relational databases:
Tables: Data is stored in tables, which consist of rows and columns.
Rows: Each row in a table represents a single record or entry.
Columns: Each column in a table represents a specific attribute or field of the record.
Relationships: Data in different tables can be related to one another through foreign keys, which link
tables.
Structured Query Language (SQL): Relational databases typically use SQL, a standardized
programming language, to define, manipulate, and query data.
SQL allows users to perform operations such as: SELECT, INSERT, UPDATE, and DELETE
Multi-user: concurrency features let many users view/edit data at same time
SQLite supports standard relational database features like SQL syntax, transactions and
prepared statements.
All other types must be converted into one of these fields before saving them in the database.
Using an SQLite database in Android does not require any database setup or administration.
You only have to define the SQL statements for creating and updating the database.
Afterwards the database is automatically managed for you by the Android platform.
If your application creates a database, this database is by default saved in the directory like
DATA/data/APP_NAME/databases/FILENAME.
SQLite is very small and light weight, less than 400KiB fully configured or less than 250KiB with
optional features omitted.
SQLite supports most of the query language features found in SQL92 (SQL2) standard.
SQLite is available on UNIX (Linux, Mac OS-X, Android, iOS) and Windows (Win32, WinCE, WinRT)
Both methods receive an SQLiteDatabase object as parameter which represents the database.
SQLiteDatabase
SQLiteDatabase is the base class for working with a SQLite database in android and provides
methods to open, query, update and close the database.
More specifically SQLiteDatabase provides the insert() , update() and delete() methods.
In addition it provides the execSQL() method, which allows to execute an SQL statement
COMPILED BY CHALEW Z.(MSC) 21
The object ContentValues allows to define key/values. Cont..
The "key" represents the table column identifier and
the "value" represents the content for the table record in this column.
Queries can be created via the rawQuery() and query() methods or via the
SQLiteQueryBuilder class .
query() Example
Process of maintaining the availability of data generated from the source and
maintaining consistency.
A consistent copy of data is a copy which may not be identical to the present data
record at the data generating source, but must satisfy all the required functions
and domain dependent specific rules .
The domain specific rules are in terms of resolution, precision, data format,
and time interval permitted for replication.
The data synchronization process typically updates both data sources by transferring
only additions, changes, and deletions.
The following are the reasons why data synchronization is required in Mobile computing:
Data synchronization is required between the mobile devices and their service provider.
It is also required between the device and personal area computer and nearby wireless access
points (in Wi-Fi connection) and other nearby devices.
It is used to establish consistency among data from a data source to the target data storage and
vice versa.
Data Replication in mobile computing means the sharing of information to ensure data
consistency between software and hardware resources connected via the internet, to
improve reliability, availability, fault-tolerance, and accessibility of data.
In simpler terms, data replication is the process of storing different copies of the
database at two or more sites in order to improve data availability in less time and at a
cheaper cost.