MAD Unit 4
MAD Unit 4
4 Persistently
Syllabus . . . .
. the Data Storage Optio ns, Uszng the internal storage, uszng the external storage, using the
JnJroducing . .
e Work ing with content provider.
sQLile Databas ,
-Contents
4. 1
storage Data Folder
al Storage
4_2 Using /ntem
Storage
4_3 Using External
4_4 Shared Preferences
4.5 SQUte
4.6 Content Provider
4. 7 Handling Database in Android
4.8 Short Questions and Answers
4.9 Multiple Choice Questions
~M ~o b~ il~ e!_ A~ pp
I ~l~ ic~
t at~ io~ n_ 4 _2
:D ~e ~v e~ o~ p~ m~ e~ n~ --- --~Storing the Data PersistenN"
-= --- --- --- --- --- --- .!_
B l Storage Data Folder
Store application-specific data
• The application dat a folder is a
special hid den folder tha t yo ur ap
store application-specific data, suc h p can use to
as configuration files.
• The application dat a folder is aut
omatically cre ate d wh en yo u att em
file in it. Use this folder to store any pt to create a
files tha t the use r sho uld n't dir ect
with. ly interact
• This folder is only accessible by
yo ur application and its con ten ts are
the use r and from oth er Drive apps. hid de n from
• The application dat a folder is del
eted wh en a use r uni nst all s yo ur
MyDrive. Users can also delete yo ur ap p fro m their
app 's dat a folder ma nua lly .
Il l• Application Data Folder Scope
• Before you can access the app lic
ati on dat a folder, yo u mu st req ues
htt ps: / /w ww .go og lea pis .co m/ au th/ t acc ess to the
dri ve. app dat a scope. Fo r mo re
about scopes and ho w to req ues t acc inf orm ati on
ess to the m, ref er to Au the nti cat e
yo ur use rs.
li fj Creating File in App Data Folder
• To create a file in the applicatio
n dat a folder, spe cif y app Da taF old
pro per ty of the file and use the file er in the parents
s.create me tho d to up loa d the file
to the folder.
• The following example sho ws
ho w to ins ert a file int o a fol der
library : usi ng a client
user by p1uggmg
computer". Example: When we say Nexus 5 32 GB.
2. Secondary External Storage : Removable storage. Example : SD Card
• All applications can read and write files placed on the external
storage and the
can
user can remove them. We need to check if the SD card is available and if we
we
write to it. Once we've checked that the external storage is available only then
can write to it else the save button would be disabled.
and
• Firstly, we need to make sure that the application has permission to read
add
write data to the users SD card, so lets open up the AndroidManifest.xml and
the following permissions :
• Also, external storage may be tied up by the user havin g moun ted
it as a USE
is no!
storage device. So we need to check if the external storage is available and
read only.
extern~
getExternalStorageState() is a static method of Environment to deter mine if
false we've
storage is presently available or not. As you can see if the condi tion is
disabled the save button.
1. Environment.getExternalStorageState() : returns path to intern al SD moun
t point like
"/mnt /sdca rd".
2. getExterna/FilesDir() : It returns the path to files folder inside
And~ oid/~ ata/da ta/app licati on_pa ckage / on the SD card. It is used to store
any
reqwred_ files. for your app (like images down loade d from web or cache files).
Ono
the app 1s urunstalled, any data stored in this folder is gone too.
ferences
- Creating Private and Shared Pre
, Individual activities . can hav e the ir ow n pri vate pref erences. These preferences are
. th ...
for the .sp eci fic Ac tiv ity on ly an d are no t shared WI·th o er actiVIties wit hin the
appIica tion.
of private preferences.
• The activity get s on ly on e gro up
activity's private preferences :
• The following cod e ret rie ves the
.: i 1 1 ! 1 :ii!!~1W!:JA~);
red pre fer enc es by nam e from any activity in the application .
• You can access sha
it to the nu mb er of dif fer ent shared preferences you can create .
There is no lim
e sha red pre fer enc es cal led Us erNetworkPreferences and another
You can have som
called Ap pD isp la y Preferences.
• How you organize sha red prefer
ences is up to you, the developer .
variable (in a base class
ur preference nam e as a
• How ever/ you wa nt to dec lar e yo ulti 1 cti vi ties · For example
nam e across m Pe a
or ucader) so that you can reuse the
1. _
CE_FILENAME • •A,ppPTe&' ;
~l lc Btatic 6- 1 StriDa PREFEREN
• SQLlte ~ management
I d ata b
provi·d_es a rel aho na
• ·SQ1Y~
_· tw.
is a sof tv.1a1e lib rar y that
S\'S tem
.gh •ght in terms of setup, database administ ration
• The lite in SQLlte means li t we1 ,
and required resoq.rce.
• A SQLlte database is a good storage so1ution w
· hen you have structur ed data ~ t
you need to store persistently and access, search, and change frequent ly.
• SQLlte is self-contained means 1t· · · · al support from the operating
reqwres nurum
system or external library.
• This makes SQLite usable in any environments especially in embedd ed devices
like iPhones, Android phones, game consoles, handhel d media players, etc.
• SQLite is developed using ANSI-C. The source code is available as a big sqlite3.c
and its header file sqlite3.h.
• If you want to develop an application that uses SQLite, you just need to drop
these files into your project and compile it with your code.
• SQLlte databases are very lightweight. Unlike other database systems , there is no
configuration, installation required to start working on an SQLite database .
• :'11en you use a SQLite database, all interactions with the database are through an
instance of the SQLiteOpenHelper class which executes your requests and manages
your database for you.
• :n,e ~droid SQLite Database requires very little memory (around 250kb), which
IS available on all android devices.
Widgets
Other applications
ContentProvider
Search
Fig. 4.6.1
k=
1
, You need not use the primar y key (id) to delete records; the wm~··":~:::: ·:
entirely up to you. For instance, the following function deletes all book records in
the table.
·a .r ···--'
. . . . •. . . ·-· .,. . . .• . .•...•...... . -. -·-·-·-•...... .. . . . . ·~-·-·.·.•.•-. ·•.:::.:.-.:.,.•:.::-:_ ·-·-•............. ·.. ·..· ;
·.. ·=:-:•:.:::::;: • • :,:,:,:,'.·',·.·:.•'·':
} .... • .
• , ',·
'·'.'·.'·.'.·•,·,...•.,·,. ·.··.•.·•.,.··.·.·.=.,·.:.:.·...·,'·,.'.,.·,·.·,.' ..',:.,:,:••.'.:.·.'·
. ..
' ·:'.:,·.:.·.·.'•.:.,.' ·,' ,:.,,·,,·,:.,.' .,'.:
..:,..·,:,'•.:.,,·,.::,:.·::...,•:.,.' ,'..','....
. :::::.::::t:=:::::::::
:•:•:::·::=::::::,:,;:::,::. . .
::::: /::::::=:=~·== :=: ~:=::-\:it\\-:::.~:=:=:::.::::J:::=:::::://J?:AiJ@( MJ:f/f?f. . .:::)?\:\(\;
.:,:,:;:,::::::::,•:.:,:::::::c:::::::·::,:;: . , ........ ·•· .
:\t{=/\{/t://}(~ :)/:i/r··· ·· ./ ✓
• You call a method of Conten tResolv er with argume nts that are passed to the
corresponding metho d of Conten tProvid er.
• The provider and provid er client automa tically handle security and inter-process
communication.
QI Inserting Data
• To insert data into a provid er, you call the ContentResolver.insert() meiliod . This
th31
method inserts a new row into the provid er and returns a content URI for
row.
Mobile Application Development 4- 10
,
•.·-:;:-: :-·.·= - __ _ ;{f. i?.. :.;:::,.::....=·:{:J;./~
=._•••.:. •/_:_;_·;,•_/.t_:!_i._•_
' ......... :(:)t;::::=\/:_
.-.. .. '
::-.:::;::: .... ... -.- ..'_: _
>?tf:.-.:
...•. .-. .-:.. ·.-:>· :> ··::;:• :-:-·-· •.-.-:-: :_ :,.. •.
::=·: ..::::- • _-_-::::···-··.:=:· ·,•::·.:-:·::·:: . ::;:/•--::_.;:=·
•,• •,• · .· ..-.-.- .·.•.·.•·-··:-t=·=· ./ \:::/=::):==:-. -· :·:·:-:..•:•··..
.:·::=: i-::\\i/·/ \?\:. :/:. /~\:;//:;;//\:\:. :··:;::.>;::" ·,:..;.
: hl;h;~,sinti i,
::-: <;:;:•·-~-:-: / ( \\:: :; .} /
le ContentValues obj~~:
• The dat a for the new row goes into a sing
in this object don't need to have llie
in form to a one-row cursor. The columns
ify a value at all, you can set a
sam e dat a type, and if you don't want to spec
col um n to null using ContentValues.putNull().
because this column is maintained
• The sni ppe t doesn't add the _ID column,
value of _ID to every row that ~
automatically. The provider assigns a unique
table's primary key.
add ed. Providers usually use this value as the
tifies the newly-added row, with the
• The con ten t URI retu rne d in newUri iden
following form at :
;:::::+t\:f}t:f§:\- l®n~pt;tZ.iifitin~~Pn$.XYLW<->t4@l~~l.tt£v.~u'~'>: s:;:.::::1::: ,:;I::Z'/,,:;":;'.;::;;;iMo;t:~;~{:7st :',prov
:i::::;:::::;::::~
iders can detoo
new row.
• The <id_value> is the contents of _ID for the
n perform the requested operatioo
this for m of con ten t URI automatically and the
on tha t par ticu lar row.
To get the val ue of _ID from the retu rne d Uri
, call ContentUris.parseld().
•
lf fj Updating Data
. h th e up d ated values jU51Th~
.ect w1t
s obJ
• To upd ate a row , you use a Con ten tVa lue · · ·
• n cnt ena 1ust as you do with a query.
ctio
you do wit h an insertion, and sele
d vou use is Con ten tRe sol ver .update().
:.-<:::·:~.:=::/;\; .:·(·
-
• You should also sanitize user input when you call ContentResolver.update().
\
• Yo u sh ou ld al so sanitize us er in pu t wh
en yo u call ContentResolver.delete() . I