Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Persistence Nasrullah Niazi. Share Preferences The SharedPreferences class provides a general framework that allows you to save and retrieve persistent.

Similar presentations


Presentation on theme: "Data Persistence Nasrullah Niazi. Share Preferences The SharedPreferences class provides a general framework that allows you to save and retrieve persistent."— Presentation transcript:

1 Data Persistence Nasrullah Niazi

2 Share Preferences The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed).SharedPreferences

3 Continued.. To get a SharedPreferences object for your application, use one of two methods:SharedPreferences getSharedPreferences() - Use this if you need multiple preferences files identified by name, which you specify with the first parameter. getSharedPreferences() getPreferences() - Use this if you need only one preferences file for your Activity. Because this will be the only preferences file for your Activity, you don't supply a name. getPreferences()

4 Continued.. To write values: 1.Call edit() to get a SharedPreferences.Editor.edit()SharedPreferences.Editor 2.Add values with methods such as putBoolean() andputString().putBoolean()putString() 3.Commit the new values with commit ()commit ()

5 Continue.. To read values, use SharedPreferences methods suchSharedPreferences as getBoolean() and getString().getBoolean()getString()

6 1 To use the SharedPreferences object, you use the getSharedPreferences() method, passing it the name of the shared preferences file (in which all the data will be saved), as well as the mode in which it should be opened: private SharedPreferences prefs;... //---get the SharedPreferences object--- prefs = getSharedPreferences(prefName, MODE_PRIVATE); SharedPreferences.Editor editor = prefs.edit(); The MODE_PRIVATE constant indicates that the shared preference file can only be opened by the application that created it.

7 2 The Editor class allows you to save key/value pairs to the preferences file by exposing methods such as the following: putString() putBoolean() putLong()

8 SharedPrefrences The shared preferences file is saved as an XML file in the /data/ data/ /shared_prefs folder

9 Using getPreferences getSharedPreferences() The information is saved in the SharedPreferences objec and visible to all activities of the aplication if you don’t want to share the data between the activities you can use getPreferences(). Prefs=getPreferences(MODE_PRIVATE); getPreferences method does not require a name. and the data save is restircted to that created it.in this case the name used for the preferences file will be named after the activity that created it.

10 Input stream Most clients will use input streams that read data from the file system (FileInputStream), the network (getInputStream()/getInputStream()), or from an in-memory byte array (ByteArrayInputStream).FileInputStreamgetInputStream() ByteArrayInputStream Use InputStreamReader to adapt a byte stream like this one into a character stream.InputStreamReader Most clients should wrap their input stream with BufferedInputStream. Callers that do only bulk reads may omit buffering. BufferedInputStream

11 InputStream::read reads a single byte and returns it as an int InputStreamReader::read reads a single char (respecting the encoding) and returns this as an int If you want to read binary data use InputStream If you want to read strings from a binary stream, use InputStreamReader. One of its constructors allows you to specify a character set.

12 Whole story of Streaming in java An inputStreamReader is a bridge from bytesStream to character stream ; it reads bytes and decodes them into characters.it usually is wrapped inside a BufferReader.

13 Web services We have to work on two types of web services A.REST B.SOAP The web services can response in one of the following way XML SOAP JSON

14 Seekbar A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch the thumb and drag left or right to set the current progress level or use the arrow keys. Placing focusable widgets to the left or right of a SeekBar is discouraged. Clients of the SeekBar can attach a SeekBar.OnSeekBarChangeListener to be notified of the user's actions.SeekBar.OnSeekBarChangeListener A callback that notifies clients when the progress level has been changed. This includes changes that were initiated by the user through a touch gesture or arrow key/trackball as well as changes that were initiated programmatically.


Download ppt "Data Persistence Nasrullah Niazi. Share Preferences The SharedPreferences class provides a general framework that allows you to save and retrieve persistent."

Similar presentations


Ads by Google