Download - Persistent Data Storage
-
8/19/2019 Persistent Data Storage
1/14
Persistent Data Storage• Data Storage Media – Joannes Arthur Galon Pajatin• User Permission – Klieser Jun Asid Baluyos• SQLite Dataase – !arl Mar" #it $ %ai %ay A&o'iado
-
8/19/2019 Persistent Data Storage
2/14
D ata Storage M edia
▪ (he Android 'lat)orm* in &omination +ith Ja,a* 'ro,ideroust set o) tools that you &an use to store data-
▪ %egardless o) the situation* Android 'ro,ides ,arious o'+hen it &omes to storing data-
▪ Android 'ro,ides ,arious lo&ations to sa,e data- (hese as )ollo+s.– Shared 're)eren&es –(hese are 'ri,ate data stored in "ey ,alue
– #nternal storage –(his is a lo&ation +here all /les &an e sa,ed ode,i&e0s internal storage- By de)ault* /les stored in internal stora'ri,ate to your a''li&ation* and other a''li&ations &annot a&&ess1neither &an the user o) the de,i&e2- 3hen the user uninstalls thea''li&ation* the 'ri,ate /les are remo,ed-
-
8/19/2019 Persistent Data Storage
3/14
D ata Storage M edia
4 Lo&al &a&he –(his &an e used i) you don0t +ant to store data'ersistently- (he internal data dire&tory is +here you should &reate &a&he- #) you store data here and the system gets lo+ on internal sts'a&e* Android may delete these /les to re&laim s'a&e- (he limit o)&onsumale s'a&e is around 5MB-
4 67ternal storage –(his &an either e the remo,ale storage su&a Se&ure Digital !ard 1SD !ard2 or nonremo,aleinternal storage-
4 SQLite dataase –(his dataase is a light+eight SQL 1Stru&turQuery Language2 dataase im'lementation that is used y Androida,ailale a&ross ,arious 'lat)orms-
4 8et+or" &onne&tion –(his is also "no+n as remote storage- #t &any remote data sour&e that user ha,e a&&ess to-
-
8/19/2019 Persistent Data Storage
4/14
U ser Perm ission
▪ Storing data any+here on the de,i&e re9uires 'ermiss)rom the user in Android-
▪ 3hen users install a''li&ations )rom the Android Mar"ethe a''li&ation0s mani)est /le is ins'e&ted )or re9uired'ermissions that the a''li&ation needs to o'erate-
▪ Anytime the a''li&ation needs a&&ess to sensiti,e&om'onents su&h as e7ternal storage* a&&ess to the#nternet* 'hone de,i&e in)o* and so on* the user is noti/that the a''li&ation +ould li"e to a&&ess these &om'on
▪ #t is then u' to the user to de&ide +hether she +ould liinstall the a''li&ation-
-
8/19/2019 Persistent Data Storage
5/14
U ser Perm ission
▪ 3hen 'ermission is re9uested* you need to add thethe AndroidMani)est-7ml /le-
▪ 8o 'ermission is ne&essary to +or" +ith an SQLitedataase: there)ore* t+o 'ermissions should e addto the (as" %eminder a''li&ation that +ill e re9uire+hen the alarm manager &ode is added.– android-'ermission-%6!6#;6
– android-'ermission-3AK6
-
8/19/2019 Persistent Data Storage
6/14
U ser Perm ission
▪
A lot o) a''li&ations re9uire a&&ess to the #nternet to o'eraSome a''li&ations also need to +rite data to the SD !ard- need either o) these* you need to add the )ollo+ing 'ermis– #nternet. android-'ermission-#8(6%86(
– SD !ard. android-'ermission-3%#(6(6%8AL
-
8/19/2019 Persistent Data Storage
7/14
U ser Perm ission
▪ (he >ML 'ermission re9uest loo"s li"e this.@uses4'ermission android.nameandroid-'ermission-3AK6<C
▪ #) 'ermissions )or the a''li&ation are not de&lared* ta''li&ation +ill not )un&tion as e7'e&ted: sometimerun4time e7&e'tions +ill e thro+n and &rash your
a''li&ation-
-
8/19/2019 Persistent Data Storage
8/14
SQ Lite D atabase
▪
(he (as" %eminder a''li&ation needs a 'la&e to store anretrie,e tas"s* and the est 'la&e )or this "ind o) in)ormis inside an SQLite dataase-
▪ Eour a''li&ation needs to read* &reate* u'date* and deletas"s )rom the dataase-
▪ (he t+o a&ti,ities in the (as" %eminder a''li&ation need
'er)orm ,arious duties to o'erate-
▪ (as"%eminder6ditneeds to do the )ollo+ing.
– !reate a ne+ re&ord-
– %ead a re&ord so that it &an dis'lay the details )or editing-
– U'date the e7isting re&ord-
-
8/19/2019 Persistent Data Storage
9/14
-
8/19/2019 Persistent Data Storage
10/14
SQ Lite D atabase
▪
(he /rst thing to do is &reate a Ja,a /le in the Andro'roje&t that +ill house all the dataase4&entri& &ode
▪ 8ame this /le %emindersDAda'ter-ja,a-
▪ (his +ould e a sim'le im'lementation o) the ada'so)t+are engineering 'attern-
– (he ada'ter 'attern is sim'ly a +ra''er &lass that allo+sin&om'atile &lasses to &ommuni&ate +ith ea&h other-
– By &reating an ada'ter to handle the dataase &ommuni&atyou &an &ommuni&ate +ith this &lass ,ia the 'rogramminglanguage o) Ja,a +hile this ada'ter &lass does the translatioada'ts &ertain Ja,a re9uests into SQLite4s'e&i/& &ommands
-
8/19/2019 Persistent Data Storage
11/14
SQ Lite D atabase
▪
SQLite &reates a tale y the name o) reminders in dataase &alled data- (he &olumns and ho+ they0reuilt in the &reate s&ri't are des&ried as )ollo+s.– &reate tale DA(ABAS6
-
8/19/2019 Persistent Data Storage
12/14
SQ Lite D atabase
▪
(o &reate a dataase tale* an im'lementation o)SQLite='enel'ershould e 'ro,ided-
▪ (he )ollo+ing is the &ode )or the%emindersDAda'ter&lass-
▪ (his &reates a nested Ja,a &lass inside the
%emindersDAda'ter&lass-
-
8/19/2019 Persistent Data Storage
13/14
private static class DatabaseHelper extends SQLiteOpenHelper{
DatabaseHelper(Context context){
super(context, DATABAS!"A#, null, DATABAS!$%S&O"
'
Override
public void onCreate(SQLiteDatabase db){
dbexecSQL(DATABAS!C%AT)*
'
Override
public void on+prade(SQLiteDatabase db, int old$ersion, int "e-vers
Lo-(TA., /+pradin database 0ro1 version / 2 old$ersion 2 /to
ne-$ersion 2
2 /, -4ic4 -ill destro5 all old data3)*
dbexecSQL(/D%O6 TABL &7 8&STS / 2 DATABAS!TABL)*
on Create(db)*
'
'
-
8/19/2019 Persistent Data Storage
14/14
SQ Lite D atabase
▪
Line 5 is the im'lementation o) the SQLite='enel'er-▪ Line H &reates* o'ens* andCor manages a dataase- (he data
not a&tually &reated or o'ened until get%eadaleDataase12 oget3ritealeDataase12 is &alled on the SQLite='enel'erinstIin this &ase* it +ould e the mDel'er,ariale-
▪ Line &alls the on!reate12 method +hen the dataase is &reatthe /rst time-
▪ Line &reates the dataase and the dataase tale- (he e7e&Smethod a&&e'ts an SQL s&ri't string as a 'arameter- (his is ththat the SQLite dataase e7e&utes to &reate the dataase ta
▪ Line 5is use )or the u'grade o) the e7isting dataase- (his usonU'grade12 method-