Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
48 views32 pages

Mad Unit-3

The document discusses different ways to store and manage data in Android applications such as SharedPreferences, files, SQLite databases, and content providers. SharedPreferences allows storing simple key-value pairs. Files can be used for reading and writing large amounts of data to internal and external storage. SQLite databases provide a way to store structured data in tables. Content providers allow sharing data between applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views32 pages

Mad Unit-3

The document discusses different ways to store and manage data in Android applications such as SharedPreferences, files, SQLite databases, and content providers. SharedPreferences allows storing simple key-value pairs. Files can be used for reading and writing large amounts of data to internal and external storage. SQLite databases provide a way to store structured data in tables. Content providers allow sharing data between applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

UNIT III: DATA

PERSISTENCE
PRABHAKARAN S
DEPARTMENT OF CSE
SRMIST-KTR CAMPUS
SharedPreferences
• Android provides many ways of storing data of an
application. One of this way is called Shared
Preferences API.
• Eg:- Username, Password, Firstname, Lastname,
email id, mobile no, DOB, etc..(profile details)
• Shared Preferences allow you to save and retrieve
data based on key-value pair. (booleans, floats, ints,
longs, and strings).
• Datatype variable = value
SharedPreferences
• Shared – distribute data
• Preference – important or preferable
• Preference File
• Preference file is XML file saved in internal memory in
device.
• You can create a new shared preference file or access
an existing one by calling one of these methods:
• Activity preference
• Custom preference
SharedPreferences
• Activity preference : getPreferences (int mode)
• developer have to call function getPreferences(int
mode)
• Use this if you need to use only one shared preference
file for the activity.
• Because this retrieves a default shared preference file
that belongs to the activity.
• Custom Preferences : getSharedPreferences(String
name,int mode)
• Developer needs to use getSharedPreferences
(String name, int mode) for custom preferences.
SharedPreferences
• Save Data In SharedPreferences:
• Call the method edit() to get a SharedPreferences.
Editor;
• To add SharedPreferences values use the methods
as putBoolean(), putString(), putint(String Keyname
int value);
• To save the values you must call commit().
• Read or get data from SharedPreferences
• To read SharedPreferences values use the methods as
getBoolean() and getString().
• Eg:- To get saved int value just
call getInt(“GameScore”,-1) function where
“GameScore” is key and -1 is a default value in case no
value was saved for GameScore.
• To remove data call remove(String key)
• To clear all data just call function clear().
SharedPreferences
• Below is an example of storing and removing data
from a preference:
SharedPreferences
SharedPreferences
SharedPreferences
File Handling
• A File object works well for reading or writing large
amounts of data.
• The exact location of the where your files can be saved
might vary across devices.
• To view files on a device, you can log the file location
provided by methods such as File.getAbsolutePath(), and
then browse the device files with Android
Studio's DeviceFileExplorer.
• All Android devices have two file storage areas:
"internal" and "external" storage.
• Most Android devices offered built-in non-volatile
memory (internal storage), plus a removable storage
medium such as a micro SD card (external storage).
File Handling
Internal storage External storage
• It's always available. • It's not always available,
because the user can mount
• Files saved here are the external storage as USB
accessible by only your storage and in some cases
app. remove it from the device.
• It's world-readable, so files
• When the user saved here may be read
uninstalls your app, the outside of your control.
system removes all • When the user uninstalls your
your app's files from app, the system removes your
internal storage. app's files from here only if
you save them in the directory
from getExternalFilesDir().
File Handling
• Write a file
• When saving a file to internal storage, you can
acquire the appropriate directory as a File by calling
one of two methods:
• getFilesDir()
• Returns a File representing an internal directory for
your app.
• getCacheDir()
• Returns a File representing an internal directory for
your app's temporary cache files.
• File file = new File(context.getFilesDir(), filename);
File Handling
File Handling
• Write a cache file
• If you instead need to cache some files, you should
use createTempFile(). For example, the following
method extracts the file name from a URL and
creates a file with that name in your app's internal
cache directory:
File Handling
• Open an existing file
• To read an existing file, call openFileInput(name),
passing the name of the file.
• You can get an array of all your app's file names by
calling fileList().
• Open a directory
• You can open a directory on the internal file system
with the following methods:
• getFilesDir()
• Returns a File representing the directory on the file
system that's uniquely associated with your app.
File Handling
• getDir(name, mode)
• Creates a new directory (or opens an existing
directory) within your app's unique file system
directory. This new directory appears inside the
directory provided by getFilesDir().
• getCacheDir()
• Returns a File representing the cache directory on the
file system that's uniquely associated with your app.
This directory is meant for temporary files, and it
should be cleaned up regularly. The system may
delete files there if it runs low on disk space, so make
sure you check for the existence of your cache files
before reading them.
File Handling
• File directory = context.getFilesDir();
• File file = new File(directory, filename);
• Delete a file
• You should always delete files that your app no
longer need. The most straightforward way to
delete a file is to call delete() on the File object.
• myFile.delete();
Managing data using
SQLite database
• Managing data using SQLite database
• SQLite is a opensource SQL database that stores
data to a text file on a device. Android comes in with
built in SQLite database implementation.
• You don't need to establish any kind of connections
for it like JDBC,ODBC e.t.c
• android.database.sqlite package contains the classes
to manage your own databases.
• Create a database using an SQL helper
• In order to create a database you just need to call
this method openOrCreateDatabase with your
database name and mode as a parameter.
Managing data using
SQLite database

To access your database, instantiate your subclass


of SQLiteOpenHelper
FeedReaderDbHelper mDbHelper = newFeedReaderDbHelper(getContext());
Managing data using
SQLite database
• Put information into a database
• Insert data into the database by passing
a ContentValues object to the insert() method:
• SQLiteDatabase db=mDbHelper.getWritableDatabase();
• Read information from a database
• To read from a database, use the query() method,
passing it your selection criteria and desired
columns. The method combines elements
of insert() and update()
• SQLiteDatabase db=mDbHelper.getReadableDatabase();
Managing data using
SQLite database
• Delete information from a database
• To delete rows from a table, you need to provide
selection criteria that identify the rows to
the delete() method.
Managing data using
SQLite database
• Update a database
• When you need to modify a subset of your database
values, use the update() method.
• Updating the table combines the ContentValues syntax
of insert() with the WHERE syntax of delete().
• SQLiteDatabase db=mDbHelper.getWritableDatabase();
Managing data using
SQLite database
• Saving data to a database is ideal for repeating or
structured data, such as contact information.
• The APIs you'll need to use a database on Android are
available in the android.database.sqlite package.
Managing data using
SQLite database
• Create a database using an SQL helper
• Once you have defined how your database looks,
you should implement methods that create and
maintain the database and tables.
• Just like files that you save on the device's internal
storage, Android stores your database in your app's
private folder.
• Your data is secure, because by default this area is
not accessible to other apps or the user.
• The SQLiteOpenHelper class contains a useful set
of APIs for managing your database.
Managing data using
SQLite database
• Put information into a database
• Insert data into the database by passing
a ContentValues object to the insert() method:
• The first argument for insert() is simply the table
name.
• The second argument tells the framework what to do
in the event that the ContentValues is empty
Content providers
Content providers
Content providers
• Sharing access to your application data with other
applications
• Sending data to a widget
• Returning custom search suggestions for your
application through the search framework
using SearchRecentSuggestionsProvider.
• Synchronizing application data with your server using
an implementation of AbstractThreadedSyncAdapter
• Loading data in your UI using a CursorLoader
Content providers
• A content provider manages access to a central
repository of data.
• A provider is part of an Android application, which
often provides its own UI for working with the data.
• A content provider presents data to external
applications as one or more tables that are similar to
the tables found in a relational database.
• A content provider coordinates access to the data
storage layer in your application for a number of
different APIs.
Content providers
• Accessing a provider
• When you want to access data in a content provider,
you use the ContentResolver object in your
application's Context to communicate with the
provider as a client.
• The ContentResolver methods provide the basic
"CRUD" (create, retrieve, update, and delete)
functions of persistent storage.
•A common pattern for accessing
a ContentProvider from your UI uses
a CursorLoader to run an asynchronous query in the
background.
Android in build
content providers
• One of the built-in providers in the Android platform
is the user dictionary, which stores the spellings of
non-standard words that the user wants to keep.

You might also like