Sunteți pe pagina 1din 14

Persistent D ata Storage

Data Storage Media Joannes Arthur Galon Pajatin


User Permission Klieser Jun Asid Baluyos
SQLite Database Carl Mark Ibit & Rai Ray Acopiado

D ata S torag e M ed ia
The Android platform, in combination with Java, provides a
robust set of tools that you can use to store data.
Regardless of the situation, Android provides various options
when it comes to storing data.
Android provides various locations to save data. These are
as follows:
Shared preferences These are private data stored in key value pairs.
Internal storage This is a location where all files can be saved on the
devices internal storage. By default, files stored in internal storage are
private to your application, and other applications cannot access them
(neither can the user of the device). When the user uninstalls the
application, the private files are removed.

D ata S torag e M ed ia
- Local cache This can be used if you dont want to store data
persistently. The internal data directory is where you should create the
cache. If you store data here and the system gets low on internal storage
space, Android may delete these files to reclaim space. The limit of
consumable space is around 1MB.
- External storage This can either be the removable storage such as
a Secure Digital Card (SD Card) or nonremovableinternal storage.
- SQLite database This database is a lightweight SQL (Structured
Query Language) database implementation that is used by Android. It is
available across various platforms.
- Network connection This is also known as remote storage. It can be
any remote data source that user have access to.

U ser P erm ission


Storing data anywhere on the device requires permission
from the user in Android.
When users install applications from the Android Market,
the applications manifest file is inspected for required
permissions that the application needs to operate.
Anytime the application needs access to sensitive
components such as external storage, access to the
Internet, phone device info, and so on, the user is notified
that the application would like to access these components.
It is then up to the user to decide whether she would like to
install the application.

U ser P erm ission


When permission is requested, you need to add them to
the AndroidManifest.xml file.
No permission is necessary to work with an SQLite
database; therefore, two permissions should be added
to the Task Reminder application that will be required
when the alarm manager code is added:
android.permission.RECEIVE_BOOT_COMPLETEThis permission
allows the application access to know when the phone reboots.
android.permission.WAKE_LOCKThis permission allows the
phone to keep the phone awake while its performing some
background processing.

U ser P erm ission


A lot of applications require access to the Internet to operate.
Some applications also need to write data to the SD Card. If you
need either of these, you need to add the following permissions:
Internet: android.permission.INTERNET
SD Card: android.permission.WRITE_EXTERNAL_STORAGE

Permissions can be added in the AndroidManifest.xml file in one


of two ways:
Through the AndroidManifest.xml Permissions Editor.
Choose AddUsesPermission, and then choose the permission from the dropdown list.

Through manually editing the XML file.

U ser P erm ission


The XML permission request looks like this:
<uses-permission android:name=android.permission.WAKE_LOCK
/>

If permissions for the application are not declared, the


application will not function as expected; sometimes
run-time exceptions will be thrown and crash your
application.

S Q Lite D atab ase


The Task Reminder application needs a place to store and
retrieve tasks, and the best place for this kind of information
is inside an SQLite database.
Your application needs to read, create, update, and delete
tasks from the database.
The two activities in the Task Reminder application need to
perform various duties to operate.
TaskReminderEditneeds to do the following:
Create a new record.
Read a record so that it can display the details for editing.
Update the existing record.

S Q Lite D atab ase


The TaskReminderneeds to perform these duties:
Read all the tasks to show them on the screen.
Delete a task by responding to the click event from the context
menu after a user has long-pressed an item.

To work with an SQLite database, communication with


SQLite through classes in the android.databasepackage
is needed.
It is very common to abstract as much of the database
communication away from the Activity objects as
possible.
The database mechanisms are placed into another Java
file to help separate the application into layers of

S Q Lite D atab ase


The first thing to do is create a Java file in the Android
project that will house all the database-centric code.
Name this file RemindersDbAdapter.java.
This would be a simple implementation of the adapter
software engineering pattern.
The adapter pattern is simply a wrapper class that allows
incompatible classes to communicate with each other.
By creating an adapter to handle the database communication,
you can communicate with this class via the programming
language of Java while this adapter class does the translation and
adapts certain Java requests into SQLite-specific commands.

S Q Lite D atab ase


SQLite creates a table by the name of reminders in a
database called data. The columns and how theyre
built in the create script are described as follows:
create table DATABASE_TABLE This portion of the script informs
SQLite that you would like to create a database table with the
name of reminders.
ROW_ID This property acts as the identifier for the task. It has
the integer primary key autoincrementattributes applied to it.
KEY_TITLE This is the title of the task that the user provides,
such as Schedule Vacation. The text attribute informs SQLite
that you are working with a text column.
KEY_BODY This is the body or description of the task. The
attributes for this column are the same as for KEY_TITLE.
KEY_DATE_TIME This is where the date and time of the reminder

S Q Lite D atab ase


To create a database table, an implementation of
SQLiteOpenHelpershould be provided.
The following is the code for the
RemindersDbAdapterclass.
This creates a nested Java class inside the
RemindersDbAdapterclass.

private static class DatabaseHelper extends SQLiteOpenHelper{


DatabaseHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION
}
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int Newversion){
Log.w(TAG, Upgrading database from version + oldVersion + to +
newVersion +
+ , which will destroy all old data);
db.execSQL(DROP TABLE IF EXISTS + DATABASE_TABLE);
on Create(db);
}
}

S Q Lite D atab ase


Line 1 is the implementation of the SQLiteOpenHelper.
Line 3 creates, opens, and/or manages a database. The database is
not actually created or opened until getReadableDatabase() or
getWriteableDatabase() is called on the SQLiteOpenHelperinstance
in this case, it would be the mDbHelpervariable.
Line 7 calls the onCreate() method when the database is created for
the first time.
Line 8creates the database and the database table. The execSQL()
method accepts an SQL script string as a parameter. This is the SQL
that the SQLite database executes to create the database table.
Line 12is use for the upgrade of the existing database. This use
onUpgrade() method.

S-ar putea să vă placă și