Preferences.

Slides:



Advertisements
Similar presentations
Bruce Scharlau, University of Aberdeen, 2012 Data storage options for mobiles Mobile Computing.
Advertisements

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 data/data-storage.html#pref data/data-storage.html#pref 
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
CS 106 Introduction to Computer Science I 09 / 14 / 2007 Instructor: Michael Eckmann.
1 The Properties Pattern Based on
Preferences. 2 Persistent storage Information is persistent if it is kept between one invocation of a program and the next Many programs keep user preferences.
Data Storage: Part 1 (Preferences)
XP New Perspectives on Microsoft Office Access 2003 Tutorial 12 1 Microsoft Office Access 2003 Tutorial 12 – Managing and Securing a Database.
IS-907 Java EE JPA: Simple Object-Relational Mapping.
CS378 - Mobile Computing Persistence. Saving State We have already seen saving app state into a Bundle on orientation changes or when an app is killed.
4.1 Instance Variables, Constructors, and Methods.
The Java Programming Language
JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.
Nilesh Singh Local Data Storage option Android provides several options for you to save persistent application data. - Shared preferences - Creation.
Data persistence How to save data using SharedPreferences, Files, and SQLite database 1Data persistence.
23-Oct-15 Abstract Data Types. 2 Data types A data type is characterized by: a set of values a data representation, which is common to all these values,
JDBC Java and Databases. RHS – SOC 2 JDBC JDBC – Java DataBase Connectivity An API (i.e. a set of classes and methods), for working with databases in.
Diagnostic Pathfinder for Instructors. Diagnostic Pathfinder Local File vs. Database Normal operations Expert operations Admin operations.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Mathematical Calculations in Java Mrs. G. Chapman.
Programming in Java CSCI-2220 Object Oriented Programming.
(1) A Proposal for the Java Public Middleware API Vito Baggiolini SL/CO.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.
Preferences. 2 Persistent storage Information is persistent if it is kept between one invocation of a program and the next Many programs keep user preferences.
CHAPTER 9 File Storage Shared Preferences SQLite.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
CompSci Toward understanding data structures  What can be put in a TreeSet?  What can be sorted?  Where do we find this information?  How do.
SQL IMPLEMENTATION & ADMINISTRATION Indexing & Views.
Unit 4 – Technology literacy
Android Application Data Storage 1.
More Sophisticated Behavior
Working with Java.
Module 11: File Structure
Phil Tayco Slide version 1.0 Created Sep 18, 2017
3 Introduction to Classes and Objects.
Introduction to Classes and Objects
Introduction to Computer Science / Procedural – 67130
University of Central Florida COP 3330 Object Oriented Programming
Java Beans Sagun Dhakhwa.
Programming Language Concepts (CIS 635)
Namespaces, Scopes, Access privileges
Defining Classes and Methods
Microsoft Office Ribbon
Chapter 1: Computer Systems
Testing and debugging A short interlude 2-Dec-18.
Coding Concepts (Data Structures)
Java Classes and Objects 3rd Lecture
Microsoft Office Access 2003
Coding Concepts (Basics)
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Focus of the Course Object-Oriented Software Development
Introduction to Primitives
Introduction to Primitives
Testing and debugging A short interlude 17-Apr-19.
Preferences.
CS2013 Lecture 7 John Hurley Cal State LA.
CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.
Securing and Sharing a Presentation
Preferences.
slides created by Ethan Apter and Marty Stepp
Preferences.
Preferences.
Preferences.
Preference Activity class
Presentation transcript:

Preferences

Persistent storage Information is persistent if it is kept between one invocation of a program and the next Many programs keep user preferences in a persistent fashion Sometimes the user sets these explicitly (in, say, a dialog box) Sometimes these are implicit settings, such as window size and position To keep persistent information, Write it to a file whenever it is changed (or when the program quits), and Read it in again whenever the program starts up Java makes this very easy to do

Types of preferences Java distinguishes user preferences, for a particular user, and system preferences, for everybody To create or change system preferences, you need “administrator privileges” Each “program” may have its own preferences file Java saves and restores preferences for a particular class Preferences are kept in a hierarchical tree structure We will consider only the simplest version, in which preference settings act like a hash table

Constructing a Preferences object import java.util.prefs.*; public class MyProgram { ... } private Preferences userPrefs; private Preferences systemPrefs; userPrefs = Preferences.userNodeForPackage(MyProgram.class); systemPrefs = Preferences.systemNodeForPackage(MyProgram.class); Note that MyProgram.class returns the Class of MyProgram

Simple use of preferences myPrefs.put(String key, String value) Just like putting a value into a hash table The call is the same for both user and system preferences String value = myPrefs.get(String key, String default) Almost like getting a value from a hash table You must supply a default value, in case the key isn’t found Saving and reloading preferences You don’t—it all happens automatically Java puts it in some system-dependent location (based on the Class you gave it) that it knows about and can find again Your preferences are “just there,” the next time you use preferences for the same Class

Saving preferences The following are methods of your Preferences object: void put(String key, String value) void putBoolean(String key, boolean value) void putByteArray(String key, byte[] value) void putDouble(String key, double value) void putFloat(String key, float value) void putInt(String key, int value) void putLong(String key, long value) void remove(String key) // remove this preference void clear() // remove all preferences void sync() // update preferences file now

Getting saved preferences All getter methods must supply a default value, in case the preferences files does not exist or cannot be read The following are methods of your Preferences object: String get(String key, String default) boolean getBoolean(String key, boolean default) byte[] getByteArray(String key, byte[] default) double getDouble(String key, double default) float getFloat(String key, float default) int getInt(String key, int default) long getLong(String key, long default) String[] keys()

How does this work? Changes to the Preferences object (via put or putXXX) happen automatically--all file I/O is done for you You only need sync() if you want to force the file update to happen immediately rather than eventually We have treated the preferences file as if it were a flat file; however, it’s actually a tree structure Use slashes (/) to separate parts, just as in a file path If you want to know more, go to the API The preferences file is in XML format Java puts the preferences file in a system-dependent location on your computer You can export to/import from a particular file You can view/edit this XML directly if you like

Final comments As described in these slides, a Preferences object is simply a hash table that is “magically” kept between invocations of your program As such, preferences are really easy to use Preference files are really intended to be used for small amounts of persistent data (for example, preferences!) For large amounts of data, you really should use a database instead

The End