Presentation is loading. Please wait.

Presentation is loading. Please wait.

Android og persistens

Similar presentations


Presentation on theme: "Android og persistens"— Presentation transcript:

1 Android og persistens http://developer.android.com/guide/topics/data/data-storage.html 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.Shared Preferences Internal Storage Store private data on the device memory.Internal Storage External Storage Store public data on the shared external storage.External Storage SQLite Databases Store structured data in a private database.SQLite Databases Network Connection Store data on the web with your own network server.Network Connection

2 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);

3 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();

4 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; ………… }

5 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

6 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.


Download ppt "Android og persistens"

Similar presentations


Ads by Google