Android og persistens http developer android comguidetopicsdatastorage html

  • Slides: 6
Download presentation
Android og persistens http: //developer. android. com/guide/topics/data-storage. html • Your data storage options are

Android og persistens http: //developer. android. com/guide/topics/data-storage. html • Your data storage options are the following: • Shared Preferences Store private primitive data in keyvalue 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.

Shared. Preferences Skriv til Shared. Preferences // We need an Editor object to make

Shared. Preferences Skriv til Shared. Preferences // We need an Editor object to make preference changes. // All objects are from android. context. Context • Shared. Preferences settings = get. Shared. Preferences("My. Prefs. File", 0); • Shared. Preferences. Editor editor = settings. edit(); • editor. put. Boolean("silent. Mode", m. Silent. Mode); Læs fra Shared. Preferences • • • Shared. Preferences settings = get. Shared. Preferences("My. Prefs. File", 0); boolean silent = settings. get. Boolean("silent. Mode", false); set. Silent(silent);

Tekstfiler i Android (java) Skriv på tekstfil • • File. Output. Stream out. Stream

Tekstfiler i Android (java) Skriv på tekstfil • • File. Output. Stream out. Stream = open. File. Output("myfiletext", Context. MODE_PRIVATE); Print. Writer out. Text. Stream = new Print. Writer(out. Stream); out. Text. Stream. println("En tekst"); out. Text. Stream. close(); Læs fra tekstfil • • • File. Input. Stream in. File. Stream = open. File. Input("myfiletext"); Input. Stream. Reader in. Reader = new Input. Stream. Reader(in. File. Stream); Buffered. Reader buff. Reader = new Buffered. Reader(in. Reader); String text; text = (String) buff. Reader. read. Line(); buff. Reader. close();

Serialisering af java-Objekter For at serialisere skal man implementere Serializable Klassen skal desuden forsynes

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 Business. Model implements Serializable { private static final long serial. Version. UID = -2255373015564100242 L; ………… }

Filer og java-Objekter Skriv objekt til fil • • • File. Output. Stream out.

Filer og java-Objekter Skriv objekt til fil • • • File. Output. Stream out. File. Stream = open. File. Output("myfileobjects”, Context. MODE_PRIVATE); Object. Output. Stream out. Object. Stream = new Object. Output. Stream(out. File. Stream); String simple. Object = "Hej "+(i+1); out. Object. Stream. write. Object(simple. Object); out. Text. Stream. close(); Læs objekt fra fil • File. Input. Stream in. Stream = open. File. Input("myfileobjects"); • Object. Input. Stream in. Object. Stream = new Object. Input. Stream(in. Stream); • String text; • String simple. Object; • simple. Object = (String) in. Object. Stream. read. Object(); • in. Object. Stream. close(); Check for exception EOFException

public directories on the external storage • • Music/ - Media scanner classifies all

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.