60 likes | 75 Vues
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.
E N D
Android og persistenshttp://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 // Weneed an Editor object to makepreferencechanges.// All objectsare from android.context.Context • SharedPreferencessettings = getSharedPreferences("MyPrefsFile", 0); • SharedPreferences.Editoreditor = settings.edit(); • editor.putBoolean("silentMode", mSilentMode); Læs fra SharedPreferences • SharedPreferencessettings = getSharedPreferences("MyPrefsFile", 0); • booleansilent = settings.getBoolean("silentMode", false); • setSilent(silent);
Tekstfiler i Android (java) Skriv på tekstfil • FileOutputStreamoutStream = openFileOutput("myfiletext",Context.MODE_PRIVATE); • PrintWriteroutTextStream = newPrintWriter(outStream); • outTextStream.println("En tekst"); • outTextStream.close(); Læs fra tekstfil • FileInputStreaminFileStream = openFileInput("myfiletext"); • InputStreamReaderinReader = newInputStreamReader(inFileStream); • BufferedReaderbuffReader = newBufferedReader(inReader); • String text; • text = (String) buffReader.readLine(); • buffReader.close();
Serialisering af java-Objekter For at serialisere skal man implementere SerializableKlassen skal desuden forsynes med en unique ID • import java.io.Serializable; • public classBusinessModelimplementsSerializable { • private static final long serialVersionUID = -2255373015564100242L; • ………… • }
Filer og java-Objekter Skriv objekt til fil • FileOutputStreamoutFileStream = openFileOutput("myfileobjects”,Context.MODE_PRIVATE); • ObjectOutputStreamoutObjectStream = newObjectOutputStream(outFileStream); • StringsimpleObject = "Hej "+(i+1); • outObjectStream.writeObject(simpleObject); • outTextStream.close(); Læs objekt fra fil • FileInputStreaminStream = openFileInput("myfileobjects"); • ObjectInputStreaminObjectStream = newObjectInputStream(inStream); • String text; • String simpleObject; • simpleObject = (String) inObjectStream.readObject(); • inObjectStream.close(); Check for exceptionEOFException
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.