Android og persistens

scacchicgardenΛογισμικό & κατασκευή λογ/κού

13 Δεκ 2013 (πριν από 3 χρόνια και 10 μήνες)

102 εμφανίσεις

Android og persistens

http://developer.android.com/guide/topics/data/data
-
storage.html



Your data storage options are the following:


Shared Preferences

Store private primitive data in key
-
value pairs.


Internal Storage

Store private data on the device
memory.


External Storage

Store public data on the shared
external storage.


SQLite Databases

Store structured data in a private
database.


Network Connection

Store data on the web with your
own network server.

SharedPreferences

Skriv til
SharedPreferences


//
We

need

an Editor
object

to
make

preference

changes
.

//
All
objects

are

from
android.context.Context


SharedPreferences

settings

=
getSharedPreferences
("
MyPrefsFile
", 0
);


SharedPreferences.Editor

editor =
settings.edit
();


editor.putBoolean
("
silentMode
",
mSilentMode
);



Læs fra
SharedPreferences



SharedPreferences

settings

=
getSharedPreferences
("
MyPrefsFile
", 0
);


boolean

silent

=
settings.getBoolean
("
silentMode
", false
);


setSilent
(
silent
);


Tekstfiler i Android (java)

Skriv på tekstfil


FileOutputStream

outStream

=
openFileOutput
("
myfiletext
",
Context.
MODE_PRIVATE
);


PrintWriter

outTextStream

=
new

PrintWriter
(
outStream
);


outTextStream.println
("En tekst");


outTextStream.close
();


Læs fra tekstfil


FileInputStream

inFileStream

=
openFileInput
("
myfiletext
");


InputStreamReader

inReader

=
new

InputStreamReader
(
inFileStream
);


BufferedReader

buffReader

=
new

BufferedReader
(
inReader
);


String text;


text = (String)
buffReader.readLine
();


buffReader.close
();

Serialisering af java
-
Objekter

For at
serialisere

skal man implementere
Serializable

Klassen skal desuden forsynes med en
unique

ID




import
java.io.Serializable
;



public
class

BusinessModel

implements

Serializable

{


private
static

final long
serialVersionUID

=
-
2255373015564100242L;


…………


}




Filer og java
-
Objekter

Skriv objekt til fil


FileOutputStream

outFileStream

=
openFileOutput
("
myfileobjects
”,
Context.
MODE_PRIVATE
);


ObjectOutputStream

outObjectStream

=
new

ObjectOutputStream
(
outFileStream
);


String

simpleObject

= "Hej "+(i+1);


outObjectStream.writeObject
(
simpleObject
);


outTextStream.close
();


Læs objekt fra fil


FileInputStream

inStream

=
openFileInput
("
myfileobjects
");


ObjectInputStream

inObjectStream

=
new

ObjectInputStream
(
inStream
);


String text;


String
simpleObject
;


simpleObject

= (String)
inObjectStream.readObject
();


inObjectStream.close
();

Check for
exception

EOFException


public directories on the
external storage


Music/
-

Media scanner classifies all media found here as user music.


Podcasts/
-

Media scanner classifies all media found here as a podcast.


Ringtones/
-

Media scanner classifies all media found here as a ringtone.


Alarms/
-

Media scanner classifies all media found here as an alarm sound.


Notifications/
-

Media scanner classifies all media found here as a
notification sound.


Pictures/
-

All photos (excluding those taken with the camera).


Movies/
-

All movies (excluding those taken with the camcorder).


Download/
-

Miscellaneous downloads.